00001 #ifdef TEST
00002
00003 #include "headers.h"
00004
00005 static const string catcherCommandString = "catcher";
00006 static const string addCommandString = "add";
00007 static const string writeCommandString = "write";
00008 static const string readCommandString = "read";
00009
00010 void
00011 CatcherCommand::getHtmlInterface(std::ostream& s) {
00012 s << "<h2>Catcher</h2>";
00013 generateHtmlCmd(s, "(help)");
00014 beginUl(s);
00015 generateHtmlSubcmd(s, showCommandString, "show");
00016 generateHtmlSubcmdArg2(s, addCommandString, "Add an IP address to the list of peers: ", "Port number (optional): ", "Add");
00017 generateHtmlSubcmdArg1(s, writeCommandString, "Write host list to disk, type file name: ", "Write");
00018 generateHtmlSubcmdArg1(s, readCommandString, "Read host list from disk, type file name: ", "Read");
00019 endUl(s);
00020 }
00021
00022
00023 string
00024 CatcherCommand::getCommandString() {
00025 return catcherCommandString;
00026 }
00027
00028
00029 void
00030 CatcherCommand::run(std::ostream& s) {
00031 if (m_args[1].empty()) {
00032 getHelp(s);
00033 return;
00034 }
00035 if(m_args[1] == showCommandString) {
00036 s << "Current list of all known nodes:\n" << *GlobalObjects::instance()->getCatcher();
00037 return;
00038 }
00039 else if(m_args[1] == addCommandString) {
00040 catcherAdd(s);
00041 return;
00042 }
00043 else if(m_args[1] == writeCommandString) {
00044 catcherWrite(s);
00045 return;
00046 }
00047 else if(m_args[1] == readCommandString) {
00048 catcherRead(s);
00049 return;
00050 }
00051 else {
00052 getHelp(s);
00053 return;
00054 }
00055 }
00056
00057
00058 void
00059 CatcherCommand::getHelp(std::ostream& s) {
00060 s << "Syntax: "
00061 << catcherCommandString << " <subcommand> [arg] -- subcommands:\n"
00062 << " " << showCommandString << "\n"
00063 << " " << addCommandString << " - add an (unfirewalled) node to the host list\n"
00064 << " Arguments: IP_ADDRESS PORT\n"
00065 << " Example: " << catcherCommandString << " " << addCommandString << " 1.2.3.4:80\n"
00066 << " " << writeCommandString << " - Write host list to disk\n"
00067 << " Arguments: FILE_NAME (Default: " << GlobalObjects::instance()->getConfig()->getPeerListFileName() << ")\n"
00068 << " " << readCommandString << " - Read host list from disk\n"
00069 << " Arguments: FILE_NAME (Default: " << GlobalObjects::instance()->getConfig()->getPeerListFileName() << ")\n";
00070 }
00071
00072
00078 void
00079 CatcherCommand::catcherAdd(std::ostream& s)
00080 {
00081 SocketAddress socketAddress(IpAddress((unsigned long)0), DEFAULT_PORT);
00082 Node *peer;
00083
00084 socketAddress.setIpAddress(m_args[2].c_str());
00085 if (!m_args[3].empty()) {
00086 socketAddress.setPort(m_args[3].c_str());
00087 }
00088
00089 if ((socketAddress.isZero()) || (socketAddress.getIpAddress().equals((unsigned long)INADDR_ANY))) {
00090 s << "IP address or hostname is bogus.";
00091 return;
00092 }
00093
00094 peer=GlobalObjects::instance()->getCatcher()->lookup(socketAddress.getIpAddress());
00095 if (!peer) {
00096 Node node;
00097 node.setSocketAddress(&socketAddress);
00098 GlobalObjects::instance()->getCatcher()->addNode(&node);
00099 s << "Node added.";
00100 return;
00101 }
00102 else {
00103 s << "Node already existed.";
00104 return;
00105 }
00106 }
00107
00108
00109 void
00110 CatcherCommand::catcherRead(std::ostream& s) {
00111 if (m_args[2].empty()) {
00112 m_args[2] = GlobalObjects::instance()->getConfig()->getPeerListFileName();
00113 }
00114 if (GlobalObjects::instance()->getCatcher()->load(m_args[1].c_str())) {
00115 s << "Reading config succeeded";
00116 }
00117 else {
00118 s << "Reading config failed!";
00119 }
00120 }
00121
00122
00123 void
00124 CatcherCommand::catcherWrite(std::ostream& s) {
00125 string arg;
00126 if (m_args[2].empty()) {
00127 arg = GlobalObjects::instance()->getConfig()->getPeerListFileName();
00128 }
00129 if (GlobalObjects::instance()->getCatcher()->save(arg)) {
00130 s << "Host list written to disk";
00131 }
00132 else {
00133 s << "Writing hosts to disk failed!";
00134 }
00135 }
00136
00137 #endif
00138