/libs/esl/src/esl_oop.cpp

https://github.com/ppanhoto/Freeswitch-mod_mp4 · C++ · 465 lines · 354 code · 108 blank · 3 comment · 71 complexity · a8f3de1ab6de6163d59ef400aeaccbfe MD5 · raw file

  1. #include <esl.h>
  2. #include <esl_oop.h>
  3. #define connection_construct_common() memset(&handle, 0, sizeof(handle))
  4. #define event_construct_common() event = NULL; serialized_string = NULL; mine = 0; hp = NULL
  5. void eslSetLogLevel(int level)
  6. {
  7. esl_global_set_default_logger(level);
  8. }
  9. ESLconnection::ESLconnection(const char *host, const char *port, const char *password)
  10. {
  11. connection_construct_common();
  12. int x_port = atoi(port);
  13. esl_connect(&handle, host, x_port, NULL, password);
  14. }
  15. ESLconnection::ESLconnection(const char *host, const char *port, const char *user, const char *password)
  16. {
  17. connection_construct_common();
  18. int x_port = atoi(port);
  19. esl_connect(&handle, host, x_port, user, password);
  20. }
  21. ESLconnection::ESLconnection(int socket)
  22. {
  23. connection_construct_common();
  24. esl_attach_handle(&handle, (esl_socket_t)socket, NULL);
  25. }
  26. ESLconnection::~ESLconnection()
  27. {
  28. if (handle.connected) {
  29. esl_disconnect(&handle);
  30. }
  31. }
  32. int ESLconnection::socketDescriptor()
  33. {
  34. if (handle.connected) {
  35. return (int) handle.sock;
  36. }
  37. return -1;
  38. }
  39. int ESLconnection::disconnect()
  40. {
  41. if (handle.connected) {
  42. return esl_disconnect(&handle);
  43. }
  44. return 0;
  45. }
  46. int ESLconnection::connected()
  47. {
  48. return handle.connected;
  49. }
  50. int ESLconnection::send(const char *cmd)
  51. {
  52. return esl_send(&handle, cmd);
  53. }
  54. ESLevent *ESLconnection::sendRecv(const char *cmd)
  55. {
  56. if (esl_send_recv(&handle, cmd) == ESL_SUCCESS) {
  57. esl_event_t *event;
  58. esl_event_dup(&event, handle.last_sr_event);
  59. return new ESLevent(event, 1);
  60. }
  61. return NULL;
  62. }
  63. ESLevent *ESLconnection::api(const char *cmd, const char *arg)
  64. {
  65. size_t len;
  66. char *cmd_buf;
  67. ESLevent *event;
  68. if (!cmd) {
  69. return NULL;
  70. }
  71. len = strlen(cmd) + (arg ? strlen(arg) : 0) + 10;
  72. cmd_buf = (char *) malloc(len + 1);
  73. assert(cmd_buf);
  74. snprintf(cmd_buf, len, "api %s %s", cmd, arg ? arg : "");
  75. *(cmd_buf + (len)) = '\0';
  76. event = sendRecv(cmd_buf);
  77. free(cmd_buf);
  78. return event;
  79. }
  80. ESLevent *ESLconnection::bgapi(const char *cmd, const char *arg)
  81. {
  82. size_t len;
  83. char *cmd_buf;
  84. ESLevent *event;
  85. if (!cmd) {
  86. return NULL;
  87. }
  88. len = strlen(cmd) + (arg ? strlen(arg) : 0) + 10;
  89. cmd_buf = (char *) malloc(len + 1);
  90. assert(cmd_buf);
  91. snprintf(cmd_buf, len, "bgapi %s %s", cmd, arg ? arg : "");
  92. *(cmd_buf + (len)) = '\0';
  93. event = sendRecv(cmd_buf);
  94. free(cmd_buf);
  95. return event;
  96. }
  97. ESLevent *ESLconnection::getInfo()
  98. {
  99. if (handle.connected && handle.info_event) {
  100. esl_event_t *event;
  101. esl_event_dup(&event, handle.info_event);
  102. return new ESLevent(event, 1);
  103. }
  104. return NULL;
  105. }
  106. int ESLconnection::setAsyncExecute(const char *val)
  107. {
  108. if (val) {
  109. handle.async_execute = esl_true(val);
  110. }
  111. return handle.async_execute;
  112. }
  113. int ESLconnection::setEventLock(const char *val)
  114. {
  115. if (val) {
  116. handle.event_lock = esl_true(val);
  117. }
  118. return handle.event_lock;
  119. }
  120. ESLevent *ESLconnection::execute(const char *app, const char *arg, const char *uuid)
  121. {
  122. if (esl_execute(&handle, app, arg, uuid) == ESL_SUCCESS) {
  123. esl_event_t *event;
  124. esl_event_dup(&event, handle.last_sr_event);
  125. return new ESLevent(event, 1);
  126. }
  127. return NULL;
  128. }
  129. ESLevent *ESLconnection::executeAsync(const char *app, const char *arg, const char *uuid)
  130. {
  131. int async = handle.async_execute;
  132. int r;
  133. handle.async_execute = 1;
  134. r = esl_execute(&handle, app, arg, uuid);
  135. handle.async_execute = async;
  136. if (r == ESL_SUCCESS) {
  137. esl_event_t *event;
  138. esl_event_dup(&event, handle.last_sr_event);
  139. return new ESLevent(event, 1);
  140. }
  141. return NULL;
  142. }
  143. ESLevent *ESLconnection::sendEvent(ESLevent *send_me)
  144. {
  145. if (esl_sendevent(&handle, send_me->event) == ESL_SUCCESS) {
  146. esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
  147. if (e) {
  148. esl_event_t *event;
  149. esl_event_dup(&event, e);
  150. return new ESLevent(event, 1);
  151. }
  152. }
  153. return new ESLevent("server_disconnected");
  154. }
  155. ESLevent *ESLconnection::recvEvent()
  156. {
  157. if (esl_recv_event(&handle, 1, NULL) == ESL_SUCCESS) {
  158. esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
  159. if (e) {
  160. esl_event_t *event;
  161. esl_event_dup(&event, e);
  162. return new ESLevent(event, 1);
  163. }
  164. }
  165. return new ESLevent("server_disconnected");
  166. }
  167. ESLevent *ESLconnection::recvEventTimed(int ms)
  168. {
  169. if (esl_recv_event_timed(&handle, ms, 1, NULL) == ESL_SUCCESS) {
  170. esl_event_t *e = handle.last_ievent ? handle.last_ievent : handle.last_event;
  171. if (e) {
  172. esl_event_t *event;
  173. esl_event_dup(&event, e);
  174. return new ESLevent(event, 1);
  175. }
  176. }
  177. return NULL;
  178. }
  179. ESLevent *ESLconnection::filter(const char *header, const char *value)
  180. {
  181. esl_status_t status = esl_filter(&handle, header, value);
  182. if (status == ESL_SUCCESS && handle.last_sr_event) {
  183. esl_event_t *event;
  184. esl_event_dup(&event, handle.last_sr_event);
  185. return new ESLevent(event, 1);
  186. }
  187. return NULL;
  188. }
  189. int ESLconnection::events(const char *etype, const char *value)
  190. {
  191. esl_event_type_t type_id = ESL_EVENT_TYPE_PLAIN;
  192. if (!strcmp(etype, "xml")) {
  193. type_id = ESL_EVENT_TYPE_XML;
  194. } else if (!strcmp(etype, "json")) {
  195. type_id = ESL_EVENT_TYPE_JSON;
  196. }
  197. return esl_events(&handle, type_id, value);
  198. }
  199. // ESLevent
  200. ///////////////////////////////////////////////////////////////////////
  201. ESLevent::ESLevent(const char *type, const char *subclass_name)
  202. {
  203. esl_event_types_t event_id;
  204. event_construct_common();
  205. if (!strcasecmp(type, "json") && !esl_strlen_zero(subclass_name)) {
  206. if (esl_event_create_json(&event, subclass_name) != ESL_SUCCESS) {
  207. return;
  208. }
  209. event_id = event->event_id;
  210. } else {
  211. if (esl_name_event(type, &event_id) != ESL_SUCCESS) {
  212. event_id = ESL_EVENT_MESSAGE;
  213. }
  214. if (!esl_strlen_zero(subclass_name) && event_id != ESL_EVENT_CUSTOM) {
  215. esl_log(ESL_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
  216. event_id = ESL_EVENT_CUSTOM;
  217. }
  218. if (esl_event_create_subclass(&event, event_id, subclass_name) != ESL_SUCCESS) {
  219. esl_log(ESL_LOG_ERROR, "Failed to create event!\n");
  220. event = NULL;
  221. }
  222. }
  223. serialized_string = NULL;
  224. mine = 1;
  225. }
  226. ESLevent::ESLevent(esl_event_t *wrap_me, int free_me)
  227. {
  228. event_construct_common();
  229. event = wrap_me;
  230. mine = free_me;
  231. serialized_string = NULL;
  232. }
  233. ESLevent::ESLevent(ESLevent *me)
  234. {
  235. /* workaround for silly php thing */
  236. event = me->event;
  237. mine = me->mine;
  238. serialized_string = NULL;
  239. me->event = NULL;
  240. me->mine = 0;
  241. esl_safe_free(me->serialized_string);
  242. }
  243. ESLevent::~ESLevent()
  244. {
  245. if (serialized_string) {
  246. free(serialized_string);
  247. }
  248. if (event && mine) {
  249. esl_event_destroy(&event);
  250. }
  251. }
  252. const char *ESLevent::nextHeader(void)
  253. {
  254. const char *name = NULL;
  255. if (hp) {
  256. name = hp->name;
  257. hp = hp->next;
  258. }
  259. return name;
  260. }
  261. const char *ESLevent::firstHeader(void)
  262. {
  263. if (event) {
  264. hp = event->headers;
  265. }
  266. return nextHeader();
  267. }
  268. const char *ESLevent::serialize(const char *format)
  269. {
  270. this_check("");
  271. esl_safe_free(serialized_string);
  272. if (format == NULL) {
  273. format = "text";
  274. }
  275. if (!event) {
  276. return "";
  277. }
  278. if (format && !strcasecmp(format, "json")) {
  279. esl_event_serialize_json(event, &serialized_string);
  280. return serialized_string;
  281. }
  282. if (esl_event_serialize(event, &serialized_string, ESL_TRUE) == ESL_SUCCESS) {
  283. return serialized_string;
  284. }
  285. return "";
  286. }
  287. bool ESLevent::setPriority(esl_priority_t priority)
  288. {
  289. this_check(false);
  290. if (event) {
  291. esl_event_set_priority(event, priority);
  292. return true;
  293. } else {
  294. esl_log(ESL_LOG_ERROR, "Trying to setPriority an event that does not exist!\n");
  295. }
  296. return false;
  297. }
  298. const char *ESLevent::getHeader(const char *header_name)
  299. {
  300. this_check("");
  301. if (event) {
  302. return esl_event_get_header(event, header_name);
  303. } else {
  304. esl_log(ESL_LOG_ERROR, "Trying to getHeader an event that does not exist!\n");
  305. }
  306. return NULL;
  307. }
  308. bool ESLevent::addHeader(const char *header_name, const char *value)
  309. {
  310. this_check(false);
  311. if (event) {
  312. return esl_event_add_header_string(event, ESL_STACK_BOTTOM, header_name, value) == ESL_SUCCESS ? true : false;
  313. } else {
  314. esl_log(ESL_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
  315. }
  316. return false;
  317. }
  318. bool ESLevent::delHeader(const char *header_name)
  319. {
  320. this_check(false);
  321. if (event) {
  322. return esl_event_del_header(event, header_name) == ESL_SUCCESS ? true : false;
  323. } else {
  324. esl_log(ESL_LOG_ERROR, "Trying to delHeader an event that does not exist!\n");
  325. }
  326. return false;
  327. }
  328. bool ESLevent::addBody(const char *value)
  329. {
  330. this_check(false);
  331. if (event) {
  332. return esl_event_add_body(event, "%s", value) == ESL_SUCCESS ? true : false;
  333. } else {
  334. esl_log(ESL_LOG_ERROR, "Trying to addBody an event that does not exist!\n");
  335. }
  336. return false;
  337. }
  338. char *ESLevent::getBody(void)
  339. {
  340. this_check((char *)"");
  341. if (event) {
  342. return esl_event_get_body(event);
  343. } else {
  344. esl_log(ESL_LOG_ERROR, "Trying to getBody an event that does not exist!\n");
  345. }
  346. return NULL;
  347. }
  348. const char *ESLevent::getType(void)
  349. {
  350. this_check("");
  351. if (event) {
  352. return esl_event_name(event->event_id);
  353. } else {
  354. esl_log(ESL_LOG_ERROR, "Trying to getType an event that does not exist!\n");
  355. }
  356. return (char *) "invalid";
  357. }