00001 #ifdef TEST
00002
00003 #include "headers.h"
00004
00005 static const string nlCommandString = "nl";
00006 static const string vcCommandString = "vc";
00007 static const string finCommandString="fin";
00008 static const string discoverCommandString = "discover";
00009 static const string pingCommandString = "ping";
00010
00011
00012 void
00013 NetworkLayerCommand::getHtmlInterface(std::ostream& s) {
00014 s << "<h2>Network Layer</h2>";
00015 generateHtmlCmd(s, "(help)");
00016 beginUl(s);
00017 generateHtmlSubcmd(s, showCommandString, "show");
00018 generateHtmlSubcmd(s, vcCommandString, "Create a virtual circuit");
00019 generateHtmlSubcmdArg1(s, finCommandString, "Destroy virtual circuit #:", "Destroy");
00020 generateHtmlSubcmd(s, discoverCommandString, "Discover more nodes");
00021 generateHtmlSubcmdArg1(s, pingCommandString, "Ping IP address (must be a node you are connected to): ", "Ping");
00022 endUl(s);
00023 }
00024
00025
00026 string
00027 NetworkLayerCommand::getCommandString() {
00028 return nlCommandString;
00029 }
00030
00031
00032 void
00033 NetworkLayerCommand::run(std::ostream& s) {
00034 if (m_args[1].empty()) {
00035 getHelp(s);
00036 return;
00037 }
00038
00039 if(m_args[1] == showCommandString)
00040 {
00041 s << GlobalObjects::instance()->getNetworkLayer()->toString();
00042 return;
00043 }
00044 else if (m_args[1] == pingCommandString) {
00045 ping(s);
00046 return;
00047 }
00048 else if (m_args[1] == discoverCommandString) {
00049 discover(s);
00050 return;
00051 }
00052 else if (m_args[1] == vcCommandString) {
00053 vc(s);
00054 return;
00055 }
00056 else if (m_args[1] == finCommandString) {
00057 fin(s);
00058 return;
00059 }
00060 else {
00061 getHelp(s);
00062 return;
00063 }
00064 }
00065
00066
00067 void
00068 NetworkLayerCommand::getHelp(std::ostream& s) {
00069 s << "Syntax: "
00070 << nlCommandString << " <subcommand> [arg] -- subcommands:\n"
00071 << " " << showCommandString << " - show network layer state\n"
00072 << " " << pingCommandString << " - ping a node you are connected to an measure RTT\n"
00073 << " Arguments: IP_address\n"
00074 << " " << discoverCommandString << " - tries to discover more hosts\n"
00075 << " Arguments: NONE\n"
00076 << " " << vcCommandString << " - create a virtual circuit\n"
00077 << " Arguments: NONE\n"
00078 << " " << finCommandString << " - destroy a virtual circuit\n"
00079 << " Arguments: VirtualCircuitNumber\n";
00080 }
00081
00082
00087 void
00088 NetworkLayerCommand::ping(std::ostream& s) {
00089 if (!GlobalObjects::instance()->getNetworkLayer()->getLli()->isConnected()) {
00090 s << "Not connected to any nodes\n";
00091 return;
00092 }
00093
00094 GlobalObjects::instance()->getNetworkLayer()->ping();
00095 s << "Node List:\n" << *GlobalObjects::instance()->getCatcher() << "\n";
00096 }
00097
00098
00103 void
00104 NetworkLayerCommand::discover(std::ostream& s)
00105 {
00106 DiscoveryPacket *discovery = new DiscoveryPacket();
00107 MEMCHECK(discovery);
00108 LinkLayerInterface *myLli = GlobalObjects::instance()->getNetworkLayer()->getLli();
00109 Node *randomNode = myLli->getRandomNeighbor();
00110 if(!randomNode) {
00111 s << "No neighbors to send to.";
00112 return;
00113 }
00114 int retVal = myLli->sendPacket(discovery,randomNode);
00115 delete discovery;
00116 if (retVal < 0) {
00117 s << "There was a problem while sending the packet.";
00118 return;
00119 }
00120 else {
00121 s << "Discovery successful";
00122 return;
00123 }
00124 }
00125
00126
00130 void
00131 NetworkLayerCommand::vc(std::ostream& s) {
00132 if (GlobalObjects::instance()->getNetworkLayer()->makeVc(0) != 0) {
00133 s << "Virtual circuit created\n";
00134 return;
00135 }
00136 else {
00137 s << "Failed to create virtual circuit\n";
00138 return;
00139 }
00140 }
00141
00142
00143 void
00144 NetworkLayerCommand::fin(std::ostream& s) {
00145 if (m_args[2].empty()) {
00146 s << "You must specify a VCN to destroy.\nNote: you must be the originator or the terminator of the connection.";
00147 return;
00148 }
00149
00150 int vcn = atoi(m_args[2].c_str());
00151 GlobalObjects::instance()->getNetworkLayer()->destroyVc(vcn);
00152 s << "Detroyed VCN " << m_args[2] << "\n";
00153 }
00154
00155 #endif
00156
00157
00158