File: yangrpc.c

package info (click to toggle)
yuma123 2.14-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,436 kB
  • sloc: ansic: 185,144; cpp: 10,968; python: 7,990; sh: 2,676; makefile: 1,175; xml: 807; exp: 776; perl: 70
file content (150 lines) | stat: -rw-r--r-- 4,228 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <Python.h>

#include "ncx.h"
#include "ncxmod.h"
#include "ncxtypes.h"
#include "status.h"

#include "procdefs.h"
#include "dlq.h"
#include "rpc.h"
#include "yangrpc.h"

static int yangrpc_init_done = 0;

static PyObject *
py_yangrpc_connect(PyObject *self, PyObject *args)
{
    status_t res;
    char* server;
    int port;
    char* user;
    char* password;
    char* private_key;
    char* public_key;
    char* other_args=NULL;
    yangrpc_cb_ptr_t yangrpc_cb_ptr;


    if (!PyArg_ParseTuple(args, (char *) "siszzz|z:yangrpc_yangrpc_connect", &server,&port,&user,&password,&public_key,&private_key,&other_args)) {
        return (NULL);
    }
    if(yangrpc_init_done==0) {
        res=yangrpc_init(NULL);
        assert(res==NO_ERR);
        yangrpc_init_done=1;
    }

    res = yangrpc_connect(server, port, user, password, public_key, private_key, other_args, &yangrpc_cb_ptr);
    if(res!=NO_ERR) {
        Py_RETURN_NONE; /*returning*/
    }
    return Py_BuildValue("O", PyCapsule_New(yangrpc_cb_ptr, "yangrpc_cb_ptr_t", NULL));
}

static PyObject *
py_yangrpc_exec(PyObject *self, PyObject *args)
{
    PyObject *py_retval;

    PyObject *py_yangrpc_cb_ptr;
    PyObject *py_rpc_val;

    int res;
    yangrpc_cb_ptr_t yangrpc_cb_ptr;
    val_value_t  *rpc_val;
    val_value_t  *reply_val;

    if (!PyArg_ParseTuple(args, (char *) "OO:yangrpc_yangrpc_exec", &py_yangrpc_cb_ptr, &py_rpc_val)) {
        return (NULL);
    }

    yangrpc_cb_ptr = (val_value_t*)PyCapsule_GetPointer(py_yangrpc_cb_ptr, "yangrpc_cb_ptr_t");
    rpc_val = (val_value_t*)PyCapsule_GetPointer(py_rpc_val, "val_value_t_ptr");

    res = yangrpc_exec(yangrpc_cb_ptr, rpc_val, &reply_val);

    py_retval = PyTuple_New(2);
    PyTuple_SetItem(py_retval, 0, Py_BuildValue("i", (int)res));
    PyTuple_SetItem(py_retval, 1, reply_val?Py_BuildValue("O", PyCapsule_New(reply_val, "val_value_t_ptr", NULL)):Py_None);
    return py_retval;
}

static PyObject *
py_yangrpc_parse_cli(PyObject *self, PyObject *args)
{
    PyObject *py_retval;

    PyObject *py_yangrpc_cb_ptr;
    PyObject *py_rpc_val;

    int res;
    yangrpc_cb_ptr_t *yangrpc_cb_ptr;
    val_value_t  *rpc_val;
    char* cmd;    

    if (!PyArg_ParseTuple(args, (char *) "Os:yangrpc_yangrpc_parse_cli", &py_yangrpc_cb_ptr, &cmd)) {
        return (NULL);
    }

    yangrpc_cb_ptr = (yangrpc_cb_ptr_t *)PyCapsule_GetPointer(py_yangrpc_cb_ptr, "yangrpc_cb_ptr_t");

    res = yangrpc_parse_cli(yangrpc_cb_ptr, cmd, &rpc_val);

    py_retval = PyTuple_New(2);
    PyTuple_SetItem(py_retval, 0, Py_BuildValue("i", (int)res));
    PyTuple_SetItem(py_retval, 1, rpc_val?Py_BuildValue("O", PyCapsule_New(rpc_val, "val_value_t_ptr", NULL)):Py_None);
    return py_retval;
}

static PyObject *
py_yangrpc_close(PyObject *self, PyObject *args)
{
    PyObject *py_retval;

    PyObject *py_yangrpc_cb_ptr;

    int res;
    yangrpc_cb_ptr_t yangrpc_cb_ptr;
    val_value_t  *rpc_val;
    val_value_t  *reply_val;

    if (!PyArg_ParseTuple(args, (char *) "O:yangrpc_yangrpc_close", &py_yangrpc_cb_ptr)) {
        return (NULL);
    }
    yangrpc_cb_ptr = (yangrpc_cb_ptr_t *)PyCapsule_GetPointer(py_yangrpc_cb_ptr, "yangrpc_cb_ptr_t");

    yangrpc_close(yangrpc_cb_ptr);

    Py_RETURN_NONE;
}

/*  define functions in module */
static PyMethodDef yang_rpc_methods[] =
{
     {"connect", py_yangrpc_connect, METH_VARARGS, "connects to client"},
     {"rpc", py_yangrpc_exec, METH_VARARGS, "executes rpc by sending the rpc data contents and blocking until reply is received"},
     {"parse_cli", py_yangrpc_parse_cli, METH_VARARGS, "converts cli string to rpc data contents"},
     {"close", py_yangrpc_close, METH_VARARGS, "close session"},
     {NULL, NULL, 0, NULL}
};

/* module initialization */
static struct PyModuleDef py_mod_def =
{
    PyModuleDef_HEAD_INIT,
    "yangrpc", /* name of module */
    "",          /* module documentation, may be NULL */
    -1,          /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
    yang_rpc_methods
};

PyMODINIT_FUNC
PyInit_yangrpc(void)
{
    PyObject *m;

    //(void) Py_InitModule("yangrpc", yang_rpc_methods);
    m = PyModule_Create(&py_mod_def);
    return m;
}