00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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();
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