Debian Codegraph @

mgendr/Source/Main.h

raw
1
2 #pragma once
3
4 #include "../JuceLibraryCode/JuceHeader.h"
5 #include "ControlComponent.h"
6
7 #include "protoDispatcher.h"
8 #include "mgen.h"
9
10
11 // Wrap a JUCE thread around a ProtoDispatcher
12 class DispatcherThread : public Thread
13 {
14 public:
15 DispatcherThread()
16 : Thread(String("MgenThread")), priority_boost(false), controller(NULL) {}
17 virtual ~DispatcherThread() {}
18
19 ProtoDispatcher& GetDispatcher()
20 {return dispatcher;}
21
22 bool SuspendThread()
23 {return dispatcher.SuspendThread();}
24 void ResumeThread()
25 {dispatcher.ResumeThread();}
26
27 bool StartThread(bool priorityBoost = false,
28 ProtoDispatcher::Controller* theController = NULL)
29 {
30 priority_boost = priorityBoost;
31 controller = theController;
32 startThread();
33 return true;
34 }
35
36 void Stop()
37 {
38 dispatcher.Stop();
39 waitForThreadToExit(-1);
40 }
41
42 operator ProtoTimerMgr&()
43 {return dispatcher;}
44 operator ProtoSocket::Notifier&()
45 {return dispatcher;}
46
47 private:
48 void run() override
49 {
50 dispatcher.StartThread(priority_boost, controller, (ProtoDispatcher::ThreadId)getThreadId());
51 }
52 ProtoDispatcher dispatcher;
53 bool priority_boost;
54 ProtoDispatcher::Controller* controller;
55
56 }; // end class DispatcherThread
57
58 //==============================================================================
59 class MgenderApp : public JUCEApplication,
60 public MgenController,
61 //public ApplicationCommandTarget,
62 public MenuBarModel
63 {
64 public:
65 //==============================================================================
66 MgenderApp();
67 ~MgenderApp();
68
69 // These will be part of a juceProtoApp class at some point
70 ProtoTimerMgr& GetTimerMgr()
71 {return dispatcher;}
72 ProtoSocket::Notifier& GetSocketNotifier()
73 {return dispatcher;}
74
75 const String getApplicationName() override {return ProjectInfo::projectName; }
76 const String getApplicationVersion() override {return ProjectInfo::versionString; }
77 bool moreThanOneInstanceAllowed() override {return true;}
78
79 //==============================================================================
80 void initialise (const String& commandLine) override;
81 void shutdown() override;
82
83 //==============================================================================
84 void systemRequestedQuit() override
85 {
86 // This is called when the app is being asked to quit: you can ignore this
87 // request and let the app carry on running, or call quit() to allow the app to close.
88 quit();
89 }
90
91 void anotherInstanceStarted (const String& commandLine) override
92 {
93 // When another instance of the app is launched while this one is running,
94 // this method is invoked, and the commandLine parameter tells you what
95 // the other instance's command-line arguments were.
96 }
97
98 //==============================================================================
99 /*
100 This class implements the desktop window that contains an instance of
101 our MainComponent class.
102 */
103 class MainWindow : public DocumentWindow
104 {
105 public:
106 MainWindow(String name, ControlComponent* theControl);
107
108 void closeButtonPressed() override
109 {
110 // This is called when the user tries to close this window. Here, we'll just
111 // ask the app to quit when this happens, but you can change this to do
112 // whatever you need.
113 JUCEApplication::getInstance()->systemRequestedQuit();
114 }
115
116 /* Note: Be careful if you override any DocumentWindow methods - the base
117 class uses a lot of them, so by overriding you might break its functionality.
118 It's best to do all your work in your content component instead, but if
119 you really have to override any DocumentWindow methods, make sure your
120 subclass also calls the superclass's method.
121 */
122
123 private:
124 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
125 }; // end class MgenderApp::MainWindow
126
127 ApplicationCommandManager& GetCommandManager() {return cmd_manager;}
128 enum CommandIDs
129 {
130 CMD_ABOUT_APP = 1,
131 CMD_LOAD_SCRIPT,
132 CMD_SET_LOG,
133 CMD_FILTER_FEEDBACK,
134 CMD_DYNAMIC_ADDR
135 };
136 void getAllCommands(Array<CommandID>& cmds) override;
137 void getCommandInfo(CommandID commandID, ApplicationCommandInfo& result) override;
138 ApplicationCommandTarget* getNextCommandTarget() override
139 {return nullptr;}
140 bool perform(const InvocationInfo& info) override;
141 StringArray getMenuBarNames() override;
142 PopupMenu getMenuForIndex(int topLevelMenuIndex, const String &menuName) override;
143 void menuItemSelected(int menuItemID, int menuIndex) override;
144 void ShowAppInfo();
145
146 bool ProcessCommandLine();
147
148 Mgen& GetMgen() {return mgen;}
149 //UINT16 GetProbePort() const
150 // {return probe_addr.GetPort();}
151 bool SetProbeAddress(const ProtoAddress& probeAddr, const char* ifaceName);
152 const ProtoAddress& GetProbeAddress() const
153 {return probe_addr;}
154 const char* GetInterface() const
155 {return (('\0' != iface_name[0]) ? iface_name : NULL);}
156 bool StartProbing(const ProtoAddress& probeAddr,
157 const char* probePattern,
158 double probeRate,
159 unsigned int probeSize,
160 const char* ifaceName);
161 void StopProbing();
162 bool IsProbing() const
163 {return (0 != probe_src_port);}
164
165
166 void LoadScriptFile();
167 void SetLogFile();
168
169 private:
170 class Report : public ProtoQueue::Item
171 {
172 public:
173 Report(Protocol theProtocol,
174 const ProtoAddress& srcAddr,
175 const ProtoAddress& dstAddr,
176 UINT32 flowId,
177 const ProtoAddress* reporterAddr = NULL);
178 ~Report();
179
180 static unsigned int BuildKey(char* buffer,
181 unsigned int buflen,
182 Protocol theProtocol,
183 const ProtoAddress& srcAddr,
184 const ProtoAddress& dstAddr,
185 UINT32 flowId,
186 const ProtoAddress* reporterAddr);
187
188 bool Update(const ProtoTime& theTime,
189 const MgenAnalytic::Report& report,
190 const ProtoTime& sentTime);
191
192 Protocol GetProtocol() const
193 {return protocol;}
194 const ProtoAddress& GetSrcAddr() const
195 {return src_addr;}
196 const ProtoAddress& GetDstAddr() const
197 {return dst_addr;}
198 UINT32 GetFlowId() const
199 {return flow_id;}
200 const ProtoAddress& GetReporterAddr() const
201 {return reporter_addr;}
202
203 double GetRateAve() const
204 {return rate_ave;}
205 double GetLossFraction() const
206 {return loss_fraction;}
207 double GetLatencyAve() const
208 {return late_ave;}
209 double GetLatencyMin() const
210 {return late_min;}
211 double GetLatencyMax() const
212 {return late_max;}
213 const ProtoTime& GetUpdateTime() const
214 {return update_time;}
215 UINT32 GetFeedbackId() const
216 {return feedback_id;}
217
218 void SetFeedbackId(UINT32 feedbackId)
219 {feedback_id = feedbackId;}
220
221 // Worst case key size is protocol + 3 IPv6 addrs, 3 ports, and a flowId
222 enum {MAX_KEY = (1 + 3*16 + 3*2 + 4)};
223 const char* GetKey() const
224 {return report_key;}
225 unsigned int GetKeysize() const
226 {return report_keysize;}
227 private:
228 char report_key[MAX_KEY];
229 unsigned int report_keysize;
230
231 Protocol protocol;
232 ProtoAddress src_addr;
233 ProtoAddress dst_addr;
234 UINT32 flow_id;
235 ProtoAddress reporter_addr;
236
237 double rate_ave;
238 double loss_fraction;
239 double late_ave;
240 double late_min;
241 double late_max;
242 ProtoTime report_time;
243 double report_window;
244 double report_offset;
245 ProtoTime update_time;
246 UINT32 feedback_id;
247
248 }; // end class MgenderApp::Report
249
250 class ReportTable : public ProtoIndexedQueueTemplate<Report>
251 {
252 public:
253 Report* FindReport(Protocol theProtocol,
254 const ProtoAddress& srcAddr,
255 const ProtoAddress& dstAddr,
256 UINT32 flowId,
257 const ProtoAddress* reporterAddr = NULL);
258 private:
259 // Required overrides for ProtoIndexedQueue subclasses
260 // (Override these to determine how items are sorted)
261 virtual const char* GetKey(const Item& item) const
262 {return (static_cast<const Report&>(item).GetKey());}
263 virtual unsigned int GetKeysize(const Item& item) const
264 {return (static_cast<const Report&>(item).GetKeysize());}
265 }; // end class MgenderApp::ReportTable
266
267 class ReportList : public ProtoSimpleQueueTemplate<Report> {};
268
269 static int DoMgenLogEvent(FILE*, const char*, ...);
270 void OnMgenLogEvent(const char* text);
271
272 // MGEN flow id values starting at 9001 are used for
273 // the as the probing flow id and (incrementally)
274 // feedback flow ids
275 enum {PROBE_FLOW_ID = 9001};
276 enum {MAX_REPORT_FLOWS = 256};
277 enum {MAX_REPORT_LINES = 256};
278
279 void OnUpdateReport(const ProtoTime& theTime,
280 const MgenAnalytic::Report& report) override;
281 void OnRecvReport(const ProtoTime& theTime,
282 const MgenAnalytic::Report& report,
283 const ProtoAddress& senderAddr,
284 const ProtoTime& sentTime) override;
285 void OnPruneTimeout(ProtoTimer& theTimer);
286
287 void RemoveFlowReport(Report& report);
288 void PruneFlowReports(const ProtoTime& theTime);
289
290 void AppendReportContent(Report& report, const ProtoTime& theTime);
291
292 //ScopedPointer<MainWindow> main_window;
293 std::unique_ptr<MainWindow> main_window;
294 ControlComponent* ctrl_window;
295 ApplicationCommandManager cmd_manager;
296
297 //ProtoDispatcher dispatcher;
298 DispatcherThread dispatcher;
299 Mgen mgen;
300 ProtoTimer prune_timer;
301 ProtoAddress probe_addr;
302 char iface_name[256];
303 UINT16 probe_src_port;
304 UINT32 next_feedback_flow_id;
305 ReportTable report_table; // indexed by flow
306 ReportList report_list; // linked list ordered, oldest -> freshest
307 unsigned int report_flow_count;
308 StringArray report_lines;
309 bool filter_feedback;
310 bool dynamic_probe_addr;
311 FileChooser* active_chooser;
312
313 char event_buffer[512];
314 unsigned int event_index;
315 }; // end class MgenderApp