📄 gw.cxx
字号:
/********************************************************************** $Id: gw.cxx,v 1.3 2000/09/20 08:01:25 cvs Exp $ Copyright 1999-2000 Vovida Networks, Inc. All Rights Reserved. $Log: gw.cxx,v $Revision 1.3 2000/09/20 08:01:25 cvsglobal format changeRevision 1.2 2000/01/11 00:55:16 bogawacopyright notice fixes for year 2000 Revision 1.1.1.1 1999/06/17 23:10:40 bogawanew MGCP builder Revision 1.1.1.2 1999/06/17 22:09:32 bogawadocification code Revision 1.1.1.1 1999/06/17 22:07:00 bogawamgcp api builder code Revision 1.4 1999/06/09 08:16:47 bogawa Add copyright notices. Revision 1.3 1999/06/09 04:09:52 bogawa This makefile now correctly does diving makes. In addition, the code has been cleaned up and adds some comments for doc++. doc++ can be invoked by using make doc -- the current version will be shippedwith appropriate html documentation. Revision 1.2 1999/06/09 01:05:02 bogawa Changes to make the new structure compile correctly, and to allow thecode to use the new hardware structure. gateway1 is now using thenew structure fully, while gateway2 is using it partially (actuallynot very much). A future version should be much cleaner. Revision 1.1.1.1 1999/06/08 18:12:25 bogawamake file system for cvs **********************************************************************/// sample gateway code.#include <signal.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>//#include "mgcpCoding.hxx"#include "mg.hxx"#include <queue>#include "nullHw.hxx"#include "tpjackHw.hxx"#include <typeinfo>#include <map>/********************************************************************** Data structures **********************************************************************/class StateMachine;class StateEvent;typedef void (*StateHandler)(StateEvent* event, StateMachine* state);void stateUninitialized(StateEvent* event, StateMachine* state);void waitForResponse(StateEvent* event, StateMachine* state);void stateIdle(StateEvent* event, StateMachine* state);void stateRingingIn(StateEvent* event, StateMachine* state);void stateRingingOut(StateEvent* event, StateMachine* state);void stateHalfOpenIn(StateEvent* event, StateMachine* state);void stateHalfOpen(StateEvent* event, StateMachine* state);void stateActiveNoNotifications(StateEvent* event, StateMachine* state);void stateActive(StateEvent* event, StateMachine* state);void stateWaitForClose(StateEvent* event, StateMachine* state);void stateWaitOnHook(StateEvent* event, StateMachine* state);enum StateEventStimulus{ StimulusNULL, StimulusNewMessage, StimulusResponse, StimulusOffHook, StimulusOnHook,};class StateMachine{ public: StateMachine() {} ; // yes, this is gross RequestId CurrentRequestId; CallId callID; ConnectionMode callMode; MgcpTransmitter* callAgent; UdpReceiver* server; LocalConnectionDescriptor localDescriptor; RemoteConnectionDescriptor RemoteDescriptor; HardwareObject* hardware; map < StateEventStimulus, Event* > activeEventList; setState(StateHandler current, StateHandler pending) { char* string = ""; if (current == stateUninitialized) string = "stateUninitialized"; if (current == waitForResponse) string = "waitForResponse"; if (current == stateIdle) string = "stateIdle"; if (current == stateRingingOut) string = "stateRingingOut"; if (current == stateHalfOpen) string = "stateHalfOpen"; if (current == stateActiveNoNotifications) string = "stateActiveNoNotifications"; if (current == stateActive) string = "stateActive"; cout << "State changed to: " << string << "\n"; currentState = current; pendingState = pending; }; StateHandler currentState; StateHandler pendingState;};class StateEvent{ public: StateEvent() : stimulus(StimulusNULL) {} StateEventStimulus getStimulusType() { return stimulus; } setStimulusType(StateEventStimulus x) { stimulus = x; } setMessage(MgcpCommand* msg) { newMessage = msg; if (dynamic_cast < MgcpResponse* > (msg)) { stimulus = StimulusResponse; } else { stimulus = StimulusNewMessage; } } MgcpCommand* getMessage() { return newMessage; } void setStimulus(StateEventStimulus stim) { stimulus = stim; } StateEventStimulus getStimulus() { return stimulus; } private: StateEventStimulus stimulus; MgcpCommand* newMessage;};char* getHost(){ static char buf[256]; gethostname(buf, 256); return buf;}void wrongMessage(){ cerr << "received invalid message\n"; int parentpid = getpid(); // parent kill(parentpid, SIGSTOP); exit(1);}void fatalError(char* errorMsg){ cerr << errorMsg << "\n"; exit(1);}/********************************************************************** Globals **********************************************************************/EndpointId set("testID@localhost:5050");void stateUninitialized(StateEvent* event, StateMachine* state){ if (event->getStimulusType() == StimulusNewMessage) { MgcpCommand* test_item = event->getMessage(); // MgcpCommand* test_item = parseMessage(data->getMessageLocation(), 0); // state 3 -- receive notification request NotificationRequest* nrPtr; nrPtr = dynamic_cast < NotificationRequest* > (test_item); if (nrPtr == NULL) wrongMessage(); // check for the right parameters state->CurrentRequestId = *(nrPtr->getRequestId()); vector < Event* > * eventList((nrPtr->getRequestedEvents())->getVector()); if (!eventList) wrongMessage(); // get the appropriate events -- vector < Event* > ::iterator eventIter; eventIter = eventList->begin(); while (eventIter != eventList->end()) { if (dynamic_cast < LineEventOffHook* > (*eventIter) == NULL) wrongMessage(); // only accept this event sir ++eventIter; } // reply OK. MgcpResponse response00(200, nrPtr->getTransactionId(), "OK"); response00.send(*(state->callAgent)); state->setState(stateIdle, NULL); } // ignore other messages}void waitForResponse(StateEvent* event, StateMachine* state){ if (state->pendingState == NULL) { fatalError("illegal state machine"); } if (event->getStimulusType() == StimulusResponse) { // state->setState(state->pendingState, NULL); } else { // this is an event which this state must handle somehow or another. state->pendingState(event, state); }}void stateIdle(StateEvent* event, StateMachine* state){ if (event->getStimulusType() == StimulusOffHook) { Notify msg8(set); ObservedEvents events8; LineEventOffHook offHook; events8.insert(offHook); msg8.insert(&events8); msg8.insert(&(state->CurrentRequestId)); msg8.send(*(state->callAgent)); state->setState(waitForResponse, stateRingingOut); } else if (event->getStimulusType() == StimulusNewMessage) { MgcpCommand* test_item = event->getMessage(); if (typeid(*test_item) == typeid(NotificationRequest)) { NotificationRequest* nrPtr; nrPtr = dynamic_cast < NotificationRequest* > (test_item); if (nrPtr == NULL) wrongMessage(); // check for the right parameters state->CurrentRequestId = *(nrPtr->getRequestId()); vector < Event* > * eventList((nrPtr->getRequestedEvents())->getVector()); if (!eventList) wrongMessage(); // get the appropriate events -- vector < Event* > ::iterator eventIter; eventIter = eventList->begin(); bool lookForOffHook(false); while (eventIter != eventList->end()) { if (dynamic_cast < LineEventOffHook* > (*eventIter) != NULL) lookForOffHook = true; ++eventIter; } if (!lookForOffHook) wrongMessage(); SignalRequests* signals; signals = nrPtr->getSignalRequests(); vector < Signal* > * signalList(signals->getList()); if (!signalList) wrongMessage(); // get the appropriate signals -- vector < Signal* > ::iterator signalIter; signalIter = signalList->begin(); bool sendRing(false); bool sendRingback(false); while (signalIter != signalList->end()) { if (dynamic_cast < SignalRing* > (*signalIter) != NULL) { sendRing = true; } if (dynamic_cast < SignalRingback* > (*signalIter) != NULL) { sendRingback = true; } ++signalIter; } if (!(sendRing && sendRingback)) wrongMessage(); state->hardware->sendSignal(SignalRingStart); state->hardware->sendSignal(SignalRemoteRingbackStart); // reply OK. MgcpResponse response00(200, nrPtr->getTransactionId(), "OK"); response00.send(*(state->callAgent)); state->CurrentRequestId = (*(nrPtr->getRequestId())); state->setState(stateRingingIn, NULL); } } else { wrongMessage(); }}// the set is ringing and you may get the message offhook/onhookvoid stateRingingIn(StateEvent* event, StateMachine* state){ if (event->getStimulusType() == StimulusOffHook) { // send a notify Notify msg8(set); ObservedEvents events8; LineEventOffHook offHook; events8.insert(offHook); // msg8.insert(&set); msg8.insert(&events8); msg8.insert(&state->CurrentRequestId); msg8.send(*(state->callAgent));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -