2
0

server2.pl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. require ESL;
  2. use IO::Socket::INET;
  3. #ESL::eslSetLogLevel(7);
  4. my $ip = "127.0.0.1";
  5. my $sock = new IO::Socket::INET ( LocalHost => $ip, LocalPort => '8040', Proto => 'tcp', Listen => 1, Reuse => 1 );
  6. die "Could not create socket: $!\n" unless $sock;
  7. for(;;) {
  8. my $new_sock = $sock->accept();
  9. my $pid = fork();
  10. if ($pid) {
  11. close($new_sock);
  12. next;
  13. }
  14. my $host = $new_sock->sockhost();
  15. my $fd = fileno($new_sock);
  16. my $con = new ESL::ESLconnection($fd);
  17. my $info = $con->getInfo();
  18. #print $info->serialize();
  19. my $uuid = $info->getHeader("unique-id");
  20. printf "Connected call %s, from %s\n", $uuid, $info->getHeader("caller-caller-id-number");
  21. $e = $con->filter("unique-id", $uuid);
  22. if ($e) {
  23. print $e->serialize();
  24. } else {
  25. printf("WTF?\n");
  26. }
  27. $con->events("plain", "all");
  28. #$con->sendRecv("myevents");
  29. $con->execute("answer");
  30. $con->execute("playback", "/ram/swimp.raw");
  31. while($con->connected()) {
  32. #my $e = $con->recvEventTimed(100);
  33. my $e = $con->recvEvent();
  34. if ($e) {
  35. my $name = $e->getHeader("event-name");
  36. print "EVENT [$name]\n";
  37. if ($name eq "DTMF") {
  38. my $digit = $e->getHeader("dtmf-digit");
  39. my $duration = $e->getHeader("dtmf-duration");
  40. print "DTMF digit $digit ($duration)\n";
  41. }
  42. }
  43. }
  44. print "BYE\n";
  45. close($new_sock);
  46. }