Google
Web alhem.net

MyMinionSocket.cpp

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2004  grymse@alhem.net
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 */
00022 //#include <stdio.h>
00023 #include "MyHandler.h"
00024 #include "MyMinionSocket.h"
00025 
00026 #define DEB(x) x
00027 
00028 
00029 MyMinionSocket::MyMinionSocket(ISocketHandler& h)
00030 :MinionSocket(h)
00031 {
00032 }
00033 
00034 
00035 MyMinionSocket::MyMinionSocket(ISocketHandler& h,const std::string& s,ipaddr_t a,port_t p)
00036 :MinionSocket(h,s,a,p)
00037 {
00038 }
00039 
00040 
00041 MyMinionSocket::~MyMinionSocket()
00042 {
00043 }
00044 
00045 
00046 bool MyMinionSocket::OnVerifiedLine(const std::string& cmd,Parse& pa)
00047 {
00048 DEB(    printf("minion cmd: %s\n",cmd.c_str());)
00049 
00050         if (cmd == "update")
00051         {
00052                 std::string info_hash = pa.getword();
00053                 std::string escaped_peer_id = pa.getword();
00054                 std::string ip = pa.getword();
00055                 port_t port = pa.getvalue();
00056                 std::string upl = pa.getword();
00057                 std::string downl = pa.getword();
00058                 std::string left = pa.getword();
00059 
00060 DEB(            printf("Update:  %s:%d\n",ip.c_str(),port);)
00061 
00062                 MyHandler::PEER *p = static_cast<MyHandler&>(Handler()).reg_peer(info_hash, escaped_peer_id, ip, port);
00063                 p -> update(upl,downl,left);
00064                 p -> updated = false;
00065                 {
00066                         time_t t = time(NULL);
00067                         struct tm *tp = localtime(&t);
00068                         printf("%d-%02d-%02d %02d:%02d:%02d :: %s %s:%d\n",tp -> tm_year + 1900,
00069                                 tp -> tm_mon + 1,tp -> tm_mday,
00070                                 tp -> tm_hour,tp -> tm_min,tp -> tm_sec,
00071                                 cmd.c_str(),ip.c_str(),port);
00072                 }
00073         }
00074         else
00075         if (cmd == "delete")
00076         {
00077                 std::string info_hash = pa.getword();
00078                 std::string escaped_peer_id = pa.getword();
00079                 std::string ip = pa.getword();
00080                 port_t port = pa.getvalue();
00081                 std::string upl = pa.getword();
00082                 std::string downl = pa.getword();
00083                 std::string left = pa.getword();
00084 
00085 DEB(            printf("Delete:  %s:%d\n",ip.c_str(),port);)
00086 
00087                 MyHandler::PEER *p = static_cast<MyHandler&>(Handler()).reg_peer(info_hash, escaped_peer_id, ip, port);
00088                 p -> stopped = true;
00089                 p -> updated = false;
00090                 {
00091                         time_t t = time(NULL);
00092                         struct tm *tp = localtime(&t);
00093                         printf("%d-%02d-%02d %02d:%02d:%02d :: %s %s:%d\n",tp -> tm_year + 1900,
00094                                 tp -> tm_mon + 1,tp -> tm_mday,
00095                                 tp -> tm_hour,tp -> tm_min,tp -> tm_sec,
00096                                 cmd.c_str(),ip.c_str(),port);
00097                 }
00098         }
00099         else
00100         if (cmd == "init")
00101         {
00102                 port_t port = pa.getvalue();
00103                 {
00104                         time_t t = time(NULL);
00105                         struct tm *tp = localtime(&t);
00106                         printf("%d-%02d-%02d %02d:%02d:%02d :: %s, port %d\n",tp -> tm_year + 1900,
00107                                 tp -> tm_mon + 1,tp -> tm_mday,
00108                                 tp -> tm_hour,tp -> tm_min,tp -> tm_sec,
00109                                 cmd.c_str(),port);
00110                 }
00111                 if (port != static_cast<MyHandler&>(Handler()).GetInt("server/port"))
00112                 {
00113                         printf("Port mismatch (%d != %d), shutting down connection\n",port,static_cast<MyHandler&>(Handler()).GetInt("server/port"));
00114                         SetCloseAndDelete();
00115                 }
00116                 else
00117                 {
00118                         static_cast<MyHandler&>(Handler()).SendInit(this);
00119                 }
00120         }
00121         else
00122         {
00123                 {
00124                         time_t t = time(NULL);
00125                         struct tm *tp = localtime(&t);
00126                         printf("%d-%02d-%02d %02d:%02d:%02d :: %s\n",tp -> tm_year + 1900,
00127                                 tp -> tm_mon + 1,tp -> tm_mday,
00128                                 tp -> tm_hour,tp -> tm_min,tp -> tm_sec,
00129                                 cmd.c_str());
00130                 }
00131                 return MinionSocket::OnVerifiedLine(cmd, pa);
00132         }
00133         return true;
00134 }
00135 
00136 
00137 void MyMinionSocket::OnConnect()
00138 {
00139         MinionSocket::OnConnect(); // SendHello
00140         std::string msg = "init";
00141         msg += ":" + static_cast<MyHandler&>(Handler()).GetString("server/port");
00142         static_cast<MyHandler&>(Handler()).SendMessage(Utility::base64(msg), 0);
00143 DEB(    printf("Minion OnConnect()\n");)
00144 }
00145 
00146 

Generated on Thu Sep 27 12:58:27 2007 for distributed bittorrent tracker by  doxygen 1.5.2