esl_oop.cpp 11 KB

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