testserver.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <esl.h>
  4. static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
  5. {
  6. esl_handle_t handle = {{0}};
  7. int done = 0;
  8. esl_status_t status;
  9. time_t exp = 0;
  10. esl_attach_handle(&handle, client_sock, addr);
  11. esl_log(ESL_LOG_INFO, "Connected! %d\n", handle.sock);
  12. esl_filter(&handle, "unique-id", esl_event_get_header(handle.info_event, "caller-unique-id"));
  13. esl_events(&handle, ESL_EVENT_TYPE_PLAIN, "SESSION_HEARTBEAT CHANNEL_ANSWER CHANNEL_ORIGINATE CHANNEL_PROGRESS CHANNEL_HANGUP "
  14. "CHANNEL_BRIDGE CHANNEL_UNBRIDGE CHANNEL_OUTGOING CHANNEL_EXECUTE CHANNEL_EXECUTE_COMPLETE DTMF CUSTOM conference::maintenance");
  15. esl_send_recv(&handle, "linger");
  16. esl_execute(&handle, "answer", NULL, NULL);
  17. esl_execute(&handle, "conference", "3000@default", NULL);
  18. while((status = esl_recv_timed(&handle, 1000)) != ESL_FAIL) {
  19. if (done) {
  20. if (time(NULL) >= exp) {
  21. break;
  22. }
  23. } else if (status == ESL_SUCCESS) {
  24. const char *type = esl_event_get_header(handle.last_event, "content-type");
  25. if (type && !strcasecmp(type, "text/disconnect-notice")) {
  26. const char *dispo = esl_event_get_header(handle.last_event, "content-disposition");
  27. esl_log(ESL_LOG_INFO, "Got a disconnection notice dispostion: [%s]\n", dispo ? dispo : "");
  28. if (dispo && !strcmp(dispo, "linger")) {
  29. done = 1;
  30. esl_log(ESL_LOG_INFO, "Waiting 5 seconds for any remaining events.\n");
  31. exp = time(NULL) + 5;
  32. }
  33. }
  34. }
  35. }
  36. esl_log(ESL_LOG_INFO, "Disconnected! %d\n", handle.sock);
  37. esl_disconnect(&handle);
  38. }
  39. int main(void)
  40. {
  41. esl_global_set_default_logger(7);
  42. esl_listen_threaded("localhost", 8040, mycallback, NULL, 100000);
  43. return 0;
  44. }