00001 #include "headers.h"
00002
00003 const string NodeManagerCommand::commandString = "nodeManager";
00004
00005 const static string addNodeCommandString = "addNode";
00006 const static string modifyNodeCommandString = "modifyNode";
00007
00008
00009 const static string nattedCommandString = "natted";
00010 const static string trustedCommandString = "trusted";
00011 const static string firewalledCommandString = "firewalled";
00012
00013
00014 const static string connectCommandString = "connect";
00015 const static string deleteCommandString = "delete";
00016 const static string editCommandString = "edit";
00017
00018 string
00019 NodeManagerCommand::getCommandString() {
00020 return commandString;
00021 }
00022
00023
00024 void
00025 NodeManagerCommand::getHtmlInterface(std::ostream& s) {
00026 UserConfiguration::pageHeading(s);
00027
00028 s << "<h2>Node Manager</h2>\n\n";
00029
00030
00031 Node node;
00032 s << "<h3>Add a Node</h3>";
00033 UserConfiguration::generateNodeHtmlInterface(s, getCommandString(), addNodeCommandString, &node);
00034
00035
00036 s << "\n\n";
00037 s << "<h3>Known Nodes</h3>";
00038 int numNodes = GlobalObjects::instance()->getCatcher()->getNumberOfNodes();
00039 Catcher* catcher = GlobalObjects::instance()->getCatcher();
00040 if (numNodes == 0) {
00041 s << "None";
00042 }
00043 else {
00044 displayHeaders(s);
00045 Node node;
00046 for (int i = 0; i < numNodes; ++i) {
00047 if (catcher->getNode(i, &node)) {
00048 s << "<tr>";
00049 displayNode(s, &node);
00050 s << "</tr>";
00051 }
00052 }
00053 s << "</table>";
00054 }
00055 }
00056
00057
00058 void
00059 NodeManagerCommand::run(std::ostream& s) {
00060 if (m_args[1].empty()) {
00061 getHtmlInterface(s);
00062 return;
00063 }
00064 if (m_args[1] == addNodeCommandString) {
00065 addNode(s);
00066 }
00067 else if (m_args[1] == connectCommandString) {
00068
00069 IpAddress ip(m_args[2]);
00070 Node* node;
00071 if (node = GlobalObjects::instance()->getCatcher()->lookup(ip)) {
00072
00073 if (GlobalObjects::instance()->getNetworkLayer()->getLli()->connect(node)) {
00074 getHtmlInterface(s);
00075 }
00076 else {
00077 s << "Connection failed";
00078 }
00079 }
00080 else {
00081 s << "Node does not exist";
00082 }
00083 }
00084 else if (m_args[1] == deleteCommandString) {
00085
00086 IpAddress ip(m_args[2]);
00087 Node* node;
00088 if (node = GlobalObjects::instance()->getCatcher()->lookup(ip)) {
00089 if (node->isConnected()) {
00090 s << "You are currently connected to that node. You must disconnect from it before you delete it.";
00091 }
00092 else {
00093 if (GlobalObjects::instance()->getCatcher()->deleteNode(node)) {
00094 getHtmlInterface(s);
00095 }
00096 else {
00097 s << "Failed to delete node - it either does not exist or it is currently connected.";
00098 }
00099 }
00100 }
00101 else {
00102 s << "Node does not exist";
00103 }
00104 }
00105 else if (m_args[1] == editCommandString) {
00106
00107 IpAddress ip(m_args[2]);
00108 Node* node;
00109 if (node = GlobalObjects::instance()->getCatcher()->lookup(ip)) {
00110
00111 UserConfiguration::generateNodeHtmlInterface(s, getCommandString(), modifyNodeCommandString, node);
00112 }
00113 }
00114 else if (m_args[1] == modifyNodeCommandString) {
00115 IpAddress ip(m_args[2]);
00116 Node modNode;
00117 UserConfiguration::setNode(this, s, &modNode);
00118 Node* existingNode;
00119 if (existingNode = GlobalObjects::instance()->getCatcher()->lookup(ip)) {
00120 *existingNode = modNode;
00121 getHtmlInterface(s);
00122 }
00123 else {
00124 s << "Node no longer exists";
00125 }
00126 }
00127 }
00128
00129
00130 void
00131 NodeManagerCommand::addNode(std::ostream& s) {
00132 Node newNode;
00133 if (UserConfiguration::setNode(this, s, &newNode)) {
00134
00135 Node* peer = GlobalObjects::instance()->getCatcher()->lookup(newNode.getSocketAddress()->getIpAddress());
00136 if (peer) {
00137 s << "Node is already in the list.";
00138 return;
00139 }
00140
00141
00142 GlobalObjects::instance()->getCatcher()->addNode(&newNode);
00143
00144
00145 GlobalObjects::instance()->getCatcher()->save();
00146 getHtmlInterface(s);
00147 }
00148 }
00149
00150
00151 void
00152 NodeManagerCommand::displayHeaders(std::ostream& s) {
00153 s << "<table border bordercolor=blue>";
00154 s << "<tr>";
00155 s << "<th>IP</th>";
00156 s << "<th>Port</th>";
00157 s << "<th>Connected?</th>";
00158 s << "<th>Censored?</th>";
00159 s << "<th>NATed?</th>";
00160 s << "<th>Trusted?</th>";
00161 s << "<th>Connect</th>";
00162 s << "<th>Delete</th>";
00163 s << "<th>Edit</th>";
00164
00165 s << "</tr>";
00166 }
00167
00168
00169 void
00170 NodeManagerCommand::displayNode(std::ostream& s, Node* node) {
00171 s << "<td>" << node->getSocketAddress()->getIpAddress() << "</td>";
00172 s << "<td>" << node->getSocketAddress()->getPort() << "</td>";
00173 s << "<td>" << (node->isConnected() ? "Yes" : "No") << "</td>";
00174 s << "<td>" << (node->isFirewalled() ? "Yes" : "No") << "</td>";
00175 s << "<td>" << (node->isNatted() ? "Yes" : "No") << "</td>";
00176 s << "<td>" << (node->isTrusted() ? "Yes" : "No") << "</td>";
00177
00178 s << "<td>";
00179 beginForm(s);
00180 formInput(s, "hidden", "cmd", getCommandString());
00181 formInput(s, "hidden", "subcmd", connectCommandString);
00182 formInput(s, "hidden", "arg1", node->getSocketAddress()->getIpAddress().toString());
00183 formInput(s, "submit", "", "Connect");
00184 endForm(s);
00185 s << "</td>";
00186
00187 s << "<td>";
00188 beginForm(s);
00189 formInput(s, "hidden", "cmd", getCommandString());
00190 formInput(s, "hidden", "subcmd", deleteCommandString);
00191 formInput(s, "hidden", "arg1", node->getSocketAddress()->getIpAddress().toString());
00192 formInput(s, "submit", "", "Delete");
00193 endForm(s);
00194 s << "</td>";
00195
00196 s << "<td>";
00197 beginForm(s);
00198 formInput(s, "hidden", "cmd", getCommandString());
00199 formInput(s, "hidden", "subcmd", editCommandString);
00200 formInput(s, "hidden", "arg1", node->getSocketAddress()->getIpAddress().toString());
00201 formInput(s, "submit", "", "Edit");
00202 endForm(s);
00203 s << "</td>";
00204 }
00205
00206