| 9 | | #define GetString(name) PyString_AsString(PyMapping_GetItemString(item, name)) |
| 10 | | #define GetLong(name) PyInt_AsLong(PyMapping_GetItemString(item, name)) |
| 11 | | #define GetFloat(name) PyFloat_AsDouble(PyMapping_GetItemString(item, name)) |
| | 9 | /* functions to put data into dict */ |
| | 10 | inline static void insert_long_into_dict(PyObject* dict, char* key, long value) { |
| | 11 | PyObject* item=PyInt_FromLong(value); |
| | 12 | PyDict_SetItemString(dict, key, item); |
| | 13 | Py_DECREF(item); |
| | 14 | } |
| | 15 | |
| | 16 | inline static void insert_str_into_dict(PyObject* dict, char* key, const char* value) { |
| | 17 | if (!value) return; |
| | 18 | PyObject* item=PyString_FromString(value); |
| | 19 | PyDict_SetItemString(dict, key, item); |
| | 20 | Py_DECREF(item); |
| | 21 | } |
| | 22 | |
| | 23 | inline static void insert_double_into_dict(PyObject* dict, char* key, double value) { |
| | 24 | PyObject* item=PyFloat_FromDouble(value); |
| | 25 | PyDict_SetItemString(dict, key, item); |
| | 26 | Py_DECREF(item); |
| | 27 | } |
| | 28 | |
| | 29 | static PyObject* farsight_transport_info_to_dict(FarsightTransportInfo* fti) { |
| | 30 | PyObject* dict = PyDict_New(); |
| | 31 | |
| | 32 | insert_str_into_dict (dict, "candidate_id", fti->candidate_id); |
| | 33 | insert_long_into_dict (dict, "component", fti->component); |
| | 34 | insert_str_into_dict (dict, "ip", fti->ip); |
| | 35 | insert_long_into_dict (dict, "port", fti->port); |
| | 36 | insert_long_into_dict (dict, "proto", fti->proto); |
| | 37 | insert_str_into_dict (dict, "proto_subtype", fti->proto_subtype); |
| | 38 | insert_str_into_dict (dict, "proto_profile", fti->proto_profile); |
| | 39 | insert_double_into_dict (dict, "preference", fti->preference); |
| | 40 | insert_long_into_dict (dict, "type", fti->type); |
| | 41 | insert_str_into_dict (dict, "username", fti->username); |
| | 42 | insert_str_into_dict (dict, "password", fti->password); |
| | 43 | |
| | 44 | return dict; |
| | 45 | } |
| 15 | | PyObject* item; |
| 16 | | |
| 17 | | PyDict_SetItemString(dict, "id", item=PyInt_FromLong(fc->id)); |
| 18 | | Py_DECREF(item); |
| 19 | | |
| 20 | | PyDict_SetItemString(dict, "encoding_name", item=PyString_FromString(fc->encoding_name)); |
| 21 | | Py_DECREF(item); |
| 22 | | |
| 23 | | PyDict_SetItemString(dict, "media_type", item=PyInt_FromLong(fc->media_type)); |
| 24 | | Py_DECREF(item); |
| 25 | | |
| 26 | | PyDict_SetItemString(dict, "clock_rate", item=PyInt_FromLong(fc->clock_rate)); |
| 27 | | Py_DECREF(item); |
| 28 | | |
| 29 | | PyDict_SetItemString(dict, "channels", item=PyInt_FromLong(fc->channels)); |
| 30 | | Py_DECREF(item); |
| | 49 | |
| | 50 | /* these two are required */ |
| | 51 | insert_long_into_dict(dict, "id", fc->id); |
| | 52 | insert_str_into_dict(dict, "encoding_name", fc->encoding_name); |
| | 53 | |
| | 54 | /* next are optional */ |
| | 55 | if (fc->media_type) insert_long_into_dict(dict, "media_type", fc->media_type); |
| | 56 | if (fc->clock_rate) insert_long_into_dict(dict, "clock_rate", fc->clock_rate); |
| | 57 | if (fc->channels) insert_long_into_dict(dict, "channels", fc->channels); |
| | 58 | |
| | 59 | if (fc->optional_params) { |
| | 60 | PyObject* params = PyDict_New(); |
| | 61 | GList* list; |
| | 62 | |
| | 63 | for(list=fc->optional_params; list; list=g_list_next(list)) { |
| | 64 | FarsightCodecParameter *fcp=list->data; |
| | 65 | insert_str_into_dict(params, fcp->name, fcp->value); |
| | 66 | } |
| | 67 | |
| | 68 | PyDict_SetItemString(dict, "params", params); |
| | 69 | Py_DECREF(params); |
| | 70 | } |
| | 74 | |
| | 75 | /* functions to get data from dict */ |
| | 76 | /* next three functions might raise an error */ |
| | 77 | inline static long get_long_from_dict(PyObject* dict, char* key) { |
| | 78 | PyObject* pyint=PyMapping_GetItemString(dict, key); |
| | 79 | return(pyint?PyInt_AsLong(pyint):0); |
| | 80 | } |
| | 81 | |
| | 82 | inline static char* get_str_from_dict(PyObject* dict, char* key) { |
| | 83 | PyObject* str=PyMapping_GetItemString(dict, key); |
| | 84 | return(str?PyString_AsString(str):NULL); |
| | 85 | } |
| | 86 | |
| | 87 | inline static double get_double_from_dict(PyObject* dict, char* key) { |
| | 88 | PyObject* pyfloat=PyMapping_GetItemString(dict, key); |
| | 89 | return(pyfloat?PyFloat_AsDouble(pyfloat):0.0); |
| | 90 | } |
| | 91 | |
| | 92 | /* may raise an exception */ |
| | 93 | static void dict_to_farsight_transport_info(PyObject* dict, FarsightTransportInfo* fti) { |
| | 94 | if (!dict) return; |
| | 95 | |
| | 96 | /* required */ |
| | 97 | fti->candidate_id = get_str_from_dict(dict, "candidate_id"); |
| | 98 | fti->component = get_long_from_dict(dict, "component"); |
| | 99 | fti->ip = get_str_from_dict(dict, "ip"); |
| | 100 | fti->port = get_long_from_dict(dict, "port"); |
| | 101 | fti->proto = get_long_from_dict(dict, "proto"); |
| | 102 | fti->proto_subtype = get_str_from_dict(dict, "proto_subtype"); |
| | 103 | fti->proto_profile = get_str_from_dict(dict, "proto_profile"); |
| | 104 | fti->preference = get_double_from_dict(dict, "preference"); |
| | 105 | fti->type = get_long_from_dict(dict, "type"); |
| | 106 | |
| | 107 | if (PyError_Occurred()) return; |
| | 108 | |
| | 109 | /* optional */ |
| | 110 | fti->username = get_str_from_dict(dict, "username"); |
| | 111 | fti->password = get_str_from_dict(dict, "password"); |
| | 112 | |
| | 113 | PyError_Clear(); |
| | 114 | } |
| | 115 | |
| | 116 | /* GArray must be freed if not NULL; |
| | 117 | may raise an exception */ |
| | 118 | static void dict_to_farsight_codec(PyObject* dict, FarsightCodec* fc, GArray** fcp) { |
| | 119 | GArray* array=*fcp; |
| | 120 | |
| | 121 | if (!dict) return; |
| | 122 | |
| | 123 | /* required data */ |
| | 124 | fc->id = get_long_from_dict(dict, "id"); |
| | 125 | fc->encoding_name = get_str_from_dict(dict, "encoding_name"); |
| | 126 | |
| | 127 | if (PyError_Occured()) return; |
| | 128 | |
| | 129 | /* optional data */ |
| | 130 | fc->media_type = get_long_from_dict(dict, "media_type"); |
| | 131 | fc->clock_rate = get_long_from_dict(dict, "clock_rate"); |
| | 132 | fc->channels = get_long_from_dict(dict, "channels"); |
| | 133 | |
| | 134 | if (PyMapping_HasKeyString(dict, "params")) { |
| | 135 | PyObject* params = PyMapping_GetItemString(dict, "params"); |
| | 136 | if (PyDict_Check(params)) { |
| | 137 | PyObject *key, *value; |
| | 138 | int pos=0; |
| | 139 | GList* list=NULL; |
| | 140 | int i=0; |
| | 141 | |
| | 142 | if (!array) |
| | 143 | array = g_array_new(FALSE, FALSE, sizeof(FarsightCodecParameter)); |
| | 144 | |
| | 145 | while (PyDict_Next(params, &pos, &key, &value)) { |
| | 146 | if (PyString_Check(key) && PyString_Check(value)) { |
| | 147 | FarsightCodecParameter fcp; |
| | 148 | fcp.name = PyString_AsString(key); |
| | 149 | fcp.value= PyString_AsString(value); |
| | 150 | g_array_append_val(array, fcp); |
| | 151 | list=g_list_prepend(list, |
| | 152 | &g_array_index(array, FarsightCodecParameter, i++)); |
| | 153 | } else { |
| | 154 | /* this is not a string? not good... */ |
| | 155 | puts("keys and values must be strings here!"); |
| | 156 | } |
| | 157 | } |
| | 158 | |
| | 159 | fc->optional_params = list; |
| | 160 | } else { |
| | 161 | /* this is not a dictionary? fail miserably... */ |
| | 162 | puts("params must be a dictionary!"); |
| | 163 | } |
| | 164 | } |
| | 165 | |
| | 166 | PyErr_Clear(); |
| | 167 | |
| | 168 | *fcp = array; |
| | 169 | } |
| | 170 | |
| 103 | | data = list->data; |
| 104 | | |
| 105 | | ret = PyDict_New(); |
| 106 | | |
| 107 | | PyDict_SetItemString(ret, "candidate_id", item=PyString_FromString(data->candidate_id)); |
| 108 | | Py_DECREF(item); |
| 109 | | |
| 110 | | PyDict_SetItemString(ret, "component", item=PyInt_FromLong(data->component)); |
| 111 | | Py_DECREF(item); |
| 112 | | |
| 113 | | PyDict_SetItemString(ret, "ip", item=PyString_FromString(data->ip)); |
| 114 | | Py_DECREF(item); |
| 115 | | |
| 116 | | PyDict_SetItemString(ret, "port", item=PyInt_FromLong(data->port)); |
| 117 | | Py_DECREF(item); |
| 118 | | |
| 119 | | PyDict_SetItemString(ret, "proto", item=PyInt_FromLong(data->proto)); |
| 120 | | Py_DECREF(item); |
| 121 | | |
| 122 | | PyDict_SetItemString(ret, "proto_subtype", item=PyString_FromString(data->proto_subtype)); |
| 123 | | Py_DECREF(item); |
| 124 | | |
| 125 | | PyDict_SetItemString(ret, "proto_profile", item=PyString_FromString(data->proto_profile)); |
| 126 | | Py_DECREF(item); |
| 127 | | |
| 128 | | PyDict_SetItemString(ret, "preference", item=PyFloat_FromDouble(data->preference)); |
| 129 | | Py_DECREF(item); |
| 130 | | |
| 131 | | PyDict_SetItemString(ret, "type", item=PyInt_FromLong(data->type)); |
| 132 | | Py_DECREF(item); |
| 133 | | |
| 134 | | PyDict_SetItemString(ret, "username", item=PyString_FromString(data->username)); |
| 135 | | Py_DECREF(item); |
| 136 | | |
| 137 | | PyDict_SetItemString(ret, "password", item=PyString_FromString(data->password)); |
| 138 | | Py_DECREF(item); |
| | 240 | |
| | 241 | ret = PyList_New(0); |
| | 242 | for(tmp=list;list;list=g_list_next(list)) { |
| | 243 | FarsightTransportInfo *fti = tmp->data; |
| | 244 | PyObject *item = farsight_transport_info_to_dict(fti); |
| | 245 | |
| | 246 | PyList_Append(ret, item); |
| | 247 | Py_DECREF(item); |
| | 248 | } |