| 1 | [wiki:WikiStart Orbit] > [wiki:OTG OTG] > [wiki:OTG/FuncSpec Function Specifications] > Understanding OTR Main Program |
| 2 | |
| 3 | |
| 4 | Tha main() fucntion in otg.cpp is given as below: |
| 5 | {{{ |
| 6 | |
| 7 | int main(int argc, const char * argv[]) |
| 8 | { |
| 9 | OrbitApp * otr1 = new OTRApp(); |
| 10 | |
| 11 | try { |
| 12 | pthread_t thread1; |
| 13 | char *protocol_name = "udp"; |
| 14 | char *debuglog_name = NULL; |
| 15 | parseOptionsPhase1(argc, argv, &protocol_name, &debuglog_name); |
| 16 | Gate* gate = createGate(protocol_name); |
| 17 | //create dummy sink |
| 18 | Sink *sin = new DummySink(); |
| 19 | otr1->init(&argc, argv, debuglog_name); |
| 20 | parseOptionsPhase2(argc, argv, gate); |
| 21 | gate->configGate(otr1,sin); |
| 22 | gate->init(); |
| 23 | if ( pthread_create( &thread1, NULL,create_receive_thread, (void*)gate) != 0 ) |
| 24 | throw "Create Thread Failed..."; |
| 25 | char msg[MAX_INPUT_SIZE]; |
| 26 | while (1) { |
| 27 | cin.getline(msg, MAX_INPUT_SIZE ); |
| 28 | parseRuntimeOptions(msg, gate, thread1, otr1); |
| 29 | } |
| 30 | |
| 31 | }catch ( const char *reason ) { |
| 32 | cerr << "Exception:" << reason << endl; |
| 33 | exit(-1); |
| 34 | } |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | }}} |
| 40 | |
| 41 | === Explanation === |
| 42 | As can be seen, this is a very clean and short program. I explain it in detail. |
| 43 | |
| 44 | 1. First an [source:otg/trunk/src/cpp/orbitappt.h#latest OrbitApp] object is createdd to handle all OML-related stuff. |
| 45 | 1. Parsing user command-line input to determine |
| 46 | 1. protocol type |
| 47 | 1. debug mode is used or not. and if yes, the log file path. |
| 48 | 1. Create [source:otg/trunk/src/cpp/port.h#latest Gate] based on the "protocol type" |
| 49 | 1. Init the OML-related object. |
| 50 | 1. Conduct 2nd-tier parse to get all detail parameters of Gate such as receiving port. |
| 51 | 1. Init Gate |
| 52 | 1. Create a seperate thread to let Gate start receiving packets. |
| 53 | 1. An eternal while loop is actting as the continuing main thread to handle run-time user inputs. |
| 54 | |
| 55 | === Discussion === |
| 56 | It is possible to have multiple gates in one OTR application. More gates,listening to more ports and we could create more threads. But it seems there is no need to do this. Users care more than lower-layer link performance than have multiple competitive applications running on one single port. |
| 57 | |
| 58 | |