| 1 |
#ifdef SIMULATE |
| 2 |
#include "nsProtoSimAgent.h" |
| 3 |
#else |
| 4 |
#include "protoApp.h" |
| 5 |
#include "protoNet.h" |
| 6 |
#include "protoRouteMgr.h" |
| 7 |
#endif // if/else SIMULATE |
| 8 |
|
| 9 |
#include <stdlib.h> // for atoi() |
| 10 |
#include <stdio.h> // for stdout/stderr printouts |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
class ProtoExample : |
| 14 |
#ifdef SIMULATE |
| 15 |
public NsProtoSimAgent |
| 16 |
#else |
| 17 |
public ProtoApp |
| 18 |
#endif // if/else SIMULATE |
| 19 |
|
| 20 |
{ |
| 21 |
public: |
| 22 |
ProtoExample(); |
| 23 |
~ProtoExample(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Override from ProtoApp or NsProtoSimAgent base |
| 27 |
*/ |
| 28 |
bool OnStartup(int argc, const char*const* argv); |
| 29 |
/** |
| 30 |
* Override from ProtoApp or NsProtoSimAgent base |
| 31 |
*/ |
| 32 |
bool ProcessCommands(int argc, const char*const* argv); |
| 33 |
/** |
| 34 |
* Override from ProtoApp or NsProtoSimAgent base |
| 35 |
*/ |
| 36 |
void OnShutdown(); |
| 37 |
/** |
| 38 |
* Override from ProtoApp or NsProtoSimAgent base |
| 39 |
*/ |
| 40 |
virtual bool HandleMessage(unsigned int len, const char* txBuffer,const ProtoAddress& srcAddr) {return true;} |
| 41 |
|
| 42 |
private: |
| 43 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 44 |
static CmdType GetCmdType(const char* string); |
| 45 |
bool OnCommand(const char* cmd, const char* val); |
| 46 |
void Usage(); |
| 47 |
|
| 48 |
void OnTxTimeout(ProtoTimer& theTimer); |
| 49 |
void OnUdpSocketEvent(ProtoSocket& theSocket, |
| 50 |
ProtoSocket::Event theEvent); |
| 51 |
void OnClientSocketEvent(ProtoSocket& theSocket, |
| 52 |
ProtoSocket::Event theEvent); |
| 53 |
void OnServerSocketEvent(ProtoSocket& theSocket, |
| 54 |
ProtoSocket::Event theEvent); |
| 55 |
static const char* const CMD_LIST[]; |
| 56 |
static void SignalHandler(int sigNum); |
| 57 |
|
| 58 |
// ProtoTimer/ UDP socket demo members |
| 59 |
ProtoTimer tx_timer; |
| 60 |
ProtoAddress dst_addr; |
| 61 |
|
| 62 |
ProtoSocket udp_tx_socket; |
| 63 |
ProtoSocket udp_rx_socket; |
| 64 |
|
| 65 |
// TCP socket demo members |
| 66 |
ProtoSocket server_socket; |
| 67 |
ProtoSocket client_socket; |
| 68 |
unsigned int client_msg_count; |
| 69 |
ProtoSocket::List connection_list; |
| 70 |
bool use_html; |
| 71 |
|
| 72 |
}; // end class ProtoExample |
| 73 |
|
| 74 |
|
| 75 |
// (TBD) Note this #if/else code could be replaced with something like |
| 76 |
// a PROTO_INSTANTIATE(ProtoExample) macro defined differently |
| 77 |
// in "protoApp.h" and "nsProtoSimAgent.h" |
| 78 |
#ifdef SIMULATE |
| 79 |
#ifdef NS2 |
| 80 |
static class NsProtoExampleClass : public TclClass |
| 81 |
{ |
| 82 |
public: |
| 83 |
NsProtoExampleClass() : TclClass("Agent/ProtoExample") {} |
| 84 |
TclObject *create(int argc, const char*const* argv) |
| 85 |
{return (new ProtoExample());} |
| 86 |
} class_proto_example; |
| 87 |
#endif // NS2 |
| 88 |
|
| 89 |
|
| 90 |
#else |
| 91 |
|
| 92 |
// Our application instance |
| 93 |
PROTO_INSTANTIATE_APP(ProtoExample) |
| 94 |
|
| 95 |
#endif // SIMULATE |
| 96 |
|
| 97 |
ProtoExample::ProtoExample() |
| 98 |
: udp_tx_socket(ProtoSocket::UDP), |
| 99 |
udp_rx_socket(ProtoSocket::UDP), |
| 100 |
server_socket(ProtoSocket::TCP), |
| 101 |
client_socket(ProtoSocket::TCP), |
| 102 |
client_msg_count(0), use_html(false) |
| 103 |
{ |
| 104 |
tx_timer.SetListener(this, &ProtoExample::OnTxTimeout); |
| 105 |
udp_tx_socket.SetNotifier(&GetSocketNotifier()); |
| 106 |
udp_tx_socket.SetListener(this, &ProtoExample::OnUdpSocketEvent); |
| 107 |
udp_rx_socket.SetNotifier(&GetSocketNotifier()); |
| 108 |
udp_rx_socket.SetListener(this, &ProtoExample::OnUdpSocketEvent); |
| 109 |
client_socket.SetNotifier(&GetSocketNotifier()); |
| 110 |
client_socket.SetListener(this, &ProtoExample::OnClientSocketEvent); |
| 111 |
server_socket.SetNotifier(&GetSocketNotifier()); |
| 112 |
server_socket.SetListener(this, &ProtoExample::OnServerSocketEvent); |
| 113 |
} |
| 114 |
|
| 115 |
ProtoExample::~ProtoExample() |
| 116 |
{ |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
void ProtoExample::Usage() |
| 121 |
{ |
| 122 |
fprintf(stderr, "protoExample [send <host>/<port>] [recv [<group>/]<port>]\n" |
| 123 |
" connect <host>/<port>] [listen <port>][html]\n"); |
| 124 |
} // end ProtoExample::Usage() |
| 125 |
|
| 126 |
|
| 127 |
const char* const ProtoExample::CMD_LIST[] = |
| 128 |
{ |
| 129 |
"+send", // Send UDP packets to destination host/port |
| 130 |
"+recv", // Recv UDP packets on <port> (<group> addr optional) |
| 131 |
"+connect", // TCP connect and send to destination host/port |
| 132 |
"+listen", // Listen for TCP connections on <port> |
| 133 |
"-html", // listening TCP server provides canned HTML response to client(s) (use with "listen" option) |
| 134 |
NULL |
| 135 |
}; |
| 136 |
|
| 137 |
ProtoExample::CmdType ProtoExample::GetCmdType(const char* cmd) |
| 138 |
{ |
| 139 |
if (!cmd) return CMD_INVALID; |
| 140 |
unsigned int len = strlen(cmd); |
| 141 |
bool matched = false; |
| 142 |
CmdType type = CMD_INVALID; |
| 143 |
const char* const* nextCmd = CMD_LIST; |
| 144 |
while (*nextCmd) |
| 145 |
{ |
| 146 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 147 |
{ |
| 148 |
if (matched) |
| 149 |
{ |
| 150 |
// ambiguous command (command should match only once) |
| 151 |
return CMD_INVALID; |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
matched = true; |
| 156 |
if ('+' == *nextCmd[0]) |
| 157 |
type = CMD_ARG; |
| 158 |
else |
| 159 |
type = CMD_NOARG; |
| 160 |
} |
| 161 |
} |
| 162 |
nextCmd++; |
| 163 |
} |
| 164 |
return type; |
| 165 |
} // end ProtoExample::GetCmdType() |
| 166 |
|
| 167 |
bool ProtoExample::OnStartup(int argc, const char*const* argv) |
| 168 |
{ |
| 169 |
#ifdef _WIN32_WCE |
| 170 |
if (!OnCommand("recv", "224.225.1.2/5002")) |
| 171 |
{ |
| 172 |
TRACE("Error with \"recv\" command ...\n"); |
| 173 |
} |
| 174 |
if (!OnCommand("send", "224.225.1.2/5002")) |
| 175 |
{ |
| 176 |
TRACE("Error with \"send\" command ...\n"); |
| 177 |
} |
| 178 |
#endif // _WIN32_WCE |
| 179 |
|
| 180 |
if (!ProcessCommands(argc, argv)) |
| 181 |
{ |
| 182 |
PLOG(PL_ERROR, "protoExample: Error! bad command line\n"); |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
#ifndef SIMULATE |
| 187 |
|
| 188 |
// Here's some code to test the ProtoSocket routines for network interface info |
| 189 |
ProtoAddress localAddress; |
| 190 |
char nameBuffer[256]; |
| 191 |
nameBuffer[255] = '\0'; |
| 192 |
|
| 193 |
// ljt test |
| 194 |
ProtoAddress dst; |
| 195 |
dst.ResolveFromString("fe80::426c:8fff:fe31:4f8e"); |
| 196 |
if (dst.ResolveToName(nameBuffer,255)) |
| 197 |
TRACE("protoExample: LJT ipv6 host name: %s\n", nameBuffer); |
| 198 |
|
| 199 |
|
| 200 |
if (localAddress.ResolveLocalAddress()) |
| 201 |
{ |
| 202 |
TRACE("protoExample: local default IP address: %s\n", localAddress.GetHostString()); |
| 203 |
if (localAddress.ResolveToName(nameBuffer, 255)) |
| 204 |
TRACE("protoExample: local default host name: %s\n", nameBuffer); |
| 205 |
else |
| 206 |
TRACE("protoExample: unable to resolve local default IP address to name\n"); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
TRACE("protoExample: unable to determine local default IP address\n"); |
| 211 |
} |
| 212 |
|
| 213 |
// Get all of our ifaces and dump some info (name, MAC addr, IP addrs, etc) |
| 214 |
unsigned int ifaceCount = ProtoNet::GetInterfaceCount(); |
| 215 |
if (ifaceCount > 0) |
| 216 |
{ |
| 217 |
// Allocate array to hold the indices |
| 218 |
unsigned int* indexArray = new unsigned int[ifaceCount]; |
| 219 |
if (NULL == indexArray) |
| 220 |
{ |
| 221 |
PLOG(PL_ERROR, "protoExample: new indexArray[%u] error: %s\n", ifaceCount, GetErrorString()); |
| 222 |
return false; |
| 223 |
} |
| 224 |
if (ProtoNet::GetInterfaceIndices(indexArray, ifaceCount) != ifaceCount) |
| 225 |
{ |
| 226 |
PLOG(PL_ERROR, "protoExample: GetInterfaceIndices() error?!\n"); |
| 227 |
delete[] indexArray; |
| 228 |
return false; |
| 229 |
} |
| 230 |
for (unsigned int i = 0; i < ifaceCount; i++) |
| 231 |
{ |
| 232 |
unsigned int index = indexArray[i]; |
| 233 |
TRACE("protoExample: interface index %u ", index); |
| 234 |
// Get the iface name |
| 235 |
if (ProtoNet::GetInterfaceName(index, nameBuffer, 255)) |
| 236 |
TRACE("has name \"%s\" ", nameBuffer); |
| 237 |
else |
| 238 |
TRACE("name \"unknown\" "); |
| 239 |
// Get MAC addr for the iface |
| 240 |
ProtoAddress ifaceAddr; |
| 241 |
if (ProtoNet::GetInterfaceAddress(nameBuffer, ProtoAddress::ETH, ifaceAddr)) |
| 242 |
TRACE("with MAC addr %s\n", ifaceAddr.GetHostString()); |
| 243 |
else |
| 244 |
TRACE("with no MAC addr\n"); |
| 245 |
|
| 246 |
ProtoAddressList addrList; |
| 247 |
// Get IPv4 addrs for the iface |
| 248 |
if (ProtoNet::GetInterfaceAddressList(nameBuffer, ProtoAddress::IPv4, addrList)) |
| 249 |
{ |
| 250 |
TRACE(" IPv4 addresses:"); |
| 251 |
ProtoAddressList::Iterator iterator(addrList); |
| 252 |
while (iterator.GetNextAddress(ifaceAddr)) |
| 253 |
{ |
| 254 |
TRACE(" %s/%d", ifaceAddr.GetHostString(),ProtoNet::GetInterfaceAddressMask(nameBuffer, ifaceAddr)); |
| 255 |
if (ifaceAddr.IsLinkLocal()) TRACE(" (link local)"); |
| 256 |
} |
| 257 |
TRACE("\n"); |
| 258 |
} |
| 259 |
addrList.Destroy(); |
| 260 |
// Get IPv6 addrs for the iface |
| 261 |
if (ProtoNet::GetInterfaceAddressList(nameBuffer, ProtoAddress::IPv6, addrList)) |
| 262 |
{ |
| 263 |
TRACE(" IPv6 addresses:"); |
| 264 |
ProtoAddressList::Iterator iterator(addrList); |
| 265 |
while (iterator.GetNextAddress(ifaceAddr)) |
| 266 |
{ |
| 267 |
|
| 268 |
TRACE(" %s/%d", ifaceAddr.GetHostString(), ProtoNet::GetInterfaceAddressMask(nameBuffer, ifaceAddr)); |
| 269 |
|
| 270 |
if (ifaceAddr.IsLinkLocal()) TRACE(" (link local)"); |
| 271 |
} |
| 272 |
TRACE("\n"); |
| 273 |
} |
| 274 |
addrList.Destroy(); |
| 275 |
|
| 276 |
// Test our interface add routine |
| 277 |
/* |
| 278 |
char * ifaceName = "192.168.1.6"; |
| 279 |
ProtoAddress tmpAddr; |
| 280 |
tmpAddr.ConvertFromString("192.168.1.6"); |
| 281 |
int maskLen = 24; |
| 282 |
bool result = ProtoNet::AddInterfaceAddress(ifaceName, tmpAddr, maskLen); |
| 283 |
*/ |
| 284 |
|
| 285 |
|
| 286 |
#ifndef WIN32 |
| 287 |
// This code should work for Linux and BSD (incl. Mac OSX) |
| 288 |
// Get IPv4 group memberships for interfaces |
| 289 |
if (ProtoNet::GetGroupMemberships(nameBuffer, ProtoAddress::IPv4, addrList)) |
| 290 |
{ |
| 291 |
TRACE(" IPv4 memberships:"); |
| 292 |
ProtoAddressList::Iterator iterator(addrList); |
| 293 |
ProtoAddress groupAddr; |
| 294 |
while (iterator.GetNextAddress(groupAddr)) |
| 295 |
{ |
| 296 |
TRACE(" %s", groupAddr.GetHostString()); |
| 297 |
} |
| 298 |
TRACE("\n"); |
| 299 |
} |
| 300 |
addrList.Destroy(); |
| 301 |
if (ProtoNet::GetGroupMemberships(nameBuffer, ProtoAddress::IPv6, addrList)) |
| 302 |
{ |
| 303 |
TRACE(" IPv6 memberships:"); |
| 304 |
ProtoAddressList::Iterator iterator(addrList); |
| 305 |
ProtoAddress groupAddr; |
| 306 |
while (iterator.GetNextAddress(groupAddr)) |
| 307 |
{ |
| 308 |
TRACE(" %s", groupAddr.GetHostString()); |
| 309 |
} |
| 310 |
TRACE("\n"); |
| 311 |
} |
| 312 |
addrList.Destroy(); |
| 313 |
#endif // WIN32 |
| 314 |
} |
| 315 |
delete[] indexArray; |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
TRACE("protoExample: host has no network interfaces?!\n"); |
| 320 |
} |
| 321 |
// Here's some code to get the system routing table |
| 322 |
ProtoRouteTable routeTable; |
| 323 |
ProtoRouteMgr* routeMgr = ProtoRouteMgr::Create(); |
| 324 |
if (NULL == routeMgr) |
| 325 |
{ |
| 326 |
PLOG(PL_ERROR, "ProtoExample::OnStartup() error creating route manager\n"); |
| 327 |
return false; |
| 328 |
} |
| 329 |
if (!routeMgr->Open()) |
| 330 |
{ |
| 331 |
PLOG(PL_ERROR, "ProtoExample::OnStartup() error opening route manager\n"); |
| 332 |
return false; |
| 333 |
} |
| 334 |
if (!routeMgr->GetAllRoutes(ProtoAddress::IPv4, routeTable)) |
| 335 |
PLOG(PL_ERROR, "ProtoExample::OnStartup() warning getting system routes\n"); |
| 336 |
// Display whatever routes we got |
| 337 |
ProtoRouteTable::Iterator iterator(routeTable); |
| 338 |
ProtoRouteTable::Entry* entry; |
| 339 |
PLOG(PL_ALWAYS, "IPv4 Routing Table:\n"); |
| 340 |
PLOG(PL_ALWAYS, "%16s/Prefix %-12s ifIndex Metric\n", "Destination", "Gateway"); |
| 341 |
while (NULL != (entry = iterator.GetNextEntry())) |
| 342 |
{ |
| 343 |
PLOG(PL_ALWAYS, "%16s/%-3u ", |
| 344 |
entry->GetDestination().GetHostString(), entry->GetPrefixSize()); |
| 345 |
const ProtoAddress& gw = entry->GetGateway(); |
| 346 |
PLOG(PL_ALWAYS, "%-16s %-02u %d\n", |
| 347 |
gw.IsValid() ? gw.GetHostString() : "0.0.0.0", |
| 348 |
entry->GetInterfaceIndex(), |
| 349 |
entry->GetMetric()); |
| 350 |
} |
| 351 |
//ProtoAddress dst; |
| 352 |
//dst.ResolveFromString("10.1.2.3"); |
| 353 |
//ProtoAddress gw; |
| 354 |
//routeMgr->SetRoute(dst, 32, gw, 4, 0); |
| 355 |
routeMgr->Close(); |
| 356 |
|
| 357 |
fprintf(stderr, "\nprotoExample: entering main loop (<CTRL-C> to exit)...\n"); |
| 358 |
#endif // !SIMULATE |
| 359 |
return true; |
| 360 |
} // end ProtoExample::OnStartup() |
| 361 |
|
| 362 |
void ProtoExample::OnShutdown() |
| 363 |
{ |
| 364 |
if (tx_timer.IsActive()) tx_timer.Deactivate(); |
| 365 |
if (udp_tx_socket.IsOpen()) udp_tx_socket.Close(); |
| 366 |
if (udp_rx_socket.IsOpen()) udp_rx_socket.Close(); |
| 367 |
if (server_socket.IsOpen()) server_socket.Close(); |
| 368 |
if (client_socket.IsOpen()) client_socket.Close(); |
| 369 |
connection_list.Destroy(); |
| 370 |
#ifndef SIMULATE |
| 371 |
PLOG(PL_ERROR, "protoExample: Done.\n"); |
| 372 |
#endif // SIMULATE |
| 373 |
CloseDebugLog(); |
| 374 |
} // end ProtoExample::OnShutdown() |
| 375 |
|
| 376 |
bool ProtoExample::ProcessCommands(int argc, const char*const* argv) |
| 377 |
{ |
| 378 |
// Dispatch command-line commands to our OnCommand() method |
| 379 |
int i = 1; |
| 380 |
while ( i < argc) |
| 381 |
{ |
| 382 |
// Is it a class ProtoExample command? |
| 383 |
switch (GetCmdType(argv[i])) |
| 384 |
{ |
| 385 |
case CMD_INVALID: |
| 386 |
{ |
| 387 |
#ifndef SIMULATE |
| 388 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommands() Invalid command:%s\n", |
| 389 |
argv[i]); |
| 390 |
#endif // SIMULATE |
| 391 |
return false; |
| 392 |
} |
| 393 |
case CMD_NOARG: |
| 394 |
if (!OnCommand(argv[i], NULL)) |
| 395 |
{ |
| 396 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommands() ProcessCommand(%s) error\n", |
| 397 |
argv[i]); |
| 398 |
return false; |
| 399 |
} |
| 400 |
i++; |
| 401 |
break; |
| 402 |
case CMD_ARG: |
| 403 |
if (!OnCommand(argv[i], argv[i+1])) |
| 404 |
{ |
| 405 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommands() ProcessCommand(%s, %s) error\n", |
| 406 |
argv[i], argv[i+1]); |
| 407 |
return false; |
| 408 |
} |
| 409 |
i += 2; |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
return true; |
| 414 |
} // end ProtoExample::ProcessCommands() |
| 415 |
|
| 416 |
bool ProtoExample::OnCommand(const char* cmd, const char* val) |
| 417 |
{ |
| 418 |
|
| 419 |
// (TBD) move command processing into Mgen class ??? |
| 420 |
CmdType type = GetCmdType(cmd); |
| 421 |
ASSERT(CMD_INVALID != type); |
| 422 |
unsigned int len = strlen(cmd); |
| 423 |
if ((CMD_ARG == type) && !val) |
| 424 |
{ |
| 425 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(%s) missing argument\n", cmd); |
| 426 |
return false; |
| 427 |
} |
| 428 |
else if (!strncmp("send", cmd, len)) |
| 429 |
{ |
| 430 |
if (!udp_tx_socket.Open(0, ProtoAddress::IPv4, false)) |
| 431 |
{ |
| 432 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(send) error opening udp_tx_socket\n"); |
| 433 |
return false; |
| 434 |
} |
| 435 |
char string[256]; |
| 436 |
strncpy(string, val, 256); |
| 437 |
string[255] ='\0'; |
| 438 |
char* ptr = strchr(string, '/'); |
| 439 |
if (ptr) |
| 440 |
{ |
| 441 |
*ptr++ = '\0'; |
| 442 |
if (!dst_addr.ResolveFromString(string)) |
| 443 |
{ |
| 444 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(send) error: invalid <host>\n"); |
| 445 |
return false; |
| 446 |
} |
| 447 |
dst_addr.SetPort(atoi(ptr)); |
| 448 |
udp_tx_socket.Connect(dst_addr); |
| 449 |
tx_timer.SetInterval(1.0); |
| 450 |
tx_timer.SetRepeat(-1); |
| 451 |
OnTxTimeout(tx_timer); |
| 452 |
if (!tx_timer.IsActive()) ActivateTimer(tx_timer); |
| 453 |
} |
| 454 |
else |
| 455 |
{ |
| 456 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(send) error: <port> not specified\n"); |
| 457 |
return false; |
| 458 |
} |
| 459 |
} |
| 460 |
else if (!strncmp("recv", cmd, len)) |
| 461 |
{ |
| 462 |
char string[256]; |
| 463 |
strncpy(string, val, 256); |
| 464 |
string[255] ='\0'; |
| 465 |
char* portPtr = strchr(string, '/'); |
| 466 |
ProtoAddress groupAddr; |
| 467 |
if (portPtr) |
| 468 |
{ |
| 469 |
*portPtr++ = '\0'; |
| 470 |
if (!groupAddr.ResolveFromString(string)) |
| 471 |
{ |
| 472 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error: invalid <groupAddr>\n"); |
| 473 |
return false; |
| 474 |
} |
| 475 |
} |
| 476 |
else |
| 477 |
{ |
| 478 |
portPtr = string; |
| 479 |
} |
| 480 |
UINT16 thePort; |
| 481 |
if (1 == sscanf(portPtr, "%hu", &thePort)) |
| 482 |
{ |
| 483 |
|
| 484 |
if (!udp_rx_socket.Open(0, ProtoAddress::IPv4, false)) |
| 485 |
{ |
| 486 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error opening udp_rx_socket\n"); |
| 487 |
return false; |
| 488 |
} |
| 489 |
udp_rx_socket.EnableRecvDstAddr(); // for testing this feature |
| 490 |
if (groupAddr.IsValid() && groupAddr.IsMulticast()) |
| 491 |
{ |
| 492 |
|
| 493 |
bool result = udp_rx_socket.SetReuse(true); |
| 494 |
TRACE("set port reuse result %d...\n", result); |
| 495 |
} |
| 496 |
if (!udp_rx_socket.Bind(thePort)) |
| 497 |
{ |
| 498 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error binding udp_rx_socket\n"); |
| 499 |
return false; |
| 500 |
} |
| 501 |
if (groupAddr.IsValid() && groupAddr.IsMulticast()) |
| 502 |
{ |
| 503 |
if (!(udp_rx_socket.JoinGroup(groupAddr))) |
| 504 |
{ |
| 505 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error joining group\n"); |
| 506 |
udp_rx_socket.Close(); |
| 507 |
return false; |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(recv) error: <port> not specified\n"); |
| 514 |
return false; |
| 515 |
} |
| 516 |
} |
| 517 |
else if (!strncmp("connect", cmd, len)) |
| 518 |
{ |
| 519 |
char string[256]; |
| 520 |
strncpy(string, val, 256); |
| 521 |
string[255] ='\0'; |
| 522 |
char* ptr = strchr(string, '/'); |
| 523 |
if (ptr) |
| 524 |
{ |
| 525 |
*ptr++ = '\0'; |
| 526 |
ProtoAddress server; |
| 527 |
if (!server.ResolveFromString(string)) |
| 528 |
{ |
| 529 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error: invalid <host>\n"); |
| 530 |
return false; |
| 531 |
} |
| 532 |
server.SetPort(atoi(ptr)); |
| 533 |
TRACE("calling connect ...\n"); |
| 534 |
if (!client_socket.Connect(server)) |
| 535 |
{ |
| 536 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error connecting\n"); |
| 537 |
return false; |
| 538 |
} |
| 539 |
client_msg_count = 5; // (Send 5 messages, then disconnect) |
| 540 |
TRACE(" (completed. starting output notification ...\n"); |
| 541 |
client_socket.StartOutputNotification(); |
| 542 |
} |
| 543 |
else |
| 544 |
{ |
| 545 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error: <port> not specified\n"); |
| 546 |
return false; |
| 547 |
} |
| 548 |
} |
| 549 |
else if (!strncmp("listen", cmd, len)) |
| 550 |
{ |
| 551 |
UINT16 thePort; |
| 552 |
if (1 == sscanf(val, "%hu", &thePort)) |
| 553 |
{ |
| 554 |
if (!server_socket.Listen(thePort)) |
| 555 |
{ |
| 556 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(listen) error listening\n"); |
| 557 |
return false; |
| 558 |
} |
| 559 |
} |
| 560 |
else |
| 561 |
{ |
| 562 |
PLOG(PL_ERROR, "ProtoExample::ProcessCommand(connect) error: <port> not specified\n"); |
| 563 |
return false; |
| 564 |
} |
| 565 |
} |
| 566 |
else if (!strncmp("html", cmd, len)) |
| 567 |
{ |
| 568 |
use_html = true; |
| 569 |
} |
| 570 |
else if (!strncmp("background", cmd, len)) |
| 571 |
{ |
| 572 |
// do nothing (this command was scanned immediately at startup) |
| 573 |
} |
| 574 |
return true; |
| 575 |
} // end ProtoExample::OnCommand() |
| 576 |
|
| 577 |
void ProtoExample::OnTxTimeout(ProtoTimer& /*theTimer*/) |
| 578 |
{ |
| 579 |
const char* string = "Hello there UDP peer, how are you doing?"; |
| 580 |
unsigned int len = strlen(string) + 1; |
| 581 |
unsigned int numBytes = len; |
| 582 |
|
| 583 |
if (!udp_tx_socket.SendTo(string, numBytes, dst_addr)) |
| 584 |
{ |
| 585 |
PLOG(PL_ERROR, "ProtoExample::OnTxTimeout() error sending to %s/%hu ...\n", |
| 586 |
dst_addr.GetHostString(), dst_addr.GetPort()); |
| 587 |
} |
| 588 |
else if (len != numBytes) |
| 589 |
{ |
| 590 |
PLOG(PL_ERROR, "ProtoExample::OnTxTimeout() incomplete SendTo()\n"); |
| 591 |
TRACE(" (only sent %lu of %lu bytes)\n", numBytes, len); |
| 592 |
} |
| 593 |
|
| 594 |
} // end ProtoExample::OnTxTimeout() |
| 595 |
|
| 596 |
void ProtoExample::OnUdpSocketEvent(ProtoSocket& theSocket, |
| 597 |
ProtoSocket::Event theEvent) |
| 598 |
{ |
| 599 |
if (&theSocket == &udp_tx_socket) |
| 600 |
TRACE("ProtoExample::OnUdpTxSocketEvent("); |
| 601 |
else |
| 602 |
TRACE("ProtoExample::OnUdpRxSocketEvent("); |
| 603 |
switch (theEvent) |
| 604 |
{ |
| 605 |
case ProtoSocket::INVALID_EVENT: |
| 606 |
TRACE("ERROR) ...\n"); |
| 607 |
break; |
| 608 |
case ProtoSocket::CONNECT: |
| 609 |
TRACE("CONNECT) ...\n"); |
| 610 |
break; |
| 611 |
case ProtoSocket::ACCEPT: |
| 612 |
TRACE("ACCEPT) ...\n"); |
| 613 |
break; |
| 614 |
case ProtoSocket::SEND: |
| 615 |
{ |
| 616 |
TRACE("SEND) ...\n"); |
| 617 |
break; |
| 618 |
} |
| 619 |
case ProtoSocket::RECV: |
| 620 |
{ |
| 621 |
static unsigned long count = 0; |
| 622 |
TRACE("RECV) ...\n"); |
| 623 |
ProtoAddress srcAddr, dstAddr; |
| 624 |
char buffer[1024]; |
| 625 |
unsigned int len = 1024; |
| 626 |
// This RecvFrom() call w/ dstAddr may not be suppoted on WIN32 yet |
| 627 |
if (theSocket.RecvFrom(buffer, len, srcAddr, dstAddr)) |
| 628 |
{ |
| 629 |
buffer[len] = '\0'; |
| 630 |
if (len) |
| 631 |
{ |
| 632 |
char dstAddrString[32]; |
| 633 |
dstAddr.GetHostString(dstAddrString, 32); |
| 634 |
TRACE("ProtoExample::OnUdpSocketEvent(%lu) received \"%s\" from %s to dest %s\n", |
| 635 |
count++, buffer, srcAddr.GetHostString(), dstAddrString); |
| 636 |
} |
| 637 |
else |
| 638 |
{ |
| 639 |
TRACE("ProtoExample::OnUdpSocketEvent() received 0 bytes\n"); |
| 640 |
} |
| 641 |
} |
| 642 |
else |
| 643 |
{ |
| 644 |
PLOG(PL_ERROR, "ProtoExample::OnUdpSocketEvent() error receiving!\n"); |
| 645 |
} |
| 646 |
break; |
| 647 |
} |
| 648 |
case ProtoSocket::DISCONNECT: |
| 649 |
TRACE("DISCONNECT) ...\n"); |
| 650 |
break; |
| 651 |
|
| 652 |
case ProtoSocket::EXCEPTION: |
| 653 |
TRACE("EXCEPTION) ...\n"); |
| 654 |
break; |
| 655 |
|
| 656 |
case ProtoSocket::ERROR_: |
| 657 |
TRACE("ERROR_) ...\n"); |
| 658 |
break; |
| 659 |
} |
| 660 |
} // end ProtoExample::OnUdpSocketEvent() |
| 661 |
|
| 662 |
|
| 663 |
void ProtoExample::OnClientSocketEvent(ProtoSocket& theSocket, |
| 664 |
ProtoSocket::Event theEvent) |
| 665 |
{ |
| 666 |
TRACE("ProtoExample::OnClientSocketEvent("); |
| 667 |
switch (theEvent) |
| 668 |
{ |
| 669 |
case ProtoSocket::INVALID_EVENT: |
| 670 |
TRACE("ERROR) ...\n"); |
| 671 |
break; |
| 672 |
case ProtoSocket::CONNECT: |
| 673 |
TRACE("CONNECT) ...\n"); |
| 674 |
break; |
| 675 |
case ProtoSocket::ACCEPT: |
| 676 |
TRACE("ACCEPT) ...\n"); |
| 677 |
break; |
| 678 |
case ProtoSocket::SEND: |
| 679 |
{ |
| 680 |
TRACE("SEND) ...\n"); |
| 681 |
if (0 == client_msg_count) |
| 682 |
{ |
| 683 |
TRACE("protoExample: client message transmission completed.\n"); |
| 684 |
client_socket.StopOutputNotification(); // don't send any more |
| 685 |
// Uncomment this to have the client cleanly disconnect after transmission |
| 686 |
// (otherwise, linger for server response until user "Ctrl-C" exit) |
| 687 |
client_socket.Shutdown(); |
| 688 |
return; |
| 689 |
} |
| 690 |
const char* string = "Hello there ProtoServer, how are you doing?"; |
| 691 |
unsigned int len = strlen(string) + 1; |
| 692 |
unsigned int numBytes = len; |
| 693 |
client_msg_count--; |
| 694 |
if (!client_socket.Send(string, numBytes)) |
| 695 |
{ |
| 696 |
PLOG(PL_ERROR, "ProtoExample::OnClientSocketEvent() error sending\n"); |
| 697 |
} |
| 698 |
else if (len != numBytes) |
| 699 |
{ |
| 700 |
PLOG(PL_WARN, "ProtoExample::OnClientSocketEvent() incomplete Send()\n"); |
| 701 |
TRACE(" (only sent %lu of %lu bytes (msgCount:%d))\n", numBytes, len, client_msg_count); |
| 702 |
} |
| 703 |
else |
| 704 |
{ |
| 705 |
TRACE("sent %u bytes to ProtoServer ...\n", len); |
| 706 |
} |
| 707 |
|
| 708 |
break; |
| 709 |
} |
| 710 |
case ProtoSocket::RECV: |
| 711 |
{ |
| 712 |
TRACE("RECV) ...\n"); |
| 713 |
char buffer[1024]; |
| 714 |
unsigned int len = 1024; |
| 715 |
theSocket.Recv(buffer, len); |
| 716 |
if (0 != len) |
| 717 |
{ |
| 718 |
buffer[len-1] = '\0'; |
| 719 |
TRACE("ProtoExample::OnClientSocketEvent() received %u bytes \"%s\" from %s\n", |
| 720 |
len, buffer, theSocket.GetDestination().GetHostString()); |
| 721 |
} |
| 722 |
else |
| 723 |
{ |
| 724 |
TRACE("ProtoExample::OnClientSocketEvent() received 0 bytes\n"); |
| 725 |
} |
| 726 |
break; |
| 727 |
} |
| 728 |
case ProtoSocket::DISCONNECT: |
| 729 |
TRACE("DISCONNECT) ...\n"); |
| 730 |
client_socket.Close(); |
| 731 |
break; |
| 732 |
|
| 733 |
case ProtoSocket::EXCEPTION: |
| 734 |
TRACE("EXCEPTION) ...\n"); |
| 735 |
break; |
| 736 |
|
| 737 |
case ProtoSocket::ERROR_: |
| 738 |
TRACE("ERROR_) ...\n"); |
| 739 |
break; |
| 740 |
} |
| 741 |
} // end ProtoExample::OnClientSocketEvent() |
| 742 |
|
| 743 |
const char* PROTO_HTML = |
| 744 |
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n" |
| 745 |
"<html>\n" |
| 746 |
"<head>\n" |
| 747 |
"<title>protoExample</title>\n" |
| 748 |
"</head>\n" |
| 749 |
"<body>\n" |
| 750 |
"<p>This is a canned response from your friendly, neighborhood <b>protoExample</b> server.</p>\n" |
| 751 |
"</body>\n" |
| 752 |
"</html>\n"; |
| 753 |
|
| 754 |
void ProtoExample::OnServerSocketEvent(ProtoSocket& theSocket, |
| 755 |
ProtoSocket::Event theEvent) |
| 756 |
{ |
| 757 |
TRACE("ProtoExample::OnServerSocketEvent("); |
| 758 |
switch (theEvent) |
| 759 |
{ |
| 760 |
case ProtoSocket::INVALID_EVENT: |
| 761 |
TRACE("INVALID_EVENT) ...\n"); |
| 762 |
break; |
| 763 |
case ProtoSocket::CONNECT: |
| 764 |
TRACE("CONNECT) ...\n"); |
| 765 |
break; |
| 766 |
case ProtoSocket::ACCEPT: |
| 767 |
{ |
| 768 |
TRACE("ACCEPT) ...\n"); |
| 769 |
ProtoSocket* connectedSocket = new ProtoSocket(ProtoSocket::TCP); |
| 770 |
if (NULL == connectedSocket) |
| 771 |
{ |
| 772 |
PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent(ACCEPT) new ProtoSocket error\n"); |
| 773 |
break; |
| 774 |
} |
| 775 |
connectedSocket->SetListener(this, &ProtoExample::OnServerSocketEvent); |
| 776 |
|
| 777 |
if (!server_socket.Accept(connectedSocket)) |
| 778 |
{ |
| 779 |
PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent(ACCEPT) error accepting connection\n"); |
| 780 |
delete connectedSocket; |
| 781 |
break; |
| 782 |
} |
| 783 |
|
| 784 |
if (!connection_list.AddSocket(*connectedSocket)) |
| 785 |
{ |
| 786 |
PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent(ACCEPT) error adding client to list\n"); |
| 787 |
delete connectedSocket; |
| 788 |
} |
| 789 |
PLOG(PL_ERROR, "protoExample: accepted connection from %s\n", |
| 790 |
connectedSocket->GetDestination().GetHostString()); |
| 791 |
break; |
| 792 |
} |
| 793 |
case ProtoSocket::SEND: |
| 794 |
TRACE("SEND) ...\n"); |
| 795 |
break; |
| 796 |
case ProtoSocket::RECV: |
| 797 |
{ |
| 798 |
TRACE("RECV) ...\n"); |
| 799 |
char buffer[1024]; |
| 800 |
unsigned int len = 1024; |
| 801 |
if (theSocket.Recv(buffer, len)) |
| 802 |
{ |
| 803 |
if (0 != len) |
| 804 |
{ |
| 805 |
buffer[len-1] = '\0'; |
| 806 |
TRACE("ProtoExample::OnServerSocketEvent() received %u bytes \"%s\" from %s\n", |
| 807 |
len, buffer, theSocket.GetDestination().GetHostString()); |
| 808 |
|
| 809 |
// Talk back to client |
| 810 |
const char* string = "Hi ProtoClient, I'm fine. How about you?"; |
| 811 |
if (use_html) string = PROTO_HTML; |
| 812 |
len = strlen(string) + 1; |
| 813 |
unsigned int numBytes = len; |
| 814 |
if (!theSocket.Send(string, numBytes)) |
| 815 |
{ |
| 816 |
PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent() error sending\n"); |
| 817 |
} |
| 818 |
else if (len != numBytes) |
| 819 |
{ |
| 820 |
PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent() incomplete Send()\n"); |
| 821 |
TRACE(" (only sent %lu of %lu bytes)\n", numBytes, len); |
| 822 |
} |
| 823 |
else |
| 824 |
{ |
| 825 |
TRACE("sent %u bytes back to ProtoClient ...\n", len); |
| 826 |
} |
| 827 |
if (use_html) theSocket.Shutdown(); |
| 828 |
} |
| 829 |
else |
| 830 |
{ |
| 831 |
TRACE("ProtoExample::OnServerSocketEvent() received 0 bytes\n"); |
| 832 |
} |
| 833 |
|
| 834 |
} |
| 835 |
else |
| 836 |
{ |
| 837 |
PLOG(PL_ERROR, "ProtoExample::OnServerSocketEvent() error receiving\n"); |
| 838 |
} |
| 839 |
break; |
| 840 |
} |
| 841 |
case ProtoSocket::DISCONNECT: |
| 842 |
TRACE("DISCONNECT) ...\n"); |
| 843 |
if(&theSocket == &server_socket) |
| 844 |
{ |
| 845 |
TRACE("server_socket disconnected!\n"); |
| 846 |
break; |
| 847 |
} |
| 848 |
theSocket.Close(); |
| 849 |
connection_list.RemoveSocket(theSocket); |
| 850 |
break; |
| 851 |
|
| 852 |
case ProtoSocket::EXCEPTION: |
| 853 |
TRACE("EXCEPTION) ...\n"); |
| 854 |
break; |
| 855 |
|
| 856 |
case ProtoSocket::ERROR_: |
| 857 |
TRACE("ERROR_) ...\n"); |
| 858 |
break; |
| 859 |
} |
| 860 |
} // end ProtoExample::OnServerSocketEvent() |
| 861 |
|
| 862 |
|