/** ** File ......... MyMinionSocket.cpp ** Published .... 2004-04-20 **/ /* Copyright (C) 2004 grymse@alhem.net This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ //#include #include "MyHandler.h" #include "MyMinionSocket.h" #define DEB(x) x MyMinionSocket::MyMinionSocket(SocketHandler& h) :MinionSocket(h) { } MyMinionSocket::MyMinionSocket(SocketHandler& h,const std::string& s,ipaddr_t a,port_t p) :MinionSocket(h,s,a,p) { } MyMinionSocket::~MyMinionSocket() { } bool MyMinionSocket::OnVerifiedLine(const std::string& cmd,Parse& pa) { DEB( printf("minion cmd: %s\n",cmd.c_str());) if (cmd == "update") { std::string info_hash = pa.getword(); std::string escaped_peer_id = pa.getword(); std::string ip = pa.getword(); port_t port = pa.getvalue(); std::string upl = pa.getword(); std::string downl = pa.getword(); std::string left = pa.getword(); DEB( printf("Update: %s:%d\n",ip.c_str(),port);) MyHandler::PEER *p = static_cast(Handler()).reg_peer(info_hash, escaped_peer_id, ip, port); p -> update(upl,downl,left); p -> updated = false; { time_t t = time(NULL); struct tm *tp = localtime(&t); printf("%d-%02d-%02d %02d:%02d:%02d :: %s %s:%d\n",tp -> tm_year + 1900, tp -> tm_mon + 1,tp -> tm_mday, tp -> tm_hour,tp -> tm_min,tp -> tm_sec, cmd.c_str(),ip.c_str(),port); } } else if (cmd == "delete") { std::string info_hash = pa.getword(); std::string escaped_peer_id = pa.getword(); std::string ip = pa.getword(); port_t port = pa.getvalue(); std::string upl = pa.getword(); std::string downl = pa.getword(); std::string left = pa.getword(); DEB( printf("Delete: %s:%d\n",ip.c_str(),port);) MyHandler::PEER *p = static_cast(Handler()).reg_peer(info_hash, escaped_peer_id, ip, port); p -> stopped = true; p -> updated = false; { time_t t = time(NULL); struct tm *tp = localtime(&t); printf("%d-%02d-%02d %02d:%02d:%02d :: %s %s:%d\n",tp -> tm_year + 1900, tp -> tm_mon + 1,tp -> tm_mday, tp -> tm_hour,tp -> tm_min,tp -> tm_sec, cmd.c_str(),ip.c_str(),port); } } else if (cmd == "init") { port_t port = pa.getvalue(); { time_t t = time(NULL); struct tm *tp = localtime(&t); printf("%d-%02d-%02d %02d:%02d:%02d :: %s, port %d\n",tp -> tm_year + 1900, tp -> tm_mon + 1,tp -> tm_mday, tp -> tm_hour,tp -> tm_min,tp -> tm_sec, cmd.c_str(),port); } if (port != static_cast(Handler()).GetInt("server/port")) { printf("Port mismatch (%d != %d), shutting down connection\n",port,static_cast(Handler()).GetInt("server/port")); SetCloseAndDelete(); } else { static_cast(Handler()).SendInit(this); } } else { { time_t t = time(NULL); struct tm *tp = localtime(&t); printf("%d-%02d-%02d %02d:%02d:%02d :: %s\n",tp -> tm_year + 1900, tp -> tm_mon + 1,tp -> tm_mday, tp -> tm_hour,tp -> tm_min,tp -> tm_sec, cmd.c_str()); } return MinionSocket::OnVerifiedLine(cmd, pa); } return true; } void MyMinionSocket::OnConnect() { MinionSocket::OnConnect(); // SendHello std::string msg = "init"; msg += ":" + static_cast(Handler()).GetString("server/port"); static_cast(Handler()).SendMessage(Utility::base64(msg), 0); DEB( printf("Minion OnConnect()\n");) }