gw.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:23k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. /* ====================================================================
  2.  * The Vovida Software License, Version 1.0 
  3.  * 
  4.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in
  15.  *    the documentation and/or other materials provided with the
  16.  *    distribution.
  17.  * 
  18.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  19.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  20.  *    not be used to endorse or promote products derived from this
  21.  *    software without prior written permission. For written
  22.  *    permission, please contact vocal@vovida.org.
  23.  *
  24.  * 4. Products derived from this software may not be called "VOCAL", nor
  25.  *    may "VOCAL" appear in their name, without prior written
  26.  *    permission of Vovida Networks, Inc.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  29.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  31.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  32.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  33.  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  34.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  37.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40.  * DAMAGE.
  41.  * 
  42.  * ====================================================================
  43.  * 
  44.  * This software consists of voluntary contributions made by Vovida
  45.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  46.  * Inc.  For more information on Vovida Networks, Inc., please see
  47.  * <http://www.vovida.org/>.
  48.  *
  49.  */
  50. /**********************************************************************
  51.  
  52. $Id: gw.cxx,v 1.4 2001/05/13 10:54:01 icahoon Exp $
  53.  
  54. **********************************************************************/
  55. // sample gateway code.
  56. #include <signal.h>
  57. #include <sys/time.h>
  58. #include <sys/types.h>
  59. #include <unistd.h>
  60. //#include "mgcpCoding.hxx"
  61. #include "mg.hxx"
  62. #include <queue>
  63. #include "nullHw.hxx"
  64. #include "tpjackHw.hxx"
  65. #include <typeinfo>
  66. #include <map>
  67. /**********************************************************************
  68.  
  69. Data structures
  70.  
  71. **********************************************************************/
  72. class StateMachine;
  73. class StateEvent;
  74. typedef void (*StateHandler)(StateEvent* event, StateMachine* state);
  75. void stateUninitialized(StateEvent* event, StateMachine* state);
  76. void waitForResponse(StateEvent* event, StateMachine* state);
  77. void stateIdle(StateEvent* event, StateMachine* state);
  78. void stateRingingIn(StateEvent* event, StateMachine* state);
  79. void stateRingingOut(StateEvent* event, StateMachine* state);
  80. void stateHalfOpenIn(StateEvent* event, StateMachine* state);
  81. void stateHalfOpen(StateEvent* event, StateMachine* state);
  82. void stateActiveNoNotifications(StateEvent* event, StateMachine* state);
  83. void stateActive(StateEvent* event, StateMachine* state);
  84. void stateWaitForClose(StateEvent* event, StateMachine* state);
  85. void stateWaitOnHook(StateEvent* event, StateMachine* state);
  86. enum StateEventStimulus
  87. {
  88.     StimulusNULL,
  89.     StimulusNewMessage,
  90.     StimulusResponse,
  91.     StimulusOffHook,
  92.     StimulusOnHook,
  93. };
  94. class StateMachine
  95. {
  96.     public:
  97.         StateMachine()
  98.         {}
  99.         ;
  100.         // yes, this is gross
  101.         RequestId CurrentRequestId;
  102.         CallId callID;
  103.         ConnectionMode callMode;
  104.         MgcpTransmitter* callAgent;
  105.         UdpReceiver* server;
  106.         LocalConnectionDescriptor localDescriptor;
  107.         RemoteConnectionDescriptor RemoteDescriptor;
  108.         HardwareObject* hardware;
  109.         map < StateEventStimulus, Event* > activeEventList;
  110.         setState(StateHandler current, StateHandler pending)
  111.         {
  112.             char* string = "";
  113.             if (current == stateUninitialized)
  114.                 string = "stateUninitialized";
  115.             if (current == waitForResponse)
  116.                 string = "waitForResponse";
  117.             if (current == stateIdle)
  118.                 string = "stateIdle";
  119.             if (current == stateRingingOut)
  120.                 string = "stateRingingOut";
  121.             if (current == stateHalfOpen)
  122.                 string = "stateHalfOpen";
  123.             if (current == stateActiveNoNotifications)
  124.                 string = "stateActiveNoNotifications";
  125.             if (current == stateActive)
  126.                 string = "stateActive";
  127.             cout << "State changed to: " << string << "n";
  128.             currentState = current;
  129.             pendingState = pending;
  130.         };
  131.         StateHandler currentState;
  132.         StateHandler pendingState;
  133. };
  134. class StateEvent
  135. {
  136.     public:
  137.         StateEvent() : stimulus(StimulusNULL)
  138.         {}
  139.         StateEventStimulus getStimulusType()
  140.         {
  141.             return stimulus;
  142.         }
  143.         setStimulusType(StateEventStimulus x)
  144.         {
  145.             stimulus = x;
  146.         }
  147.         setMessage(MgcpCommand* msg)
  148.         {
  149.             newMessage = msg;
  150.             if (dynamic_cast < MgcpResponse* > (msg))
  151.             {
  152.                 stimulus = StimulusResponse;
  153.             }
  154.             else
  155.             {
  156.                 stimulus = StimulusNewMessage;
  157.             }
  158.         }
  159.         MgcpCommand* getMessage()
  160.         {
  161.             return newMessage;
  162.         }
  163.         void setStimulus(StateEventStimulus stim)
  164.         {
  165.             stimulus = stim;
  166.         }
  167.         StateEventStimulus getStimulus()
  168.         {
  169.             return stimulus;
  170.         }
  171.     private:
  172.         StateEventStimulus stimulus;
  173.         MgcpCommand* newMessage;
  174. };
  175. char* getHost()
  176. {
  177.     static char buf[256];
  178.     gethostname(buf, 256);
  179.     return buf;
  180. }
  181. void wrongMessage()
  182. {
  183.     cerr << "received invalid messagen";
  184.     int parentpid = getpid();
  185.     // parent
  186.     kill(parentpid, SIGSTOP);
  187.     exit(1);
  188. }
  189. void fatalError(char* errorMsg)
  190. {
  191.     cerr << errorMsg << "n";
  192.     exit(1);
  193. }
  194. /**********************************************************************
  195.  
  196. Globals
  197.  
  198. **********************************************************************/
  199. EndpointId set("testID@localhost:5050");
  200. void stateUninitialized(StateEvent* event, StateMachine* state)
  201. {
  202.     if (event->getStimulusType() == StimulusNewMessage)
  203.     {
  204.         MgcpCommand* test_item = event->getMessage();
  205.         //    MgcpCommand* test_item = parseMessage(data->getMessageLocation(), 0);
  206.         // state 3 -- receive notification request
  207.         NotificationRequest* nrPtr;
  208.         nrPtr = dynamic_cast < NotificationRequest* > (test_item);
  209.         if (nrPtr == NULL)
  210.             wrongMessage();
  211.         // check for the right parameters
  212.         state->CurrentRequestId = *(nrPtr->getRequestId());
  213.         vector < Event* > * eventList((nrPtr->getRequestedEvents())->getVector());
  214.         if (!eventList)
  215.             wrongMessage();
  216.         // get the appropriate events --
  217.         vector < Event* > ::iterator eventIter;
  218.         eventIter = eventList->begin();
  219.         while (eventIter != eventList->end())
  220.         {
  221.             if (dynamic_cast < LineEventOffHook* > (*eventIter) == NULL)
  222.                 wrongMessage();  // only accept this event sir
  223.             ++eventIter;
  224.         }
  225.         // reply OK.
  226.         MgcpResponse response00(200, nrPtr->getTransactionId(), "OK");
  227.         response00.send(*(state->callAgent));
  228.         state->setState(stateIdle, NULL);
  229.     }
  230.     // ignore other messages
  231. }
  232. void waitForResponse(StateEvent* event, StateMachine* state)
  233. {
  234.     if (state->pendingState == NULL)
  235.     {
  236.         fatalError("illegal state machine");
  237.     }
  238.     if (event->getStimulusType() == StimulusResponse)
  239.     {
  240.         //
  241.         state->setState(state->pendingState, NULL);
  242.     }
  243.     else
  244.     {
  245.         // this is an event which this state must handle somehow or another.
  246.         state->pendingState(event, state);
  247.     }
  248. }
  249. void stateIdle(StateEvent* event, StateMachine* state)
  250. {
  251.     if (event->getStimulusType() == StimulusOffHook)
  252.     {
  253.         Notify msg8(set);
  254.         ObservedEvents events8;
  255.         LineEventOffHook offHook;
  256.         events8.insert(offHook);
  257.         msg8.insert(&events8);
  258.         msg8.insert(&(state->CurrentRequestId));
  259.         msg8.send(*(state->callAgent));
  260.         state->setState(waitForResponse, stateRingingOut);
  261.     }
  262.     else if (event->getStimulusType() == StimulusNewMessage)
  263.     {
  264.         MgcpCommand* test_item = event->getMessage();
  265.         if (typeid(*test_item) == typeid(NotificationRequest))
  266.         {
  267.             NotificationRequest* nrPtr;
  268.             nrPtr = dynamic_cast < NotificationRequest* > (test_item);
  269.             if (nrPtr == NULL)
  270.                 wrongMessage();
  271.             // check for the right parameters
  272.             state->CurrentRequestId = *(nrPtr->getRequestId());
  273.             vector < Event* > * eventList((nrPtr->getRequestedEvents())->getVector());
  274.             if (!eventList)
  275.                 wrongMessage();
  276.             // get the appropriate events --
  277.             vector < Event* > ::iterator eventIter;
  278.             eventIter = eventList->begin();
  279.             bool lookForOffHook(false);
  280.             while (eventIter != eventList->end())
  281.             {
  282.                 if (dynamic_cast < LineEventOffHook* > (*eventIter) != NULL)
  283.                     lookForOffHook = true;
  284.                 ++eventIter;
  285.             }
  286.             if (!lookForOffHook)
  287.                 wrongMessage();
  288.             SignalRequests* signals;
  289.             signals = nrPtr->getSignalRequests();
  290.             vector < Signal* > * signalList(signals->getList());
  291.             if (!signalList)
  292.                 wrongMessage();
  293.             // get the appropriate signals --
  294.             vector < Signal* > ::iterator signalIter;
  295.             signalIter = signalList->begin();
  296.             bool sendRing(false);
  297.             bool sendRingback(false);
  298.             while (signalIter != signalList->end())
  299.             {
  300.                 if (dynamic_cast < SignalRing* > (*signalIter) != NULL)
  301.                 {
  302.                     sendRing = true;
  303.                 }
  304.                 if (dynamic_cast < SignalRingback* > (*signalIter) != NULL)
  305.                 {
  306.                     sendRingback = true;
  307.                 }
  308.                 ++signalIter;
  309.             }
  310.             if (!(sendRing && sendRingback))
  311.                 wrongMessage();
  312.             state->hardware->sendSignal(SignalRingStart);
  313.             state->hardware->sendSignal(SignalRemoteRingbackStart);
  314.             // reply OK.
  315.             MgcpResponse response00(200, nrPtr->getTransactionId(), "OK");
  316.             response00.send(*(state->callAgent));
  317.             state->CurrentRequestId = (*(nrPtr->getRequestId()));
  318.             state->setState(stateRingingIn, NULL);
  319.         }
  320.     }
  321.     else
  322.     {
  323.         wrongMessage();
  324.     }
  325. }
  326. // the set is ringing and you may get the message offhook/onhook
  327. void stateRingingIn(StateEvent* event, StateMachine* state)
  328. {
  329.     if (event->getStimulusType() == StimulusOffHook)
  330.     {
  331.         // send a notify
  332.         Notify msg8(set);
  333.         ObservedEvents events8;
  334.         LineEventOffHook offHook;
  335.         events8.insert(offHook);
  336.         //    msg8.insert(&set);
  337.         msg8.insert(&events8);
  338.         msg8.insert(&state->CurrentRequestId);
  339.         msg8.send(*(state->callAgent));
  340.         // wait for the create connection
  341.         state->setState(waitForResponse, stateHalfOpenIn);
  342.     }
  343. }
  344. void stateRingingOut(StateEvent* event, StateMachine* state)
  345. {
  346.     if (event->getStimulusType() == StimulusNewMessage)
  347.     {
  348.         MgcpCommand* test_item = event->getMessage();
  349.         // state 5 -- receive create connection
  350.         CreateConnection* ccPtr;
  351.         ccPtr = dynamic_cast < CreateConnection* > (test_item);
  352.         if (ccPtr == NULL)
  353.             wrongMessage();
  354.         // now calculate the return address information
  355.         // don't forget to save the remote side's data
  356.         // now you need to reply with some useful info.
  357.         state->callID = (*(ccPtr->getCallId()));
  358.         ConnectionMode callMode(*(ccPtr->getConnectionMode()));
  359.         if (callMode.getMode() != ConnectionSendRecv)
  360.             wrongMessage();
  361.         state->RemoteDescriptor = *(ccPtr->getRemoteConnection());
  362.         //    cout << "remote: " << *RemoteDescriptor << "n";
  363.         // send a reply with the correct SDP information
  364.         SDPDescriptor local;
  365.         local.setSessionName("test session");
  366.         local.setUserName("testID");
  367.         string host;
  368.         host = getHost();
  369.         local.setAddress(host);
  370.         local.setSessionId(128538);
  371.         local.setVersion(1000);
  372.         local.setPort(6050);
  373.         //    local.addFormat(0);
  374.         //    cout << *(local.encode()) << "n";
  375.         LocalConnectionDescriptor msg9_rd(local);
  376.         state->localDescriptor = msg9_rd;
  377.         state->setState(stateHalfOpen, NULL);
  378.         MgcpResponse response01(200, ccPtr->getTransactionId(), "OK");
  379.         response01.insert(&msg9_rd);
  380.         response01.send(*(state->callAgent));
  381.         //    free(data);
  382.         //    data = NULL;
  383.     }
  384. }
  385. void stateHalfOpenIn(StateEvent* event, StateMachine* state)
  386. {
  387.     if (event->getStimulusType() == StimulusNewMessage)
  388.     {
  389.         MgcpCommand* test_item = event->getMessage();
  390.         CreateConnection* ccPtr;
  391.         ccPtr = dynamic_cast < CreateConnection* > (test_item);
  392.         if (ccPtr == NULL)
  393.             wrongMessage();
  394.         state->callID = (*(ccPtr->getCallId()));
  395.         state->callMode = (*(ccPtr->getConnectionMode()));
  396.         if (state->callMode.getMode() != ConnectionSendRecv)
  397.             wrongMessage();
  398.         state->RemoteDescriptor = *(ccPtr->getRemoteConnection());
  399.         SDPDescriptor local;
  400.         local.setSessionName("test session");
  401.         local.setUserName("testID");
  402.         string host;
  403.         host = getHost();
  404.         local.setAddress(host);
  405.         local.setSessionId(128538);
  406.         local.setVersion(1000);
  407.         local.setPort(6060 + (rand() % 1024));
  408.         LocalConnectionDescriptor msg9_rd(local);
  409.         MgcpResponse response01(200, ccPtr->getTransactionId(), "OK");
  410.         response01.insert(&msg9_rd);
  411.         response01.send(*(state->callAgent));
  412.         state->hardware->audioStart(
  413.             local.getPort(),
  414.             state->RemoteDescriptor.getDescriptor()->getAddress()->c_str(),
  415.             state->RemoteDescriptor.getDescriptor()->getPort()
  416.         );
  417.         state->setState(stateActiveNoNotifications, NULL);
  418.     }
  419. }
  420. void stateHalfOpen(StateEvent* event, StateMachine* state)
  421. {
  422.     if (event->getStimulusType() == StimulusNewMessage)
  423.     {
  424.         MgcpCommand* test_item = event->getMessage();
  425.         // state 7 -- receive create connection
  426. #ifdef DEBUG
  427.         cout << "ttdata: " << (data->getMessageLocation()) << "n";
  428.         cout << "ttparsed: " << (*test_item) << "n";
  429. #endif
  430.         ModifyConnection* mdPtr;
  431.         mdPtr = dynamic_cast < ModifyConnection* > (test_item);
  432.         if (mdPtr == NULL)
  433.             wrongMessage();
  434.         CallId newCallID(*(mdPtr->getCallId()));
  435.         if (newCallID != state->callID)
  436.             wrongMessage();
  437.         ConnectionMode newCallMode(*(mdPtr->getConnectionMode()));
  438.         if (newCallMode.getMode() != ConnectionSendRecv)
  439.             wrongMessage();
  440.         state->RemoteDescriptor = *(mdPtr->getRemoteConnection());
  441.         // this needs to be fixed
  442.         state->hardware->audioStart(
  443.             state->localDescriptor.getDescriptor()->getPort(),
  444.             state->RemoteDescriptor.getDescriptor()->getAddress()->c_str(),
  445.             state->RemoteDescriptor.getDescriptor()->getPort()
  446.         );
  447.         MgcpResponse response02(200, mdPtr->getTransactionId(), "OK");
  448.         response02.send(*(state->callAgent));
  449.         state->setState(stateActiveNoNotifications, NULL);
  450.     }
  451. }
  452. void stateActiveNoNotifications(StateEvent* event, StateMachine* state)
  453. {
  454.     if (event->getStimulusType() == StimulusNewMessage)
  455.     {
  456.         MgcpCommand* test_item = event->getMessage();
  457.         NotificationRequest* nrPtr;
  458.         nrPtr = dynamic_cast < NotificationRequest* > (test_item);
  459.         if (nrPtr == NULL)
  460.             wrongMessage();
  461.         // check for the right parameters
  462.         state->CurrentRequestId = (*(nrPtr->getRequestId()));
  463.         vector < Event* > * eventList((nrPtr->getRequestedEvents())->getVector());
  464.         if (!eventList)
  465.             wrongMessage();
  466.         // get the appropriate events --
  467.         vector < Event* > ::iterator eventIter;
  468.         eventIter = eventList->begin();
  469.         while (eventIter != eventList->end())
  470.         {
  471.             if (dynamic_cast < LineEventOnHook* > (*eventIter) == NULL)
  472.                 wrongMessage();  // only accept this event sir
  473.             ++eventIter;
  474.         }
  475.         // reply OK.
  476.         MgcpResponse response00(200, nrPtr->getTransactionId(), "OK");
  477.         response00.send(*(state->callAgent));
  478.         state->setState(stateActive, NULL);
  479.     }
  480.     //    else if(event->getStimulusType() == StimulusNewMessage)
  481.     //    cout << "remote: " << *RemoteDescriptor << "n";
  482. }
  483. void stateActive(StateEvent* event, StateMachine* state)
  484. {
  485.     if (event->getStimulusType() == StimulusNewMessage)
  486.     {
  487.         MgcpCommand* test_item = event->getMessage();
  488.         DeleteConnection* p;
  489.         p = dynamic_cast < DeleteConnection* > (test_item);
  490.         if (p == NULL)
  491.             wrongMessage();
  492.         state->callID = (*(p->getCallId()));
  493.         ConnectionId connectId(*(p->getConnectionId()));
  494.         // ack this message
  495.         MgcpResponse response00(200, p->getTransactionId(), "OK");
  496.         response00.send(*(state->callAgent));
  497.         state->setState(stateWaitOnHook, NULL);
  498.         // close the audio channel
  499.         state->hardware->sendSignal(SignalAudioStop);
  500.     }
  501.     else if (event->getStimulusType() == StimulusOnHook)
  502.     {
  503.         Notify msg8(set);
  504.         ObservedEvents events8;
  505.         LineEventOnHook onHook;
  506.         events8.insert(onHook);
  507.         msg8.insert(&events8);
  508.         msg8.insert(&(state->CurrentRequestId));
  509.         msg8.send(*(state->callAgent));
  510.         state->setState(waitForResponse, stateWaitForClose);
  511.     }
  512. }
  513. void stateWaitForClose(StateEvent* event, StateMachine* state)
  514. {
  515.     if (event->getStimulusType() == StimulusNewMessage)
  516.     {
  517.         MgcpCommand* test_item = event->getMessage();
  518.         DeleteConnection* p;
  519.         p = dynamic_cast < DeleteConnection* > (test_item);
  520.         if (p == NULL)
  521.             wrongMessage();
  522.         state->callID = (*(p->getCallId()));
  523.         ConnectionId connectId(*(p->getConnectionId()));
  524.         // ack this message
  525.         MgcpResponse response00(200, p->getTransactionId(), "OK");
  526.         response00.send(*(state->callAgent));
  527.         state->setState(stateUninitialized, NULL);
  528.         // close the audio channel
  529.         state->hardware->sendSignal(SignalAudioStop);
  530.     }
  531. }
  532. void stateWaitOnHook(StateEvent* event, StateMachine* state)
  533. {
  534.     if (event->getStimulusType() == StimulusOnHook)
  535.     {
  536.         Notify msg8(set);
  537.         ObservedEvents events8;
  538.         LineEventOnHook onHook;
  539.         events8.insert(onHook);
  540.         msg8.insert(&events8);
  541.         msg8.insert(&(state->CurrentRequestId));
  542.         msg8.send(*(state->callAgent));
  543.         state->setState(waitForResponse, stateUninitialized);
  544.     }
  545. }
  546. void EventWatcher(StateEvent* event, StateMachine* state)
  547. {
  548.     // this watches for offhook/onhook events and handles them as
  549.     // appropriate due to the bit flag that has been set.
  550.     if (state->activeEventList[event->getStimulus()])
  551.     {
  552.         // now you need to send a copy of this one
  553.         Notify msg8(set);
  554.         ObservedEvents events8;
  555.         events8.insert(*(state->activeEventList[event->getStimulusType()]));
  556.         msg8.insert(&events8);
  557.         msg8.insert(&state->CurrentRequestId);
  558.         msg8.send(*(state->callAgent));
  559.     }
  560.     // now you need to go on to the next state, or just wait here...
  561. }
  562. int main(int argc, char* argv[])
  563. {
  564.     char* agent_host = "localhost";
  565.     string device("null");
  566.     StateMachine state;
  567.     int port = 5050;
  568.     if (argc != 4)
  569.     {
  570.         // this is an error
  571.         cerr << "usage: gw1 device agentHostname portnumbern";
  572.         cerr << "defaulting to null / localhostn";
  573.     }
  574.     else
  575.     {
  576.         device = argv[1];
  577.         agent_host = argv[2];
  578.         port = atoi(argv[3]);
  579.     }
  580.     if (device == "tpjack")
  581.     {
  582.         state.hardware = new TpjackHardwareObject("/dev/ixj0");
  583.     }
  584.     else
  585.     {
  586.         state.hardware = new NullHardwareObject("/dev/ixj0");
  587.     }
  588.     MgcpTransmitter callAgent(agent_host, 5010);
  589.     UdpReceiver server(port);   // hardcoded...
  590.     UdpPacket* data = 0;
  591.     state.callAgent = &callAgent;
  592.     state.server = &server;
  593.     state.setState(stateUninitialized, NULL);
  594.     MgcpCommand* test_item;
  595.     fd_set read_fds;
  596.     struct timeval time_val;
  597.     srand(time(NULL));
  598.     for (; ; )
  599.     {
  600.         FD_ZERO(&read_fds);
  601.         state.hardware->addToFdSet(&read_fds);
  602.         state.server->addToFdSet(&read_fds);
  603.         time_val.tv_sec = 0;
  604.         time_val.tv_usec = 300;
  605.         // instead of data, it should be stateevent
  606.         int retval = select(128, &read_fds, NULL, NULL, &time_val);
  607.         if (state.hardware->process(&read_fds))
  608.         {
  609.             //     hardware.processData(&read_fds);
  610.             // incoming/outgoing hardware interface
  611.             HardwareEvent hwEvent;
  612.             while ((hwEvent = state.hardware->getEvent()) != EventNULL)
  613.             {
  614.                 StateEvent event;
  615.                 switch (hwEvent)
  616.                 {
  617.                     case EventOffHook:
  618.                     event.setStimulus(StimulusOffHook);
  619.                     break;
  620.                     case EventOnHook:
  621.                     event.setStimulus(StimulusOnHook);
  622.                     break;
  623.                     case EventAudioFailed:
  624.                     case EventAudioClosed:
  625.                     default:
  626.                     break;
  627.                 };
  628.                 state.currentState(&event, &state);
  629.             }
  630.         }
  631.         if (state.server->checkIfSet(&read_fds))
  632.         {
  633.             // incoming control packet
  634.             data = state.server->receive();
  635.             MgcpCommand* test_item =
  636.                 parseMessage(data->getMessageLocation(), 0);
  637.             StateEvent event;
  638.             event.setMessage(test_item);
  639.             state.currentState(&event, &state);
  640.         }
  641.         if (0)
  642.         {
  643.             // incoming audio
  644.         }
  645.     }
  646.     return 0;
  647. }