|
17 | 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 | 18 | */ |
19 | 19 |
|
| 20 | +#include <Python.h> |
20 | 21 | #include <stdio.h> |
21 | 22 | #include <stdlib.h> |
22 | 23 | #include <sys/types.h> |
@@ -89,6 +90,81 @@ void reconfig(int) |
89 | 90 | int main(int, char * []) |
90 | 91 |
|
91 | 92 | { |
| 93 | + // BEGINNING OF PYTHON-RELATED CODE |
| 94 | + |
| 95 | + // Initialize the Python interpreter |
| 96 | + Py_Initialize(); |
| 97 | + // Important: Replace with absolute path to Python files folder |
| 98 | + PyRun_SimpleString("import sys\nfrom time import time,ctime; sys.path.insert(0, '/ospf/python')\nprint('Python started at',ctime(time()))\n"); |
| 99 | + |
| 100 | + // Run Python function returning a string, with no arguments |
| 101 | + PyObject *pName, *pModule, *pFunc, *pArgs, *pValue; |
| 102 | + pName = PyUnicode_FromString((char*)"example"); |
| 103 | + pModule = PyImport_Import(pName); |
| 104 | + pFunc = PyObject_GetAttrString(pModule, (char*)"no_arguments"); |
| 105 | + pArgs = PyTuple_Pack(0, NULL); |
| 106 | + pValue = PyObject_CallObject(pFunc, pArgs); |
| 107 | + char* result = {}; |
| 108 | + result = _PyUnicode_AsString(pValue); |
| 109 | + printf("%s", result); |
| 110 | + |
| 111 | + // Run Python function returning a string, with one argument |
| 112 | + pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"string_example"); |
| 113 | + pArgs = PyTuple_Pack(1, PyUnicode_FromString((char*)"This experiment was successful")); |
| 114 | + result = _PyUnicode_AsString(PyObject_CallObject(pFunc, pArgs)); |
| 115 | + printf("%s", result); |
| 116 | + |
| 117 | + // Run Python function returning an int |
| 118 | + pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"int_example"); |
| 119 | + pArgs = PyTuple_Pack(1, PyLong_FromLong(10)); |
| 120 | + long long_result = 0; |
| 121 | + long_result = PyLong_AsLong(PyObject_CallObject(pFunc, pArgs)); |
| 122 | + printf("%s%d%s", "The received number was: ", long_result, "\n"); |
| 123 | + |
| 124 | + // Run Python function returning a boolean |
| 125 | + pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"bool_example"); |
| 126 | + pArgs = PyTuple_Pack(1, Py_False); |
| 127 | + bool bool_result = 0; |
| 128 | + bool_result = PyBool_Check(PyObject_CallObject(pFunc, pArgs)); |
| 129 | + printf("%s%d%s", "The received boolean was: ", bool_result, "\n"); |
| 130 | + |
| 131 | + // Run Python function returning a bytes object |
| 132 | + pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"bytes_example"); |
| 133 | + pArgs = PyTuple_Pack(1, PyBytes_FromString("abcd\x61\x62\x63\x64")); |
| 134 | + result = PyBytes_AsString(PyObject_CallObject(pFunc, pArgs)); |
| 135 | + printf("%s%s%s", "The received bytes were: ", result, "\n"); |
| 136 | + |
| 137 | + // Run Python function returning a list |
| 138 | + pFunc = PyObject_GetAttrString(PyImport_Import(PyUnicode_FromString((char*)"example")), (char*)"list_example"); |
| 139 | + PyObject *list = PyList_New(0); |
| 140 | + PyList_Append(list, PyUnicode_FromString((char*)"a")); |
| 141 | + PyList_Append(list, PyUnicode_FromString((char*)"b")); |
| 142 | + pArgs = PyTuple_Pack(1, list); |
| 143 | + PyObject *list_result = PyObject_CallObject(pFunc, pArgs); |
| 144 | + printf("%s%d%s", "The length of the received list is: ", PyList_Size(list_result), "\n"); |
| 145 | + const char *element_1 = PyUnicode_AS_DATA(PyList_GetItem(list_result, 0)); |
| 146 | + const char *element_2 = PyUnicode_AS_DATA(PyList_GetItem(list_result, 1)); |
| 147 | + const char *element_3 = PyUnicode_AS_DATA(PyList_GetItem(list_result, 2)); |
| 148 | + printf("%s%s%s%s%s", "The elements of the received list are: ", element_1, element_2, element_3, "\n"); |
| 149 | + |
| 150 | + // Create a custom data object, pack it to bytes, and unpack the bytes back to the original data object |
| 151 | + PyObject *module = PyImport_ImportModule((char*)"data_object"); |
| 152 | + PyObject *custom_class = PyObject_GetAttrString(module, (char*)"ExtensionAbr"); |
| 153 | + PyObject *instance = PyObject_CallObject(custom_class, PyTuple_Pack(0, NULL)); |
| 154 | + PyObject *metric = PyLong_FromLong(10); |
| 155 | + PyObject *neighbor_router_id = PyUnicode_FromString((char*)"1.1.1.1"); |
| 156 | + PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"add_abr_info"), metric, neighbor_router_id, NULL); |
| 157 | + PyObject *printed_object = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"__str__"), NULL); |
| 158 | + printf("%s%s%s", "The printed custom object is: ", _PyUnicode_AsString(printed_object), "\n"); |
| 159 | + PyObject *object_bytes = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"pack_lsa_body"), NULL); |
| 160 | + PyObject *restored_instance = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString((char*)"unpack_lsa_body"), object_bytes, NULL); |
| 161 | + printed_object = PyObject_CallMethodObjArgs(restored_instance, PyUnicode_FromString((char*)"__str__"), NULL); |
| 162 | + printf("%s%s%s", "The restored printed custom object is: ", _PyUnicode_AsString(printed_object), "\n"); |
| 163 | + |
| 164 | + Py_Finalize(); |
| 165 | + |
| 166 | + // END OF PYTHON-RELATED CODE |
| 167 | + |
92 | 168 | int n_fd; |
93 | 169 | itimerval itim; |
94 | 170 | fd_set fdset; |
|
0 commit comments