00001 #ifdef _WIN32
00002 #pragma warning(disable:4786)
00003 #endif
00004 #include <StdoutLog.h>
00005 #include <SocketHandler.h>
00006 #include <TcpSocket.h>
00007 #include <ListenSocket.h>
00008 #include <Utility.h>
00009 #include <Parse.h>
00010 #include <HttpGetSocket.h>
00011 #include <PoolSocket.h>
00012 #include <Socket.h>
00013 #include <HttpDebugSocket.h>
00014 #include <ResolvServer.h>
00015
00016 #ifdef SOCKETS_NAMESPACE
00017 using namespace SOCKETS_NAMESPACE;
00018 #endif
00019
00020
00021 class MyHandler : public SocketHandler
00022 {
00023 public:
00024 MyHandler(StdLog *p) : SocketHandler(p),m_done(false),m_quit(false) {}
00025 ~MyHandler() {}
00026
00027 void List(TcpSocket *p) {
00028 for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00029 {
00030 Socket *p0 = (*it).second;
00031 if (dynamic_cast<PoolSocket *>(p0))
00032 {
00033 p -> Send("PoolSocket\n");
00034 }
00035 else
00036 if (dynamic_cast<HttpGetSocket *>(p0))
00037 {
00038 p -> Send("HttpGetSocket\n");
00039 }
00040 else
00041 if (dynamic_cast<TcpSocket *>(p0))
00042 {
00043 p -> Send("TcpSocket\n");
00044 }
00045 else
00046 {
00047 p -> Send("Some kind of Socket\n");
00048 }
00049 bool r;
00050 bool w;
00051 bool e;
00052 Handler().ISocketHandler_Get(p, r, w, e);
00053 char slask[1000];
00054 sprintf(slask," Read: %s Write: %s Exception: %s\n",
00055 r ? "SET" : "not set",
00056 w ? "SET" : "not set",
00057 e ? "SET" : "not set");
00058 p -> Send( slask );
00059 }
00060 }
00061 void SetQuit() { m_quit = true; }
00062 bool Quit() { return m_quit; }
00063 void CheckHtml() {
00064 if (m_done)
00065 {
00066 if (m_ok)
00067 printf("Html OK:\n%s\n", m_html.c_str());
00068 else
00069 printf("Html Failed\n");
00070 m_done = false;
00071 }
00072 }
00073
00074 std::string m_html;
00075 bool m_ok;
00076 bool m_done;
00077
00078 private:
00079 bool m_quit;
00080 };
00081
00082
00083 class MySocket : public TcpSocket
00084 {
00085 public:
00086 MySocket(ISocketHandler& h) : TcpSocket(h) {
00087 }
00088 void OnAccept() {
00089 int port = GetParent() -> GetPort();
00090 Send("I'm the server at port " +
00091 Utility::l2string(port) + "\n");
00092 SetCloseAndDelete();
00093 }
00094 };
00095
00096
00097 class hSocket : public HttpGetSocket
00098 {
00099 public:
00100 hSocket(ISocketHandler& h,const std::string& x,const std::string& y) : HttpGetSocket(h,x,y) {}
00101
00102 void OnConnect() {
00103 printf("hSocket::OnConnect\n");
00104 HttpGetSocket::OnConnect();
00105 }
00106 };
00107
00108
00109 class OrderSocket : public TcpSocket
00110 {
00111 public:
00112 OrderSocket(ISocketHandler& h) : TcpSocket(h) {
00113 SetLineProtocol();
00114 }
00115 Socket *Create() {
00116 Handler().LogError(this, "Create", 0, "OrderSocket", LOG_LEVEL_INFO);
00117 return new OrderSocket(Handler());
00118 }
00119 void OnAccept() {
00120 Send("Cmd (get,quit,list,stop,detach,count,resolve <name>)>");
00121 }
00122 void OnLine(const std::string& line) {
00123 Parse pa(line);
00124 std::string cmd = pa.getword();
00125 std::string arg = pa.getrest();
00126 if (cmd == "get")
00127 {
00128 HttpGetSocket *p = new hSocket(Handler(), arg, "tmpfile.html");
00129 p -> SetHttpVersion("HTTP/1.1");
00130 p -> AddResponseHeader("Connection", "keep-alive");
00131 p -> SetDeleteByHandler();
00132 Handler().Add( p );
00133 Send("Reading url '" + arg + "'\n");
00134 }
00135 else
00136 if (cmd == "quit")
00137 {
00138 Send("Goodbye!\n");
00139 SetCloseAndDelete();
00140 }
00141 else
00142 if (cmd == "list")
00143 {
00144 static_cast<MyHandler&>(Handler()).List( this );
00145 }
00146 else
00147 if (cmd == "stop")
00148 {
00149 static_cast<MyHandler&>(Handler()).SetQuit();
00150 }
00151 else
00152 if (cmd == "resolve")
00153 {
00154
00155 ipaddr_t a;
00156 if (Utility::u2ip(arg, a))
00157 {
00158 std::string tmp;
00159 Utility::l2ip(a, tmp);
00160 Send("Resolved: " + tmp + "\n");
00161 }
00162 else
00163 {
00164 Send("Resolve failed: " + arg + "\n");
00165 }
00166 }
00167 else
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 if (cmd == "detach")
00179 {
00180 if (!Detach())
00181 {
00182 Send("Detach() call failed\n");
00183 }
00184 else
00185 {
00186 Send("Ok.\n");
00187 }
00188 }
00189 else
00190 if (cmd == "count")
00191 {
00192 Send("Socket count: " + Utility::l2string( (long)Handler().GetCount()) + "\n");
00193 }
00194 else
00195 {
00196 Send("Huh?\n");
00197 }
00198 Send("Cmd>");
00199 }
00200 void OnDelete() {
00201 printf("OrderSocket::OnDelete()\n");
00202 }
00203 void OnResolved(int id,ipaddr_t a,port_t port)
00204 {
00205 }
00206 void OnResolved(int id,const std::string& name,port_t port)
00207 {
00208 Send("Resolve id " + Utility::l2string(id) + " = " + name + "\n");
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 void OnDetached() {
00225 Send("\nDetached.\nCmd>");
00226 }
00227 };
00228
00229
00230 class TestSocket : public TcpSocket
00231 {
00232 public:
00233 TestSocket(ISocketHandler& h) : TcpSocket(h) {
00234 SetLineProtocol();
00235 }
00236 void OnConnect() {
00237 printf("TestSocket connected, sending QUIT\n");
00238 Send( "quit\n" );
00239 }
00240 void OnConnectFailed() {
00241 printf("TestSocket::OnConnectFailed\n");
00242 SetCloseAndDelete();
00243 }
00244 void OnLine(const std::string& line) {
00245 printf("TestSocket: %s\n", line.c_str());
00246 }
00247 void OnDelete() {
00248 printf("TestSocket::OnDelete()\n");
00249 }
00250 void OnResolved(int id,ipaddr_t a,port_t port) {
00251 printf("TestSocket::OnResolved(): %d, %08x:%d\n", id, a, port);
00252 TcpSocket::OnResolved(id,a,port);
00253 }
00254 };
00255
00256
00257 int main()
00258 {
00259 StdoutLog log;
00260 MyHandler h(&log);
00261
00262 h.EnableResolver(9999);
00263
00264 printf(" *** My hostname: %s\n", Utility::GetLocalHostname().c_str());
00265 printf(" *** My local IP: %s\n", Utility::GetLocalAddress().c_str());
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 ListenSocket<MySocket> l1(h);
00278 if (l1.Bind(1024))
00279 {
00280 printf("Bind 1024 failed\n");
00281 exit(-1);
00282 }
00283 h.Add(&l1);
00284
00285
00286 ListenSocket<MySocket> l2(h);
00287 if (l2.Bind(1025))
00288 {
00289 printf("Bind 1025 failed\n");
00290 exit(-1);
00291 }
00292 h.Add(&l2);
00293
00294
00295 ListenSocket<OrderSocket> l3(h);
00296 if (l3.Bind(1026))
00297 {
00298 printf("Bind 1026 failed\n");
00299 exit(-1);
00300 }
00301 h.Add(&l3);
00302
00303
00304 ListenSocket<HttpDebugSocket> l4(h);
00305 if (l4.Bind(8080))
00306 {
00307 printf("Bind 8080 failed\n");
00308 exit(-1);
00309 }
00310 h.Add(&l4);
00311
00312
00313 printf("Waiting for resolver ...");
00314 while (!h.ResolverReady())
00315 ;
00316 printf(" resolver ready!\n");
00317
00318 TestSocket ts(h);
00319 printf(">>> TestSocket.Open\n");
00320 ts.Open("localhost", 1026);
00321 printf(">>> Adding TestSocket\n");
00322 h.Add(&ts);
00323
00324 printf(">>> mainloop\n");
00325 h.Select(0,0);
00326 while (!h.Quit())
00327 {
00328 h.Select(1,0);
00329 h.CheckSanity();
00330 }
00331
00332 return 0;
00333 }
00334
00335