00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "test.h"
00022
00023 bool runNetInfo();
00024 bool runNetTest();
00025 bool runAdvNetTest();
00026
00027
00028 int main(int argc, char* argv[]) {
00029
00030
00031 JString allArgs;
00032 for (int ii=0; ii<argc; ii++) {
00033 allArgs += JString(argv[ii]) + " ";
00034 }
00035 allArgs.trim();
00036
00037
00038 Dictionary argDict = allArgs.splitCommandLine();
00039
00040 JString arg, val;
00041
00042
00043 arg = argDict.getKey(1);
00044 val = argDict.get(1);
00045
00046 printf("Running test: %s\n\n", (char*) arg);
00047
00048 if (arg.equalsIgnoreCase("netinfo")) {
00049 runNetInfo();
00050 }
00051 else if (arg.equalsIgnoreCase("nettest")) {
00052 runNetTest();
00053 }
00054 else if (arg.equalsIgnoreCase("advnet")) {
00055 runAdvNetTest();
00056 }
00057 else if (arg.equalsIgnoreCase("xxx")) {
00058
00059 }
00060 else {
00061 runNetInfo();
00062 }
00063
00064 return 0;
00065 }
00066
00067
00068
00069
00070
00071
00072 bool runNetInfo() {
00073 JSocket* con = new JSocket("localhost", 25471, "");
00074 Collection addresses = con->getLocalIPAddresses();
00075 printf("NetInfo:\n\nFound %d local IP addresses:\n\t%s\n\n", addresses.getCount(), (char*) addresses.printListLine("\n\t"));
00076 Dictionary interfaces = con->getLocalInterfaceAddresses();
00077 printf("Found %d local interfaces:\n\t%s\n\n", interfaces.getCount(), (char*) interfaces.printListLine("\n\t"));
00078 printf("This computer is '%s' with primary IP address %s\n\n", (char*) con->getLocalHostname(), (char*) con->getLocalIPAddress());
00079 return true;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088 MyReceiver::MyReceiver() {};
00089 MyReceiver::~MyReceiver() {};
00090
00091 JString MyReceiver::getName() {
00092 return "TestServer";
00093 }
00094
00095 Message* MyReceiver::netObjectReceive(Message* msg, NetworkConnection* con) {
00096 Message* retMsg = NULL;
00097 if (msg != NULL) {
00098 printf("MyReceiver received message:\n %s\n\n", (char*) msg->print());
00099 retMsg = new Message("from", "to", "got it");
00100 delete(msg);
00101 }
00102 else {
00103 retMsg = new Message("from", "to", "didn't get it");
00104 }
00105 return retMsg;
00106 };
00107
00108
00109 bool runNetTest() {
00110
00111 MyReceiver* receiver = new MyReceiver();
00112 Network* network = new Network(receiver);
00113
00114 network->addProtocol(new NetMessageProtocol());
00115
00116 if (!network->init(1500))
00117 return false;
00118 network->start();
00119
00120 NetworkConnection* con = new NetworkConnection("localhost", 1500, new NetMessageProtocol(), NULL);
00121 if (!con->isConnected())
00122 return false;
00123
00124 Message* msg = new Message("from", "to", "Hello");
00125 Message* returnMsg = con->sendReceiveObject(msg, 1500);
00126 printf("Main received message:\n %s\n\n", (char *)returnMsg->print());
00127 delete(returnMsg);
00128
00129 msg = new Message("from", "to", "Hello2");
00130 returnMsg = con->sendReceiveObject(msg, 1500);
00131 printf("Main received message:\n %s\n\n", (char *)returnMsg->print());
00132 delete(returnMsg);
00133
00134 msg = new Message("from", "to", "Hello3");
00135 returnMsg = con->sendReceiveObject(msg, 1500);
00136 printf("Main received message:\n %s\n\n", (char *)returnMsg->print());
00137 delete(returnMsg);
00138
00139 con->terminate();
00140 delete(con);
00141
00142 network->terminate();
00143 delete(network);
00144
00145 return true;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 MyAdvReceiver::MyAdvReceiver() {};
00159 MyAdvReceiver::~MyAdvReceiver() {};
00160
00161 JString MyAdvReceiver::getName() {
00162 return "MyServer";
00163 }
00164
00165 Message* MyAdvReceiver::netObjectReceive(Message* msg, NetworkConnection* con) {
00166 Message* outMsg = NULL;
00167 JString from = msg->getFrom();
00168 JString type = msg->getType();
00169 JString content = msg->getContent();
00170 JTime now;
00171
00172 if ((type.equals("HTTP")) && (msg->getObject() != NULL)) {
00173 HTTPRequest* request = (HTTPRequest*) msg->getObject();
00174 if (request != NULL) {
00175 JString rec = request->getEntry("request");
00176 if (rec.endsWith("/"))
00177 rec = rec.substring(1, rec.length()-1);
00178 else
00179 rec = rec.substring(1);
00180 HTMLPage* page = new HTMLPage();
00181 page->title = "MyServer Homepage";
00182 page->body = "<H1>Welcome to MyServer</H1><BR><BR>\n";
00183 page->body += JString::format("You asked for page '%s'<BR>\n", (char*) rec);
00184
00185 Dictionary parms = request->paramdict;
00186 if (parms.getCount() == 0) {
00187 page->body += "Please fill in some text...";
00188 page->body += "<FORM ACTION=\"MyQuestion\" METHOD=\"POST\">Entry 1: <input name=\"entry1\" type=\"text\" size=\"67\"><BR>";
00189 page->body += "Entry 2: <input name=\"entry2\" type=\"text\" size=\"67\">";
00190 page->body += "<input name=\"submit\" type=\"submit\" value=\"submit\"><BR></FORM>";
00191 }
00192 else {
00193 page->body += "Your text was:<br>\n";
00194 for (int n=0; n<parms.getCount(); n++) {
00195 page->body += JString::format("%s = '%s'<BR>\n", (char*) parms.getKey(n), (char*) parms.get(n));
00196 }
00197 page->body += "\nPlease fill in some more text...";
00198 page->body += "<FORM ACTION=\"myquestion\" METHOD=\"POST\">Entry 1: <input name=\"entry1\" type=\"text\" size=\"67\"><BR>";
00199 page->body += "Entry 2: <input name=\"entry2\" type=\"text\" size=\"67\">";
00200 page->body += "<input name=\"submit\" type=\"submit\" value=\"submit\"><BR></FORM>";
00201 }
00202 outMsg = new Message("MyServer", from, type);
00203 outMsg->setObject(page);
00204 }
00205 }
00206 else if (type.equals("TelnetInput")) {
00207 outMsg = new Message("MyServer", "", "");
00208 outMsg->set("NewPromptText", JString::format("MyServer [%s] ",
00209 (char*) now.printTime()));
00210 outMsg->setContent(JString::format("\n\r You said: '%s'\n\r",
00211 (char*) content), "");
00212 }
00213 else {
00214 printf("MyAdvReceiver received message:\n %s\n\n", (char*) msg->print());
00215 outMsg = new Message("from", "to", "got it");
00216 }
00217 delete(msg);
00218 return outMsg;
00219 };
00220
00221
00222 bool runAdvNetTest() {
00223
00224 MyAdvReceiver* receiver = new MyAdvReceiver();
00225 Network* network = new Network(receiver);
00226
00227 network->name = JString::format("MyServer");
00228 network->addProtocol(new NetMessageProtocol());
00229 network->addProtocol(new NetHTTPProtocol());
00230 network->addProtocol(new NetTelnetProtocol("Welcome to the Server", "Prompt > "));
00231 network->setPreferredNetProtocol(new NetMessageProtocol());
00232
00233 if (!network->init(1500))
00234 return false;
00235 network->start();
00236
00237 NetworkConnection* con = new NetworkConnection("localhost", 1500, new NetMessageProtocol(), NULL);
00238 if (!con->isConnected())
00239 return false;
00240
00241 Message* msg = new Message("from", "to", "Hello");
00242 Message* returnMsg = con->sendReceiveObject(msg, 1500);
00243 printf("Main received message:\n %s\n\n", (char *)returnMsg->print());
00244
00245 printf("Now try to connect with either Telnet or a web browswer\n\n telnet localhost 1500 & press <enter>\n\n URL: http://localhost:1500/\n\nThen press CTRL-C to exit\n\n");
00246
00247 while (true) {
00248 network->wait(100);
00249 }
00250
00251 con->terminate();
00252 delete(con);
00253
00254 network->terminate();
00255 delete(network);
00256
00257 return true;
00258 }