testserver_fork.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. if (fork()) {
  11. return;
  12. }
  13. if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS) {
  14. return;
  15. }
  16. esl_log(ESL_LOG_INFO, "Connected! %d\n", handle.sock);
  17. esl_filter(&handle, "unique-id", esl_event_get_header(handle.info_event, "caller-unique-id"));
  18. esl_events(&handle, ESL_EVENT_TYPE_PLAIN, "SESSION_HEARTBEAT CHANNEL_ANSWER CHANNEL_ORIGINATE CHANNEL_PROGRESS CHANNEL_HANGUP "
  19. "CHANNEL_BRIDGE CHANNEL_UNBRIDGE CHANNEL_OUTGOING CHANNEL_EXECUTE CHANNEL_EXECUTE_COMPLETE DTMF CUSTOM conference::maintenance");
  20. esl_send_recv(&handle, "linger");
  21. esl_execute(&handle, "answer", NULL, NULL);
  22. esl_execute(&handle, "conference", "3000@default", NULL);
  23. while((status = esl_recv_timed(&handle, 1000)) != ESL_FAIL) {
  24. if (done) {
  25. if (time(NULL) >= exp) {
  26. break;
  27. }
  28. } else if (status == ESL_SUCCESS) {
  29. const char *type = esl_event_get_header(handle.last_event, "content-type");
  30. if (type && !strcasecmp(type, "text/disconnect-notice")) {
  31. const char *dispo = esl_event_get_header(handle.last_event, "content-disposition");
  32. esl_log(ESL_LOG_INFO, "Got a disconnection notice dispostion: [%s]\n", dispo ? dispo : "");
  33. if (dispo && !strcmp(dispo, "linger")) {
  34. done = 1;
  35. esl_log(ESL_LOG_INFO, "Waiting 5 seconds for any remaining events.\n");
  36. exp = time(NULL) + 5;
  37. }
  38. }
  39. }
  40. }
  41. esl_log(ESL_LOG_INFO, "Disconnected! %d\n", handle.sock);
  42. esl_disconnect(&handle);
  43. }
  44. static esl_socket_t server_sock = ESL_SOCK_INVALID;
  45. static void handle_sig(int sig)
  46. {
  47. shutdown(server_sock, 2);
  48. }
  49. int main(void)
  50. {
  51. signal(SIGINT, handle_sig);
  52. signal(SIGCHLD, SIG_IGN);
  53. esl_global_set_default_logger(7);
  54. esl_listen("localhost", 8040, mycallback, NULL, &server_sock);
  55. return 0;
  56. }