| 1 |
|
| 2 |
#include "protokit.h" |
| 3 |
#include <stdio.h> // for stderr output as needed |
| 4 |
#include <stdlib.h> // for atoi() |
| 5 |
|
| 6 |
#define PIPE_TYPE ProtoPipe::MESSAGE |
| 7 |
|
| 8 |
/** |
| 9 |
* @class Sock2Pipe |
| 10 |
* |
| 11 |
* @brief This example program illustrates/tests the use of the ProtoPipe class |
| 12 |
* for local domain interprocess communications |
| 13 |
*/ |
| 14 |
class Sock2Pipe : public ProtoApp |
| 15 |
{ |
| 16 |
public: |
| 17 |
Sock2Pipe(); |
| 18 |
~Sock2Pipe(); |
| 19 |
|
| 20 |
// Overrides from ProtoApp or NsProtoSimAgent base |
| 21 |
bool OnStartup(int argc, const char*const* argv); |
| 22 |
bool ProcessCommands(int argc, const char*const* argv); |
| 23 |
void OnShutdown(); |
| 24 |
|
| 25 |
bool OnCommand(const char* cmd, const char* val); |
| 26 |
|
| 27 |
private: |
| 28 |
enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG}; |
| 29 |
static CmdType GetCmdType(const char* string); |
| 30 |
static const char* const CMD_LIST[]; |
| 31 |
void Usage(); |
| 32 |
|
| 33 |
void OnListenEvent(ProtoSocket& theSocket, |
| 34 |
ProtoSocket::Event theEvent); |
| 35 |
|
| 36 |
void SendMessage(); |
| 37 |
|
| 38 |
ProtoPipe send_pipe; |
| 39 |
char send_pipe_name[8191]; |
| 40 |
|
| 41 |
ProtoSocket socket; |
| 42 |
int port; |
| 43 |
ProtoAddress addr; |
| 44 |
|
| 45 |
char msg_buffer[8191]; |
| 46 |
unsigned int msg_len; |
| 47 |
}; // end class Sock2Pipe |
| 48 |
|
| 49 |
// Instantiate our application instance |
| 50 |
PROTO_INSTANTIATE_APP(Sock2Pipe) |
| 51 |
|
| 52 |
Sock2Pipe::Sock2Pipe() |
| 53 |
: send_pipe(PIPE_TYPE), socket(ProtoSocket::UDP), |
| 54 |
msg_len(8191) |
| 55 |
{ |
| 56 |
memset(msg_buffer,0,8191); |
| 57 |
socket.SetNotifier(&GetSocketNotifier()); |
| 58 |
socket.SetListener(this, &Sock2Pipe::OnListenEvent); |
| 59 |
port = 11151;//this should be set to the default port sdt listens too |
| 60 |
|
| 61 |
sprintf(send_pipe_name,"sdt"); |
| 62 |
} |
| 63 |
|
| 64 |
Sock2Pipe::~Sock2Pipe() |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
void Sock2Pipe::Usage() |
| 69 |
{ |
| 70 |
fprintf(stderr, "sock2Pipe [listen [<mcastAddr>/]<port>][send <pipename>]\n"); |
| 71 |
} // end Sock2Pipe::Usage() |
| 72 |
|
| 73 |
bool Sock2Pipe::OnStartup(int argc, const char*const* argv) |
| 74 |
{ |
| 75 |
if (argc == 1) |
| 76 |
{ |
| 77 |
TRACE("No args! Are you sure you want to be running without any args?\n"); |
| 78 |
Usage(); |
| 79 |
} |
| 80 |
if (!ProcessCommands(argc, argv)) |
| 81 |
{ |
| 82 |
PLOG(PL_ERROR, "sock2Pipe::OnStartup() error processing command line\n"); |
| 83 |
Usage(); |
| 84 |
return false; |
| 85 |
} |
| 86 |
if(!(socket.Open(port,ProtoAddress::IPv4))) |
| 87 |
{ |
| 88 |
PLOG(PL_ERROR,"sock2Pipe::OnStartup() error opening the socket on port %d\n",port); |
| 89 |
return false; |
| 90 |
} |
| 91 |
TRACE("Listening to port %d\n",port); |
| 92 |
if(addr.IsMulticast()) //do multicast stuff |
| 93 |
{ |
| 94 |
if(!socket.JoinGroup(addr)) |
| 95 |
{ |
| 96 |
PLOG(PL_ERROR,"sock2Pipe::OnStartup() error joining multicast group %s\n",addr.GetHostString()); |
| 97 |
socket.Close(); |
| 98 |
return false; |
| 99 |
} |
| 100 |
TRACE("Joined multicast group %s\n",addr.GetHostString()); |
| 101 |
} |
| 102 |
//connect to a sending pipe |
| 103 |
if (!send_pipe.Connect(send_pipe_name)) |
| 104 |
{ |
| 105 |
TRACE("Sock2Pipe::OnCommand() send_pipe.Connect() error connecting to pipe with name \"%s\".\nWill continue to attempt to connect.\n",send_pipe_name); |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
TRACE("sock2Pipe: sending to pipe \"%s\"\n", send_pipe_name); |
| 110 |
} |
| 111 |
return true; |
| 112 |
} // end Sock2Pipe::OnStartup() |
| 113 |
|
| 114 |
void Sock2Pipe::OnShutdown() |
| 115 |
{ |
| 116 |
if (send_pipe.IsOpen()) send_pipe.Close(); |
| 117 |
if (socket.IsOpen()) |
| 118 |
{ |
| 119 |
if(addr.IsMulticast()) |
| 120 |
{ |
| 121 |
socket.LeaveGroup(addr); |
| 122 |
} |
| 123 |
socket.Close(); |
| 124 |
} |
| 125 |
TRACE("sock2Pipe: Done.\n"); |
| 126 |
} // end Sock2Pipe::OnShutdown() |
| 127 |
|
| 128 |
void Sock2Pipe::OnListenEvent(ProtoSocket& /*theSocket*/, |
| 129 |
ProtoSocket::Event theEvent) |
| 130 |
{ |
| 131 |
switch (theEvent) |
| 132 |
{ |
| 133 |
case ProtoSocket::RECV: |
| 134 |
{ |
| 135 |
TRACE("sock2Pipe: listen RECV event ..\n"); |
| 136 |
unsigned int len = 8191; |
| 137 |
if (socket.Recv(msg_buffer, len)) |
| 138 |
{ |
| 139 |
if (len){ |
| 140 |
SendMessage(); |
| 141 |
TRACE("sock2Pipe: recvd \"%s\"\n", msg_buffer); |
| 142 |
} |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
PLOG(PL_ERROR, "Sock2Pipe::OnListenEvent() listen_pipe.Recv() error\n"); |
| 147 |
} |
| 148 |
break; |
| 149 |
} |
| 150 |
case ProtoSocket::SEND: |
| 151 |
TRACE("sock2Pipe: listen SEND event ..\n"); |
| 152 |
break; |
| 153 |
case ProtoSocket::ACCEPT: |
| 154 |
TRACE("sock2Pipe: listen ACCEPT event ..\n"); |
| 155 |
break; |
| 156 |
case ProtoSocket::DISCONNECT: |
| 157 |
TRACE("sock2Pipe: listen DISCONNECT event ..\n"); |
| 158 |
break; |
| 159 |
default: |
| 160 |
TRACE("Sock2Pipe::OnListenEvent(%d) unhandled event type\n", theEvent); |
| 161 |
break; |
| 162 |
|
| 163 |
} // end switch(theEvent) |
| 164 |
} // end Sock2Pipe::OnListenEvent() |
| 165 |
|
| 166 |
void Sock2Pipe::SendMessage() |
| 167 |
{ |
| 168 |
if(!send_pipe.IsOpen()) |
| 169 |
{ |
| 170 |
//try and connect to the pipe again maybe its up now... |
| 171 |
send_pipe.Connect(send_pipe_name); |
| 172 |
} |
| 173 |
if(send_pipe.IsOpen()) |
| 174 |
{ |
| 175 |
TRACE("Sending message \"%s\" to pipe \"%s\"\n",msg_buffer,send_pipe_name); |
| 176 |
unsigned int len = strlen(msg_buffer); |
| 177 |
send_pipe.Send(msg_buffer,len); |
| 178 |
memset(msg_buffer,0,msg_len); //clear out the message |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
TRACE("Unable to send message \"%s\" to pipe \"%d\"\n",msg_buffer,send_pipe_name); |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
bool Sock2Pipe::ProcessCommands(int argc, const char*const* argv) |
| 188 |
{ |
| 189 |
// Dispatch command-line commands to our OnCommand() method |
| 190 |
int i = 1; |
| 191 |
while ( i < argc) |
| 192 |
{ |
| 193 |
if (!strcmp("listen", argv[i])) |
| 194 |
{ |
| 195 |
i++; |
| 196 |
const char* index = strchr(argv[i],'/'); |
| 197 |
if(index!=NULL) |
| 198 |
{ |
| 199 |
char charAddr[255]; |
| 200 |
memset(charAddr,0,255); |
| 201 |
strncpy(charAddr,argv[i],index-argv[i]); |
| 202 |
if(!(addr.ResolveFromString(charAddr))) |
| 203 |
{ |
| 204 |
PLOG(PL_ERROR, "Sock2Pipe::OnCommand() error setting address to \"%s\"\n",charAddr); |
| 205 |
return false; |
| 206 |
} |
| 207 |
port = atoi(index+1); |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
port = atoi(argv[i]); |
| 212 |
} |
| 213 |
} |
| 214 |
else if (!strcmp("send", argv[i])) |
| 215 |
{ |
| 216 |
i++; |
| 217 |
strcpy(send_pipe_name,argv[i]); |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
PLOG(PL_ERROR, "Sock2Pipe::OnCommand() unknown command error?\n"); |
| 222 |
return false; |
| 223 |
} |
| 224 |
i++; |
| 225 |
} |
| 226 |
return true; |
| 227 |
} // end Sock2Pipe::ProcessCommands() |