| 1 |
#ifdef SIMULATE |
| 2 |
#include "nsProtoSimAgent.h" |
| 3 |
#else |
| 4 |
#include "protoApp.h" |
| 5 |
#endif // if/else SIMULATE |
| 6 |
|
| 7 |
#include "protokit.h" |
| 8 |
|
| 9 |
#include <stdlib.h> // for atoi() |
| 10 |
#include <stdio.h> // for stdout/stderr printouts |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
class ProtoFileExample : |
| 14 |
#ifdef SIMULATE |
| 15 |
public NsProtoSimAgent |
| 16 |
#else |
| 17 |
public ProtoApp |
| 18 |
#endif // if/else SIMULATE |
| 19 |
|
| 20 |
{ |
| 21 |
public: |
| 22 |
ProtoFileExample(); |
| 23 |
~ProtoFileExample(); |
| 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 |
bool OnTimeout(ProtoTimer& theTimer);//do nothing utility timer |
| 49 |
void OnFileEvent(ProtoChannel& theChannel, |
| 50 |
ProtoChannel::Notification notifyType); |
| 51 |
static const char* const CMD_LIST[]; |
| 52 |
static void SignalHandler(int sigNum); |
| 53 |
|
| 54 |
// ProtoTimer/ UDP socket demo members |
| 55 |
ProtoTimer example_timer; |
| 56 |
|
| 57 |
ProtoFile example_file; |
| 58 |
|
| 59 |
ProtoSocket::List connection_list; |
| 60 |
}; // end class ProtoFileExample |
| 61 |
|
| 62 |
|
| 63 |
// (TBD) Note this #if/else code could be replaced with something like |
| 64 |
// a PROTO_INSTANTIATE(ProtoFileExample) macro defined differently |
| 65 |
// in "protoApp.h" and "nsProtoSimAgent.h" |
| 66 |
#ifdef SIMULATE |
| 67 |
#ifdef NS2 |
| 68 |
static class NsProtoFileExampleClass : public TclClass |
| 69 |
{ |
| 70 |
public: |
| 71 |
NsProtoFileExampleClass() : TclClass("Agent/ProtoFileExample") {} |
| 72 |
TclObject *create(int argc, const char*const* argv) |
| 73 |
{return (new ProtoFileExample());} |
| 74 |
} class_proto_example; |
| 75 |
#endif // NS2 |
| 76 |
|
| 77 |
|
| 78 |
#else |
| 79 |
|
| 80 |
// Our application instance |
| 81 |
PROTO_INSTANTIATE_APP(ProtoFileExample) |
| 82 |
|
| 83 |
#endif // SIMULATE |
| 84 |
|
| 85 |
ProtoFileExample::ProtoFileExample() |
| 86 |
{ |
| 87 |
example_timer.SetListener(this, &ProtoFileExample::OnTimeout); |
| 88 |
//these don't currently work as linux doesn't support async signaling on files (it always sends a ready signal) |
| 89 |
//example_file.SetNotifier(&GetChannelNotifier()); |
| 90 |
//example_file.SetListener(this, &ProtoFileExample::OnFileEvent); |
| 91 |
// SetDebugLevel(PL_MAX); |
| 92 |
} |
| 93 |
|
| 94 |
ProtoFileExample::~ProtoFileExample() |
| 95 |
{ |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
void ProtoFileExample::Usage() |
| 100 |
{ |
| 101 |
fprintf(stderr, "protoExample [listen <file>]\n"); |
| 102 |
} // end ProtoFileExample::Usage() |
| 103 |
|
| 104 |
|
| 105 |
const char* const ProtoFileExample::CMD_LIST[] = |
| 106 |
{ |
| 107 |
"+listen", // Open and listen to a file. |
| 108 |
NULL |
| 109 |
}; |
| 110 |
|
| 111 |
ProtoFileExample::CmdType ProtoFileExample::GetCmdType(const char* cmd) |
| 112 |
{ |
| 113 |
if (!cmd) return CMD_INVALID; |
| 114 |
unsigned int len = strlen(cmd); |
| 115 |
bool matched = false; |
| 116 |
CmdType type = CMD_INVALID; |
| 117 |
const char* const* nextCmd = CMD_LIST; |
| 118 |
while (*nextCmd) |
| 119 |
{ |
| 120 |
if (!strncmp(cmd, *nextCmd+1, len)) |
| 121 |
{ |
| 122 |
if (matched) |
| 123 |
{ |
| 124 |
// ambiguous command (command should match only once) |
| 125 |
return CMD_INVALID; |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
matched = true; |
| 130 |
if ('+' == *nextCmd[0]) |
| 131 |
type = CMD_ARG; |
| 132 |
else |
| 133 |
type = CMD_NOARG; |
| 134 |
} |
| 135 |
} |
| 136 |
nextCmd++; |
| 137 |
} |
| 138 |
return type; |
| 139 |
} // end ProtoFileExample::GetCmdType() |
| 140 |
|
| 141 |
bool ProtoFileExample::OnStartup(int argc, const char*const* argv) |
| 142 |
{ |
| 143 |
if (!ProcessCommands(argc, argv)) |
| 144 |
{ |
| 145 |
PLOG(PL_ERROR, "protoFileExample: Error! bad command line\n"); |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
return true; |
| 150 |
} // end ProtoFileExample::OnStartup() |
| 151 |
|
| 152 |
void ProtoFileExample::OnShutdown() |
| 153 |
{ |
| 154 |
if (example_timer.IsActive()) example_timer.Deactivate(); |
| 155 |
if (example_file.IsOpen()) example_file.Close(); |
| 156 |
connection_list.Destroy(); |
| 157 |
CloseDebugLog(); |
| 158 |
} // end ProtoFileExample::OnShutdown() |
| 159 |
|
| 160 |
bool ProtoFileExample::ProcessCommands(int argc, const char*const* argv) |
| 161 |
{ |
| 162 |
// Dispatch command-line commands to our OnCommand() method |
| 163 |
if( argc <= 1 ) |
| 164 |
{ |
| 165 |
PLOG(PL_ERROR,"ProtoFileExample::ProcessCommands() No args given\n"); |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
int i = 1; |
| 170 |
while ( i < argc) |
| 171 |
{ |
| 172 |
// Is it a class ProtoFileExample command? |
| 173 |
switch (GetCmdType(argv[i])) |
| 174 |
{ |
| 175 |
case CMD_INVALID: |
| 176 |
{ |
| 177 |
#ifndef SIMULATE |
| 178 |
PLOG(PL_ERROR, "ProtoFileExample::ProcessCommands() Invalid command:%s\n", |
| 179 |
argv[i]); |
| 180 |
#endif // SIMULATE |
| 181 |
return false; |
| 182 |
} |
| 183 |
case CMD_NOARG: |
| 184 |
if (!OnCommand(argv[i], NULL)) |
| 185 |
{ |
| 186 |
PLOG(PL_ERROR, "ProtoFileExample::ProcessCommands() ProcessCommand(%s) error\n", |
| 187 |
argv[i]); |
| 188 |
return false; |
| 189 |
} |
| 190 |
i++; |
| 191 |
break; |
| 192 |
case CMD_ARG: |
| 193 |
if (!OnCommand(argv[i], argv[i+1])) |
| 194 |
{ |
| 195 |
PLOG(PL_ERROR, "ProtoFileExample::ProcessCommands() ProcessCommand(%s, %s) error\n", |
| 196 |
argv[i], argv[i+1]); |
| 197 |
return false; |
| 198 |
} |
| 199 |
i += 2; |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
return true; |
| 204 |
} // end ProtoFileExample::ProcessCommands() |
| 205 |
|
| 206 |
bool ProtoFileExample::OnCommand(const char* cmd, const char* val) |
| 207 |
{ |
| 208 |
|
| 209 |
// (TBD) move command processing into Mgen class ??? |
| 210 |
CmdType type = GetCmdType(cmd); |
| 211 |
ASSERT(CMD_INVALID != type); |
| 212 |
unsigned int len = strlen(cmd); |
| 213 |
if ((CMD_ARG == type) && !val) |
| 214 |
{ |
| 215 |
PLOG(PL_ERROR, "ProtoFileExample::ProcessCommand(%s) missing argument\n", cmd); |
| 216 |
return false; |
| 217 |
} |
| 218 |
else if (!strncmp("listen", cmd, len)) |
| 219 |
{ |
| 220 |
TRACE("opening file ...\n"); |
| 221 |
if (!example_file.Open(val)) |
| 222 |
{ |
| 223 |
PLOG(PL_ERROR, "ProtoFileExample::ProcessCommand(listen) error opening %s\n",val); |
| 224 |
return false; |
| 225 |
} |
| 226 |
TRACE("file opened ...\n"); |
| 227 |
example_timer.SetInterval(1.0); |
| 228 |
example_timer.SetRepeat(-1); |
| 229 |
TRACE("calling OnTimeout() ...\n"); |
| 230 |
OnTimeout(example_timer); |
| 231 |
if (!example_timer.IsActive()) ActivateTimer(example_timer); |
| 232 |
} |
| 233 |
else if (!strncmp("background", cmd, len)) |
| 234 |
{ |
| 235 |
// do nothing (this command was scanned immediately at startup) |
| 236 |
} |
| 237 |
return true; |
| 238 |
} // end ProtoFileExample::OnCommand() |
| 239 |
|
| 240 |
bool ProtoFileExample::OnTimeout(ProtoTimer& /*theTimer*/) |
| 241 |
{ |
| 242 |
//TRACE("ProtoFileExample::OnTimeout() ...\n"); |
| 243 |
char buffer[1024]; |
| 244 |
unsigned int len = 1024; |
| 245 |
while(example_file.Readline(buffer,len)) |
| 246 |
{ |
| 247 |
TRACE("Got line\"%s\" with length %d\n",buffer,len); |
| 248 |
len = 1024; |
| 249 |
} |
| 250 |
if(len!=0) |
| 251 |
{ |
| 252 |
TRACE("Got partial line!\n%s",buffer); |
| 253 |
} |
| 254 |
return true; |
| 255 |
} // end ProtoFileExample::OnTxTimeout() |
| 256 |
|
| 257 |
void ProtoFileExample::OnFileEvent(ProtoChannel& theChannel, |
| 258 |
ProtoChannel::Notification notifyType) |
| 259 |
{ |
| 260 |
// TRACE("ProtoFileExample::OnFileEvent with notifyType value of %d\n",notifyType); |
| 261 |
char buffer[1024]; |
| 262 |
unsigned int len = 1024; |
| 263 |
while(example_file.Readline(buffer,len)) |
| 264 |
{ |
| 265 |
TRACE("Got line\"%s\" with length %d\n",buffer,len); |
| 266 |
len = 1024; |
| 267 |
} |
| 268 |
if(len!=0) |
| 269 |
{ |
| 270 |
// TRACE("Got partial line!\n%s",buffer); |
| 271 |
} |
| 272 |
} // end ProtoFileExample::OnUdpSocketEvent() |