RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/testing/ping_pong.h
00001 struct PingConnectionHandler : public ConnectionHandler {
00002     PingConnectionHandler(std::string & errorWhere,
00003                           ACE_Semaphore & finished)
00004         : errorWhere(errorWhere), finished(finished), messages(0)
00005     {
00006         constructed = Date::now();
00007         //cerr << "creating ping handler" << endl;
00008     }
00009 
00010     ~PingConnectionHandler()
00011     {
00012         //cerr << "destructing ping handler" << endl;
00013     }
00014 
00015     std::string & errorWhere;
00016     ACE_Semaphore & finished;
00017     int messages;
00018     Date constructed;
00019 
00020     void doError(const std::string & error)
00021     {
00022         errorWhere = error;
00023     }
00024 
00025     void onGotTransport()
00026     {
00027         startReading();
00028         startWriting();
00029     }
00030 
00031     void handleInput()
00032     {
00033         //cerr << "ping got input" << endl;
00034         //cerr << Date::now().print(5)
00035         //     << " ping handle_input on " << fd << " messages = "
00036         //     << messages << endl;
00037 
00038         char buf[100] = "error";
00039         int res = recv(buf, 100, MSG_DONTWAIT);
00040         if (res != -1)
00041             buf[res] = 0;
00042         BOOST_CHECK_EQUAL(res, 4);
00043         if (res == -1)
00044             BOOST_CHECK_EQUAL(strerror(errno), "success");
00045         BOOST_CHECK_EQUAL(string(buf), string("Hi!!"));
00046 
00047         if (messages == 1000) {
00048             cerr << "did 1000 messages in "
00049                  << Date::now().secondsSince(constructed)
00050                  << " seconds" << endl;
00051 
00052             stopReading();
00053             stopWriting();
00054             finished.release();
00055             closeWhenHandlerFinished();
00056             return;
00057         }
00058 
00059         ++messages;
00060         if (messages % 100 == 0)
00061             cerr << messages << endl;
00062 
00063         startWriting();
00064     }
00065 
00066     void handleOutput()
00067     {
00068         //cerr << "ping got output" << endl;
00069         //cerr << Date::now().print(5)
00070         //     << " ping handle_output on " << fd << endl;
00071 
00072         int res = send("hello", 5, MSG_DONTWAIT | MSG_NOSIGNAL);
00073         BOOST_CHECK_EQUAL(res, 5);
00074         if (res == -1)
00075             BOOST_CHECK_EQUAL(strerror(errno), "success");
00076         stopWriting();
00077     }
00078 };
00079 
00080 struct PongConnectionHandler : public ConnectionHandler {
00081     PongConnectionHandler(std::string & errorWhere)
00082         : errorWhere(errorWhere)
00083     {
00084         //cerr << "creating pong handler" << endl;
00085     }
00086 
00087     ~PongConnectionHandler()
00088     {
00089         //cerr << "destructing pong handler" << endl;
00090     }
00091 
00092     std::string & errorWhere;
00093 
00094     void doError(const std::string & error)
00095     {
00096         errorWhere = error;
00097     }
00098 
00099     void onGotTransport()
00100     {
00101         //cerr << "pong handler on GotTransport: handle " << getHandle()
00102         //     << endl;
00103         startReading();
00104     }
00105 
00106     void handleInput()
00107     {
00108         //cerr << "pong handler on handleInput: handle " << getHandle()
00109         //     << endl;
00110         //cerr << Date::now().print(5)
00111         //     << " pong handle_input on " << fd << endl;
00112 
00113         char buf[100] = "error";
00114         int res = recv(buf, 100, MSG_DONTWAIT);
00115 
00116         if (res == 0) {
00117             closeWhenHandlerFinished();
00118             return;
00119         }
00120         if (res != -1)
00121             buf[res] = 0;
00122         BOOST_CHECK_EQUAL(res, 5);
00123         if (res == -1)
00124             BOOST_CHECK_EQUAL(strerror(errno), "success");
00125         BOOST_CHECK_EQUAL(buf, string("hello"));
00126         startWriting();
00127     }
00128 
00129     void handleOutput()
00130     {
00131         //cerr << "pong handler on handleOutput: handle " << getHandle()
00132         //     << endl;
00133         //cerr << Date::now().print(5)
00134         //     << " pong handle_output on " << fd << endl;
00135         
00136         int res = send("Hi!!", 4, MSG_DONTWAIT | MSG_NOSIGNAL);
00137         BOOST_CHECK_EQUAL(res, 4);
00138         if (res == -1)
00139             BOOST_CHECK_EQUAL(strerror(errno), "success");
00140         stopWriting();
00141     }
00142 };
00143 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator