Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

SarCommand.cpp

Go to the documentation of this file.
00001 #ifdef TEST
00002 
00003 #include "headers.h"
00004 
00005 const static string sarCommandString = "sar";
00006 const static string sendCommandString = "send1";
00007 const static string send2CommandString = "send2";
00008 const static string send3CommandString = "send3";
00009 const static string constructCommandString = "construct";
00010 const static string destructCommandString = "destruct";
00011 
00012 SarCommand::SarCommand() : Command() {
00013     m_packetWriterA = new PacketWriterTest();
00014     m_packetWriterB = new PacketWriterTest();
00015     m_sarA = new SAR(m_packetWriterA, 1);
00016     m_sarB = new SAR(m_packetWriterB, 2);
00017     m_packetWriterA->setSar(m_sarB);
00018     m_packetWriterB->setSar(m_sarA);
00019     
00020     m_singleSar = NULL;
00021 }
00022 
00023 
00024 SarCommand::~SarCommand() {
00025     delete m_sarA;
00026     delete m_sarB;
00027     delete m_packetWriterA;
00028     delete m_packetWriterB;
00029 }
00030 
00031 
00032 string
00033 SarCommand::getCommandString() {
00034     return sarCommandString;
00035 }
00036 
00037 
00038 void
00039 SarCommand::run(std::ostream& s) {
00040     if (m_args[1].empty()) {
00041         getHelp(s);
00042         return;
00043     }
00044 
00045     if (m_args[1] == showCommandString) {
00046         s << "Sending side of the connection:\n" << *m_sarA 
00047             << "\nReceiving side of the connection:\n" << *m_sarB;
00048     }
00049     else if (m_args[1] == constructCommandString) {
00050         if (m_singleSar == NULL) {
00051             m_singleSar = new SAR(NULL, 0);
00052             s << *m_singleSar;
00053         }
00054     }
00055     else if (m_args[1] == destructCommandString) {
00056         if (m_singleSar != NULL) {
00057             delete m_singleSar;
00058             m_singleSar = NULL;
00059             s << "Destroyed\n";
00060         }
00061         else {
00062             s << "You must construct one first.";
00063         }
00064     }
00065     else if (m_args[1] == sendCommandString) {
00066         pthread_t receiveThreadId;
00067         pthread_attr_t attr;
00068         pthread_attr_init(&attr);
00069         pthread_create(&receiveThreadId, &attr, receive, (void*)m_sarB);
00070 
00071         const int bufLength = 1024;
00072         char buf[bufLength];
00073         strcpy(buf, "Hello world");
00074 
00075         // send all the data 
00076         int retVal = m_sarA->reliableSend((unsigned char*)&buf[0], strlen(&buf[0]));
00077         if (retVal) {
00078             s << "Sent string \"" << &buf[0] << "\" successfully\n" << retVal << " bytes transferred.\n";
00079         }
00080         else {
00081             s << "Error during transmission: " << retVal << "\n";
00082         }
00083     }
00084     else if (m_args[1] == send2CommandString) {
00085         pthread_t receiveThreadId;
00086         pthread_attr_t attr;
00087         pthread_attr_init(&attr);
00088         pthread_create(&receiveThreadId, &attr, receive, (void*)m_sarB);
00089 
00090         const int bufLength = SEGMENT_SIZE + 1;
00091         char buf[bufLength + 1];
00092         memset(&buf[0], (int)'a', bufLength);
00093         buf[bufLength - 3] = '\n';
00094         buf[bufLength - 2] = 'b';
00095         buf[bufLength - 1] = 0;
00096         // send all the data 
00097         int retVal = m_sarA->reliableSend((unsigned char*)&buf[0], bufLength);
00098         if (retVal) {
00099             s << "Sent string \"" << &buf[0] << "\" successfully\n" << retVal << " bytes transferred.\n";
00100         }
00101         else {
00102             s << "Error during transmission: " << retVal << "\n";
00103         }
00104         
00105     }
00106     else if (m_args[1] == send3CommandString) {
00107         TimeValue start = TimeValue::getCurrentTime();
00108 
00109         if (!m_args[3].empty()) {
00110             m_packetWriterA->setDropDataProbability(atoi(m_args[3].c_str()));
00111         }
00112         
00113         if (!m_args[4].empty()) {
00114             m_packetWriterA->setDropAckProbability(atoi(m_args[4].c_str()));
00115         }
00116 
00117         pthread_t receiveThreadId;
00118         pthread_attr_t attr;
00119         pthread_attr_init(&attr);
00120         pthread_create(&receiveThreadId, &attr, receive, (void*)m_sarB);
00121 
00122         int numPackets = atoi(m_args[2].c_str());
00123         int bufLength = SEGMENT_SIZE * numPackets;
00124         char* buf = new char[bufLength + 1];
00125         memset(&buf[0], (int)'a', bufLength);
00126         buf[bufLength - 3] = '\n';
00127         buf[bufLength - 2] = 'b';
00128         buf[bufLength - 1] = 0;
00129         
00130         // send all the data 
00131         int retVal = m_sarA->reliableSend((unsigned char*)&buf[0], bufLength);
00132      
00133         // fake connection closing on both SARs
00134         m_sarA->connectionClosed();
00135         m_sarB->connectionClosed();
00136 
00137         // join with the receive thread - this verifies that the receive side is working.
00138         void* statusp;
00139         pthread_join(receiveThreadId, &statusp);
00140 
00141         // delete and recreate SARs
00142         delete m_sarA;
00143         delete m_sarB;
00144         m_sarA = new SAR(m_packetWriterA, 1);
00145         m_sarB = new SAR(m_packetWriterB, 2);
00146         m_packetWriterA->setSar(m_sarB);
00147         m_packetWriterB->setSar(m_sarA);
00148 
00149         TimeValue totalTime = TimeValue::getCurrentTime() - start;
00150 
00151         if (retVal > 0) {
00152             s << retVal << " bytes transferred.\n";
00153             s << "Total time: " << totalTime;
00154         }
00155         else {
00156             s << "Error during transmission: " << retVal << "\n";
00157         }
00158         delete buf;
00159     }
00160     else {
00161         getHelp(s);
00162         return;
00163     }
00164 }
00165 
00166 
00167 void*
00168 SarCommand::receive(void* sarPtr) {
00169     //pthread_detach(pthread_self());
00170     const int bufLength = 1024;
00171     char buf[bufLength];
00172     SAR* sar = (SAR*)sarPtr;
00173     
00174     memset(buf, 0, bufLength);
00175     int bytes;
00176     while ( (bytes = sar->receive((unsigned char*)&buf[0], bufLength)) > 0) {
00177         buf[bytes] = 0;
00178         debug(DEBUG_SAR, "Received %d bytes", bytes);
00179     }
00180     return NULL;
00181 }
00182 
00183 
00184 void
00185 SarCommand::getHtmlInterface(std::ostream& s) {
00186     s << "<h2>Segmentation And Reassembly (SAR)</h2>"
00187         << "Note: This command simulates the transfer of data over a network. "
00188         "It does the packet transmission in-memory.\n";
00189     beginUl(s);
00190     generateHtmlSubcmd(s, constructCommandString, "Construct (not needed before send tests)");
00191     generateHtmlSubcmd(s, destructCommandString, "Destruct");
00192     generateHtmlSubcmd(s, showCommandString, "Show");
00193     generateHtmlSubcmd(s, sendCommandString, "Send a one packet transmission");
00194     generateHtmlSubcmd(s, send2CommandString, "Send a two packet transmission");
00195     generateHtmlSubcmdArg3(s, 
00196                            send3CommandString, 
00197                            "Send a multi-packet transmission, number of packets to send: ", 
00198                            "\nDrop Data Probability: ",
00199                            "\nDrop ACK Probability: ",
00200                            "Send");
00201     endUl(s);
00202 }
00203 
00204 
00205 void
00206 SarCommand::getHelp(std::ostream& s) {
00207     s << sarCommandString << "\n"
00208       << "  " << constructCommandString << "\n"
00209       << "  " << destructCommandString << "\n"
00210       << "  " << showCommandString << "\n"
00211       << "  " << sendCommandString << "\n"
00212       << "  " << send2CommandString << "\n"
00213       << "  " << send3CommandString << "\n";
00214 }
00215 
00216 #endif
00217 

Generated at Thu Jul 11 13:31:51 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001