Debian Codegraph @

protolib/examples/netExample.cpp

raw
1
2 #include "protoApp.h"
3 #include "protoNet.h"
4
5 #include <stdlib.h> // for atoi()
6 #include <stdio.h> // for stdout/stderr printouts
7 #include <string.h>
8
9 /**
10 * @class NetExample
11 *
12 * @brief An example using a ProtoNet class instance to
13 * monitor network device status
14 */
15 class NetExample : public ProtoApp
16 {
17 public:
18 NetExample();
19 ~NetExample();
20
21 // Overrides from ProtoApp or NsProtoSimAgent base
22 bool OnStartup(int argc, const char*const* argv);
23 bool ProcessCommands(int argc, const char*const* argv)
24 {return true;}
25 void OnShutdown();
26
27 private:
28 void MonitorEventHandler(ProtoChannel& theChannel,
29 ProtoChannel::Notification theNotification);
30
31 enum CmdType {CMD_INVALID, CMD_ARG, CMD_NOARG};
32 static const char* const CMD_LIST[];
33 static CmdType GetCmdType(const char* string);
34 bool OnCommand(const char* cmd, const char* val);
35 void Usage();
36
37 ProtoNet::Monitor* monitor;
38
39 }; // end class NetExample
40
41 const char* const NetExample::CMD_LIST[] =
42 {
43 NULL
44 };
45
46 // This macro creates our ProtoApp derived application instance
47 PROTO_INSTANTIATE_APP(NetExample)
48
49 NetExample::NetExample()
50 : monitor(NULL)
51
52 {
53
54 }
55
56 NetExample::~NetExample()
57 {
58 }
59
60 void NetExample::Usage()
61 {
62 fprintf(stderr, "netExample\n");
63 } // end NetExample::Usage()
64
65
66
67 bool NetExample::OnStartup(int argc, const char*const* argv)
68 {
69
70 // Create our cap_rcvr instance and initialize ...
71 if (!(monitor = ProtoNet::Monitor::Create()))
72 {
73 PLOG(PL_ERROR, "detourExample::OnStartup() new ProtoNet::Monitor error: %s\n", GetErrorString());
74 return false;
75 }
76
77 monitor->SetNotifier(static_cast<ProtoChannel::Notifier*>(&dispatcher));
78 monitor->SetListener(this, &NetExample::MonitorEventHandler);
79
80 if (!monitor->Open())
81 {
82 PLOG(PL_ERROR, "detourExample::OnStartup() ProtoNet::Monitor::Open() error\n");
83 }
84
85 return true;
86 } // end NetExample::OnStartup()
87
88 void NetExample::OnShutdown()
89 {
90
91 if (NULL != monitor)
92 {
93 monitor->Close();
94 delete monitor;
95 monitor = NULL;
96 }
97 PLOG(PL_ERROR, "netExample: Done.\n");
98 CloseDebugLog();
99 } // end NetExample::OnShutdown()
100
101 void NetExample::MonitorEventHandler(ProtoChannel& theChannel,
102 ProtoChannel::Notification theNotification)
103 {
104 if (ProtoChannel::NOTIFY_INPUT == theNotification)
105 {
106 // Read all available (until NULL_EVENT) events
107 while (1)
108 {
109 ProtoNet::Monitor::Event theEvent;
110 if (!monitor->GetNextEvent(theEvent))
111 {
112 PLOG(PL_ERROR, "NetExample::MonitorEventHandler() error: failure getting network events\n");
113 return;
114 }
115 if (ProtoNet::Monitor::Event::NULL_EVENT == theEvent.GetType()) break;
116 char ifName[256];
117 ifName[255] = '\0';
118 if (ProtoNet::Monitor::Event::UNKNOWN_EVENT != theEvent.GetType())
119 {
120 if (!ProtoNet::GetInterfaceName(theEvent.GetInterfaceIndex(), ifName, 255))
121 {
122
123 ifName[0] = '\0';
124 #ifndef WIN32
125 PLOG(PL_ERROR, "NetExample::MonitorEventHandler(event:%d) error: unable to get name for iface index %d\n",
126 theEvent.GetType(),theEvent.GetInterfaceIndex());
127 continue; // we want to report "down" interfaces on win32 that we won't have interface names for
128 #endif // WIN32
129 }
130 }
131 switch(theEvent.GetType())
132 {
133 case ProtoNet::Monitor::Event::IFACE_UP:
134 PLOG(PL_ALWAYS, "netExample: interface \"%s\" index %d is up ...\n", ifName,theEvent.GetInterfaceIndex());
135 break;
136 case ProtoNet::Monitor::Event::IFACE_DOWN:
137 PLOG(PL_ALWAYS, "netExample: interface \"%s\" index %d is down ...\n", ifName, theEvent.GetInterfaceIndex());
138 break;
139 case ProtoNet::Monitor::Event::IFACE_ADDR_NEW:
140 PLOG(PL_ALWAYS, "netExample: interface \"%s\" index %d new address %s ...\n",
141 ifName, theEvent.GetInterfaceIndex(),theEvent.GetAddress().GetHostString());
142 break;
143 case ProtoNet::Monitor::Event::IFACE_ADDR_DELETE:
144 PLOG(PL_ALWAYS, "netExample: interface \"%s\" index %d deleted address %s ...\n",
145 ifName, theEvent.GetInterfaceIndex(), theEvent.GetAddress().GetHostString());
146 break;
147 case ProtoNet::Monitor::Event::IFACE_STATE:
148 PLOG(PL_ALWAYS, "netExample: interface \"%s\" index %d state change ", ifName, theEvent.GetInterfaceIndex());
149 if (theEvent.GetAddress().GetType() != ProtoAddress::INVALID)
150 PLOG(PL_ALWAYS, "address %s ...\n", theEvent.GetAddress().GetHostString());
151 else
152 PLOG(PL_ALWAYS,"... \n");
153
154 break;
155 default:
156 break;
157 }
158 }
159 }
160
161 } // end NetExample::MonitorEventHandler()