Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

LinkLayerCommand.cpp

Go to the documentation of this file.
00001 #ifdef TEST
00002 #include "headers.h"
00003 
00004 static const string llCommandString = "ll";
00005 static const string connectCommandString = "connect";
00006 static const string disconnectCommandString = "disconnect";
00007 static const string listenportCommandString = "listenport";
00008 
00009 string 
00010 LinkLayerCommand::getCommandString() { 
00011     return llCommandString; 
00012 }
00013 
00014 
00015 void
00016 LinkLayerCommand::getHtmlInterface(std::ostream& s) {
00017     s  << "<h2>Link Layer Interface</h2>";
00018     generateHtmlCmd(s, "(help)");
00019     beginUl(s);
00020     generateHtmlSubcmd(s, showCommandString, "show");
00021     generateHtmlSubcmdArg2(s, connectCommandString, "Connect to IP address: ", "Port (optional): ", "Connect");
00022     generateHtmlSubcmdArg1(s, disconnectCommandString, "Disconnect from IP address: ", "Disconnect");
00023     generateHtmlSubcmdArg1(s, listenportCommandString, "Change listening port to: ", "Change");
00024     endUl(s);
00025 }
00026 
00027 
00028 void 
00029 LinkLayerCommand::run(std::ostream& s) {
00030     // check if there is at least one argument.  if not, display help
00031     if (m_numArgs < 1) {
00032         getHelp(s);
00033         return;
00034     }
00035     
00036     if(m_args[1] == showCommandString)
00037     {
00038         s << GlobalObjects::instance()->getNetworkLayer()->getLli()->toString();
00039         return;
00040     }
00041     else if (m_args[1] == connectCommandString) {
00042         connect(s);
00043         return;
00044     }
00045     else if (m_args[1] == disconnectCommandString) {
00046         disconnect(s);
00047         return;
00048     }
00049     else if (m_args[1] == listenportCommandString) {
00050         GlobalObjects::instance()->getNetworkLayer()->getLli()->changeListeningPortTo(atoi(m_args[2].c_str()));
00051         s << "Changed listening port to " << m_args[2];
00052         return;
00053     }
00054     else {
00055         getHelp(s);
00056         return;
00057     }
00058 }
00059 
00060 
00061 void 
00062 LinkLayerCommand::getHelp(std::ostream& s) {
00063     s << "Syntax: " 
00064         << llCommandString << " <subcommand> [arg] -- subcommands:\n"
00065         << " show - print out the connection table\n"
00066         << "    Arguments: NONE\n"
00067         << " " << connectCommandString << " - connect to another node\n"
00068         << "    Arguments: <IP address> [port]\n"
00069         << "    Example: " << llCommandString << " " << connectCommandString << " 1.2.3.4:80\n"
00070         << " " << disconnectCommandString << " - closes connection to a connected node\n"
00071         << "    Arguments: <IP address>\n"
00072         << "    Example: " << llCommandString << " " << disconnectCommandString << " 1.2.3.4\n"
00073         << " " << listenportCommandString << " - change listening port\n"
00074         << "    Arguments: <port number>\n"
00075         << "    Example: " << llCommandString << " " << listenportCommandString << " 8000\n";
00076 }
00077 
00078 
00084 void 
00085 LinkLayerCommand::connect(std::ostream& s)
00086 {
00087     if (m_args[2].empty()) {
00088         getHelp(s);
00089         return;
00090     }
00091 
00092     Node *peer;
00093     SocketAddress socketAddress;
00094     socketAddress.setPort(DEFAULT_PORT);
00095     socketAddress.setIpAddress(IpAddress(m_args[2]));
00096 
00097     // if there is a port parameter
00098     if(!m_args[3].empty()) {
00099         socketAddress.setPort(m_args[3]);
00100     }
00101     
00102     if (socketAddress.getIpAddress().isInAddrAny()) {
00103         s << "IP address or hostname is bogus.";
00104         return;
00105     }
00106     
00107     peer = GlobalObjects::instance()->getCatcher()->lookup(socketAddress.getIpAddress());
00108     if (!peer) {
00109         peer = new Node();
00110         peer->setSocketAddress(&socketAddress);
00111     }
00112 
00113     LinkLayerInterface *myLLI = GlobalObjects::instance()->getNetworkLayer()->getLli();
00114     
00115     if (!myLLI->connect(peer)) {
00116         s << "Connection to " << m_args[2] << " failed.";
00117         return;
00118     }
00119     else {
00120         s << "Connection to " << m_args[2] << " established.";
00121 
00122         // add to the catcher
00123         GlobalObjects::instance()->getCatcher()->addNode(peer);
00124         return;
00125     }
00126 } // fn connect
00127 
00128 
00132 void 
00133 LinkLayerCommand::disconnect(std::ostream& s)
00134 {
00135     IpAddress ip(m_args[1]);
00136     if (ip.isInAddrAny()) {
00137         s << "IP address or hostname is bogus.";
00138         return;
00139     }
00140     
00141     Node *peer = GlobalObjects::instance()->getCatcher()->lookup(ip);
00142     if (!peer) {
00143         s << "Sorry, node was not found in hostlist.";
00144         return;
00145     }
00146     GlobalObjects::instance()->getNetworkLayer()->getLli()->close(peer);
00147     //peer->close();
00148     s << "Node " << m_args[1] << " was disconnected.";
00149 } // fn disconnect
00150 
00151 #endif
00152 
00153 

Generated at Thu Jul 11 13:31:50 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001