Google
Web alhem.net
Main Page | Alphabetical List | Class List | File List | Class Members | File Members

MyTcpSocket.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 #include <string>
00027 #include <vector>
00028 #include <map>
00029 #include <Parse.h>
00030 #include <time.h>
00031 
00032 #include "config.h"
00033 #include "minder.h"
00034 #include "MyHandler.h"
00035 #include "TestSocket.h"
00036 #include "MyTcpSocket.h"
00037 
00038 using std::string;
00039 
00040 #ifdef _DEBUG
00041 #define DEB(x) x
00042 #else
00043 #define DEB(x)  
00044 #endif
00045 
00046 
00047 
00048 
00049 MyTcpSocket::MyTcpSocket(ISocketHandler& h)
00050 :TcpSocket(h)
00051 {
00052         SetLineProtocol(); // we want the OnLine callback
00053 }
00054 
00055 
00056 MyTcpSocket::~MyTcpSocket()
00057 {
00058 }
00059 
00060 
00061 void MyTcpSocket::OnLine(const std::string& line)
00062 {
00063         std::string cmd;
00064         std::string app;
00065         std::string id;
00066         std::string host;
00067         Parse pa(Utility::base64d(line),"_:");
00068         pa.getword(cmd); // 'Hello' / 'Goodbye'
00069         app = Utility::base64d(pa.getword());
00070         pa.getword(id);
00071         pa.getword(host); // host
00072         port_t port = pa.getvalue();
00073         unsigned long hid_from_host = pa.getvalue();
00074         long extra_info = pa.getvalue();
00075 
00076         if (cmd != "Hello" && cmd != "Goodbye")
00077         {
00078                 SetCloseAndDelete(true);
00079                 return;
00080         }
00081 
00082         // get ip sent to us by the minion
00083         ipaddr_t ip;
00084         Utility::u2ip(host,ip);
00085 
00086         // we ignore whatever ip the minion sends us
00087         // we only use the ip from the connection endpoint
00088         ipaddr_t ip2 = GetRemoteIP4();
00089 DEB(    printf("Incoming: id '%s' %s:%d cmd '%s'\n",id.c_str(),GetRemoteAddress().c_str(),port,cmd.c_str());)
00090         if (cmd == "Goodbye")
00091         {
00092                 static_cast<MyHandler&>(Handler()).unreg_host(app, id, ip2, port, ip);
00093                 SetCloseAndDelete(true);
00094                 return;
00095         }
00096         unsigned long host_id = 0;
00097         HOST *h = static_cast<MyHandler&>(Handler()).reg_host(app, id, ip2, port, ip, host_id, hid_from_host);
00098         h -> extra_information = extra_info;
00099         {
00100                 std::string str;
00101                 std::string msg;
00102                 std::string ipstr;
00103 
00104                 str = "You_" + id + ":" + GetRemoteAddress() + ":" + Utility::l2string(port) + ":" + Utility::l2string(host_id);
00105                 msg = Utility::base64(str) + "\n";
00106 
00107                 if (config[app + "/dbname"].size())
00108                 {
00109                         str = "Database_" + config[app + "/dbname"];
00110                         msg += Utility::base64(str) + "\n";
00111                 }
00112 
00113                 if (config[app + "/update_host"].size() &&
00114                     config[app + "/update_port"].size() &&
00115                     config[app + "/update_url"].size() )
00116                 {
00117                         str = "Update_" + config[app + "/update_host"];
00118                         str += ":" + config[app + "/update_port"];
00119                         str += ":" + config[app + "/update_url"];
00120                         msg += Utility::base64(str) + "\n";
00121                 }
00122 
00123                 static_cast<MyHandler&>(Handler()).reset_sent(app);
00124                 for (int i = 0; i < 20; i++)
00125                 {
00126                         HOST *p = static_cast<MyHandler&>(Handler()).GetRandomHOST(app);
00127                         if (!p -> sent)
00128                         {
00129                                 Utility::l2ip(p -> ip,ipstr);
00130                                 str = "Minion_" + p -> id + ":" + ipstr + ":" + Utility::l2string(p -> port);
00131                                 str += ":" + Utility::l2string(p -> host_id);
00132                                 msg += Utility::base64(str) + "\n";
00133                                 p -> sent = true;
00134                         }
00135                 }
00136                 msg += Utility::base64("End") + "\n";
00137                 Send(msg);
00138         }
00139 
00140         if (app == "viawww" && !h -> connectable) // try again
00141         {
00142                 TestSocket *t = new TestSocket(Handler(),h -> connectable);
00143                 t -> Open(ip2, h -> extra_information);
00144                 t -> SetDeleteByHandler();
00145                 Handler().Add(t);
00146         }
00147 }
00148 
00149 
00150 void MyTcpSocket::InitSSLServer()
00151 {
00152         InitializeContext("comb.pem","");
00153 }
00154 
00155 

Generated on Tue Oct 3 23:47:05 2006 for The Minder by doxygen 1.3.6