Debian Codegraph @

protolib/include/protoApp.h

raw
1 #ifndef _PROTO_APP
2 #define _PROTO_APP
3
4 #include "protoDispatcher.h"
5 /**
6 * @class ProtoApp
7 *
8 * @brief Base class for implementing protolib-based
9 * command-line applications.
10 *
11 * Note that "ProtoApp" and "ProtoSimAgent" are
12 * designed such that subclasses can be derived
13 * from either to reuse the same code in either a
14 * real-world applications or as an "agent"
15 * (entity) within a network simulation
16 * environment (e.g. ns-2, OPNET). A "background"
17 * command is included for Win32 to launch the
18 * app without a terminal window.
19 */
20
21 class ProtoApp
22 {
23 public:
24 virtual ~ProtoApp();
25
26 virtual bool OnStartup(int argc, const char*const* argv) = 0;
27 virtual bool ProcessCommands(int argc, const char*const* argv) = 0;
28 virtual void OnShutdown() = 0;
29
30 bool ProcessCommandString(const char* cmd);
31
32 int Run()
33 {return dispatcher.Run();}
34 bool IsRunning() const
35 {return dispatcher.IsRunning();}
36 void Stop(int exitCode = 0)
37 {dispatcher.Stop(exitCode);}
38
39 // Some helper methods
40 void ActivateTimer(ProtoTimer& theTimer)
41 {dispatcher.ActivateTimer(theTimer);}
42
43 void DeactivateTimer(ProtoTimer& theTimer)
44 {dispatcher.DeactivateTimer(theTimer);}
45
46 ProtoSocket::Notifier& GetSocketNotifier()
47 {return static_cast<ProtoSocket::Notifier&>(dispatcher);}
48
49 ProtoChannel::Notifier& GetChannelNotifier()
50 {return static_cast<ProtoChannel::Notifier&>(dispatcher);}
51
52 ProtoTimerMgr& GetTimerMgr()
53 {return static_cast<ProtoTimerMgr&>(dispatcher);}
54
55 ProtoDispatcher& GetDispatcher()
56 {return dispatcher;}
57 static ProtoApp* GetApp()
58 {return the_app;}
59
60 static void SignalHandler(int sigNum);
61
62 #ifdef WIN32
63 bool Win32Init() {return dispatcher.Win32Init();}
64 #endif // WIN32
65
66 protected:
67 ProtoApp();
68
69 ProtoDispatcher dispatcher;
70
71 private:
72 static ProtoApp* the_app;
73
74 }; // end class ProtoApp
75
76
77 /*
78 * The functions and macros below are used to instantiate
79 * a derive ProtoApp when needed.
80 */
81 int ProtoMain(int argc, char* argv[], bool pauseForUser = false);
82
83 #ifdef WIN32
84
85 int PASCAL Win32ProtoMain(HINSTANCE instance,
86 HINSTANCE prevInst,
87 LPSTR cmdline,
88 int cmdshow);
89 int Win32ProtoMainConsole(int argc, char* argv[]);
90
91 #ifdef _CONSOLE
92 #define PROTO_MAIN() \
93 int main(int argc, char* argv[]) \
94 {return Win32ProtoMainConsole(argc, argv);}
95 #else
96 #ifdef _WIN32_WCE
97 #define PROTO_MAIN() \
98 int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInst, LPWSTR cmdline, int cmdshow) \
99 {return Win32ProtoMain(instance, prevInst, (char*)cmdline, cmdshow);}
100 #else
101 #define PROTO_MAIN() \
102 int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInst, LPSTR cmdline, int cmdshow) \
103 {return Win32ProtoMain(instance, prevInst, cmdline, cmdshow);}
104 #endif // if/else _WIN32_WCE
105 #endif // if/else _CONSOLE
106
107 #else
108
109 #define PROTO_MAIN() \
110 int main(int argc, char* argv[]) \
111 {return ProtoMain(argc, argv);}
112
113 #endif // if/else WIN32/UNIX
114
115 /**
116 * Use this macro to instantiate your derived ProtoApp instance
117 */
118 #define PROTO_INSTANTIATE_APP(X) X protoApp; PROTO_MAIN()
119
120 #endif // !_PROTO_APP