| 1 | [wiki:WikiStart Orbit] > [wiki:OTG OTG] > [wiki:OTG/FuncSpec Function Specifications] > Understanding OTG 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 | |
| 10 | OrbitApp *otg1 = new OTGApp(); |
| 11 | |
| 12 | try { |
| 13 | |
| 14 | pthread_t thread1; |
| 15 | char *protocol_name = "udp"; |
| 16 | char *generator_name = "cbr"; |
| 17 | char *debuglog_name = NULL; |
| 18 | parseOptionsPhase1(argc, argv, &protocol_name, &generator_name, &debuglog_name); |
| 19 | Port* port = createPort(protocol_name); |
| 20 | Generator *gen = createGenerator(generator_name); |
| 21 | otg1->init(&argc, argv, debuglog_name); //Initialize OML |
| 22 | parseOptionsPhase2(argc, argv, port, gen); |
| 23 | port->init(); |
| 24 | gen->init(); |
| 25 | Stream *str = new Stream(0, gen, port, otg1); //default stream No.0 |
| 26 | int cst1 = pthread_create( &thread1, NULL, create_stream_function, (void *)str); |
| 27 | if (cst1!=0)throw "Create a Stream Thread Failed..."; |
| 28 | work(str); //this is the main thread |
| 29 | |
| 30 | } catch (const char *reason ) { |
| 31 | cerr << "Exception:" << reason << endl; |
| 32 | exit(-1); |
| 33 | } |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | }}} |
| 38 | |
| 39 | === Explanation === |
| 40 | As can be seen, this is a very clean and short program. I explain it in detail. |
| 41 | |
| 42 | 1. First an [source:otg/trunk/src/cpp/orbitappt.h#latest OrbitApp] object is createdd to handle all OML-related stuff. |
| 43 | 1. Parsing user command-line input to determine |
| 44 | 1. protocol type |
| 45 | 1. generator type |
| 46 | 1. debug mode is used or not. and if yes, the log file path. |
| 47 | 1. Create [source:otg/trunk/src/cpp/port.h#latest Port] and [source:otg/trunk/src/cpp/generator.h#latest Generator] based on the "protocol type" and "generator type" |
| 48 | 1. Init the OML-related object. |
| 49 | 1. Conduct 2nd-tier parse to get all detail parameters of Port and Generator |
| 50 | 1. Init Port and Generator |
| 51 | 1. Create Stream Object to control Port and Generator, as a seperate thread. |
| 52 | 1. Let work() actting as the continuing main thread to handle run-time user inputs. this function is designed as an eternal while loop. |
| 53 | |
| 54 | === Discussion === |
| 55 | We are going to have multiple streams in one OTG using multiple thread. The main thread is handling command-line inputs. Each stream is a seperate thread, by calling th stream_init functon, the stream is independently sending packets. Now, one stream is enough. Although the current architecture allows more streams in more threads (and we already test this successfully), there seems no need for doing this. If multiple streams are prefered, the user could run multiple OTG programs. |
| 56 | |