-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobsim1.erl
577 lines (493 loc) · 20.3 KB
/
mobsim1.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
% src/erlang/mobsim1.erl 2018-2-26 Alan U. Kennington.
% This module tests feasibility of simulating a mobile network using wxErlang.
% (See mobsim2.erl for further development.)
% For wx: http://erlang.org/doc/apps/wx/index.html
% See also file: /usr/local/lib/erlang/lib/wx-1.8.3/examples/simple/hello.erl
-module(mobsim1).
% This includes a large amount of code.
% File: /usr/local/lib/erlang/lib/wx-1.8.3/src/wx.erl
-include_lib("wx/include/wx.hrl").
-export([startMobSimA/0, startWindowA/0]).
-export([startMobileA/1, procMobSimA/3]).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% On my system, this function has problems with Glib invoking SCIM.
% The following garbage appears after calls to:
% wxFrame:new/4. wxFrame:createStatusBar/2
%
% (clientD@hostA)286> wx1:startWindowA().
% (Erlang:8988): GLib-GObject-WARNING **: plugin
% '/usr/lib64/gtk-2.0/immodules/im-scim.so' failed to register type
% 'GtkIMContextSCIM'
% (Erlang:8988): Gtk-WARNING **: Loading IM context type 'scim' failed
% true
%
% However, a window does in fact appear!!!
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
createWindowA(ServerA) ->
% Create a frame on the X server.
% This call triggers the GLib-GObject-WARNING for plug-in GtkIMContextSCIM.
io:format("Calling wxFrame:new/4.~n", []),
FrameA = wxFrame:new(ServerA, -1,
"Mobile simulation A", [{size, {600, 400}}]),
% Carriage return. Get the shell text cursor back to the left of the line.
io:format("~n", []),
% Create status bar.
% This call triggers the GLib-GObject-WARNING for plug-in GtkIMContextSCIM.
io:format("Calling wxFrame:createStatusBar/2.~n", []),
wxFrame:createStatusBar(FrameA, []),
% Carriage return. Get the shell text cursor back to the left of the line.
io:format("~n", []),
% Create tool bar.
% This doesn't create a tool bar, and doesn't cause a Glib/SCIM warning.
io:format("Calling wxFrame:createToolBar/2.~n", []),
wxFrame:createToolBar(FrameA, []),
% Carriage return. Get the shell text cursor back to the left of the line.
% io:format("~n", []),
% Set keyboard focus.
% This seems to have no effect.
% wxFrame:setFocus(FrameA),
wxWindow:setFocus(FrameA),
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% See http://erlang.org/doc/man/wxFrame.html
% which says that wxFrame is derived from:
% wxTopLevelWindow, wxWindow, wxEvtHandler
% http://erlang.org/doc/man/wxEvtHandler.html#connect-2
% wxCloseEventType() = close_window | end_session | query_end_session
% Events defined in file: /usr/local/lib/erlang/lib/wx-1.8.3/include/wx.hrl
% wxClose
wxFrame:connect(FrameA, close_window),
wxFrame:connect(FrameA, end_session),
wxFrame:connect(FrameA, query_end_session),
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Other event classes and types.
% wxActivate
wxFrame:connect(FrameA, activate),
wxFrame:connect(FrameA, activate_app),
wxFrame:connect(FrameA, hibernate),
% wxAuiManager [Advanced User Interface]
% See http://docs.wxwidgets.org/2.8.12/wx_wxauioverview.html#wxauioverview
wxFrame:connect(FrameA, aui_pane_button),
wxFrame:connect(FrameA, aui_pane_close),
wxFrame:connect(FrameA, aui_pane_maximize),
wxFrame:connect(FrameA, aui_pane_restore),
% The following line gives an error message at run time.
% ** exception exit: {{badarg,aui_pane_activated}, ....
% wxFrame:connect(FrameA, aui_pane_activated),
wxFrame:connect(FrameA, aui_render),
wxFrame:connect(FrameA, aui_find_manager),
% wxAuiNotebook [Advanced User Interface]
% (Not interesting enough to list here.)
% wxCalendar
% (Not relevant enough to list here.)
% wxChildFocus
wxFrame:connect(FrameA, child_focus),
% wxClipboardText
wxFrame:connect(FrameA, command_text_copy),
wxFrame:connect(FrameA, command_text_cut),
wxFrame:connect(FrameA, command_text_paste),
% wxColourPicker
wxFrame:connect(FrameA, command_colourpicker_changed),
% wxCommand
% (Too big, and not relevant enough to list here.)
% wxContextMenu
wxFrame:connect(FrameA, context_menu),
% wxDate
wxFrame:connect(FrameA, date_changed),
% wxDisplayChanged
wxFrame:connect(FrameA, display_changed),
% wxDropFiles
wxFrame:connect(FrameA, drop_files),
% wxErase
wxFrame:connect(FrameA, erase_background),
% wxFileDirPiacker
% (Not relevant enough to list here.)
% wxFocus
wxFrame:connect(FrameA, set_focus),
wxFrame:connect(FrameA, kill_focus),
% wxFontPicker
wxFrame:connect(FrameA, command_fontpicker_changed),
% wxGrid
% (Too many to list.)
% wxHelp
wxFrame:connect(FrameA, help),
wxFrame:connect(FrameA, detailed_help),
% wxHtmlLink
wxFrame:connect(FrameA, command_html_link_clicked),
% wxIconize
wxFrame:connect(FrameA, iconize),
% wxIdle
wxFrame:connect(FrameA, idle),
% wxInitDialog
wxFrame:connect(FrameA, init_dialog),
% wxJoystick
wxFrame:connect(FrameA, joy_button_down),
wxFrame:connect(FrameA, joy_button_up),
wxFrame:connect(FrameA, joy_move),
wxFrame:connect(FrameA, joy_zmove),
% wxKey
wxFrame:connect(FrameA, 'char'),
wxFrame:connect(FrameA, char_hook),
wxFrame:connect(FrameA, key_down),
wxFrame:connect(FrameA, key_up),
% wxList
% (Too many to list.)
% wxMaximize
wxFrame:connect(FrameA, maximize),
% wxMenu
wxFrame:connect(FrameA, menu_open),
wxFrame:connect(FrameA, menu_close),
wxFrame:connect(FrameA, menu_highlight),
% wxMouse
wxFrame:connect(FrameA, left_down),
wxFrame:connect(FrameA, left_up),
wxFrame:connect(FrameA, middle_down),
wxFrame:connect(FrameA, middle_up),
wxFrame:connect(FrameA, right_down),
wxFrame:connect(FrameA, right_up),
% wxFrame:connect(FrameA, motion),
wxFrame:connect(FrameA, enter_window),
wxFrame:connect(FrameA, leave_window),
wxFrame:connect(FrameA, left_dclick),
wxFrame:connect(FrameA, middle_dclick),
wxFrame:connect(FrameA, right_dclick),
wxFrame:connect(FrameA, mousewheel),
% wxMove
% wxFrame:connect(FrameA, move),
% wxNavigationKey
wxFrame:connect(FrameA, navigation_key),
% wxNotebook
wxFrame:connect(FrameA, command_notebook_page_changed),
wxFrame:connect(FrameA, command_notebook_page_changing),
% wxPaint
wxFrame:connect(FrameA, paint),
% wxPaletteChanged
wxFrame:connect(FrameA, palette_changed),
% wxQueryNewPalette
wxFrame:connect(FrameA, query_new_palette),
% wxSash
wxFrame:connect(FrameA, sash_dragged),
% wxScroll
wxFrame:connect(FrameA, scroll_top),
wxFrame:connect(FrameA, scroll_bottom),
wxFrame:connect(FrameA, scroll_lineup),
wxFrame:connect(FrameA, scroll_linedown),
wxFrame:connect(FrameA, scroll_pageup),
wxFrame:connect(FrameA, scroll_pagedown),
wxFrame:connect(FrameA, scroll_thumbtrack),
wxFrame:connect(FrameA, scroll_thumbrelease),
wxFrame:connect(FrameA, scroll_changed),
% wxScrollWin
wxFrame:connect(FrameA, scrollwin_top),
wxFrame:connect(FrameA, scrollwin_bottom),
wxFrame:connect(FrameA, scrollwin_lineup),
wxFrame:connect(FrameA, scrollwin_linedown),
wxFrame:connect(FrameA, scrollwin_pageup),
wxFrame:connect(FrameA, scrollwin_pagedown),
wxFrame:connect(FrameA, scrollwin_thumbtrack),
wxFrame:connect(FrameA, scrollwin_thumbrelease),
% wxSetCursor
wxFrame:connect(FrameA, set_cursor),
% wxShow
wxFrame:connect(FrameA, show),
% wxSize
wxFrame:connect(FrameA, size),
% wxSpin
wxFrame:connect(FrameA, command_spinctrl_updated),
wxFrame:connect(FrameA, spin_up),
wxFrame:connect(FrameA, spin_down),
wxFrame:connect(FrameA, spin),
% wxSplitter
% (Not relevant enough to list here.)
% wxStyledText
% (Too many to list. Probably not relevant.)
% wxSysColourChanged
wxFrame:connect(FrameA, sys_colour_changed),
% wxTaskBarIcon
% (Too many to list. Probably not relevant.)
% wxTree
% (Too many to list. Probably not relevant.)
% wxUpdateUI
wxFrame:connect(FrameA, update_ui),
% wxWindowCreate
wxFrame:connect(FrameA, create),
% wxWindowDestroy
wxFrame:connect(FrameA, destroy),
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% The text for wxFrame:setStatusText/3 appears at the bottom of the window.
% See http://erlang.org/doc/man/wxFrame.html#setStatusText-2
ok = wxFrame:setStatusText(FrameA, "Mobile simulation status", []),
% Return the newly created frame.
FrameA.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Event-handling loop for FrameA window.
% Apparently tail-recursive.
% If exit(normal) is called, this function will not return.
% So to allow this function to return, the inner case-statement of
% a nested pair of case-statements returns either carry_on or exit_normal.
% At the end of the inner case-statement, its output-value is used in
% an if-statement to determine whether to recurse or just "fall off the end".
% If the outer case-statement "falls off the end", this function returns to
% the caller, which is startWindowA/0, which can then call wx:destroy/0.
% If exit(normal) is called in the inner loop, this function cannot return.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
handleWindowA(FrameA) ->
receive
% Try to catch all events in a single case.
#wx{id=Id, obj=Obj, event=EvtA} ->
% Trace all messages.
% io:format("Process ~p event id=~p, obj=~p, event=~n ~p~n",
% [self(), Id, Obj, EvtA]),
% Each case must return carry_on or exit_normal.
CarryOn = case EvtA of
#wxClose{type=TypeA} ->
case TypeA of
% This is the event which is called when the window is closed.
close_window ->
io:format("Process ~p closing window id=~p, obj=~p~n",
[self(), Id, Obj]),
wxFrame:setStatusText(FrameA, "Closing soon...", []),
timer:sleep(2000),
wxWindow:destroy(FrameA),
% exit(normal);
exit_normal;
% This event is never received!?
end_session ->
io:format("Process ~p ending session id=~p, obj=~p~n",
[self(), Id, Obj]),
wxFrame:setStatusText(FrameA, "Ending session soon...", []),
timer:sleep(2000),
wxWindow:destroy(FrameA),
% exit(normal);
exit_normal;
_Else ->
carry_on
end;
#wxActivate{type=TypeA, active=Ac} ->
case TypeA of
activate ->
io:format("window activate: active=~p~n",
[Ac]);
activate_app ->
io:format("window activate app: active=~p~n",
[Ac]);
hibernate ->
io:format("window hibernate: active=~p~n",
[Ac]);
_Else ->
true
end,
carry_on;
#wxIconize{type=TypeA, iconized=Ic} ->
case TypeA of
iconize ->
io:format("window iconize: iconized=~p~n",
[Ic]);
_Else ->
true
end,
carry_on;
#wxKey{type=TypeA, keyCode=KeyC} ->
case TypeA of
char ->
io:format("char: keyCode=~p~n",
[KeyC]);
key_down ->
io:format("key down: keyCode=~p~n",
[KeyC]);
_Else ->
true
end,
carry_on;
#wxMouse{type=TypeA, x=X, y=Y} ->
case TypeA of
motion ->
io:format("mouse motion: "
"X=~p, Y=~p~n",
[X, Y]);
left_down ->
io:format("mouse left down: "
"X=~p, Y=~p~n",
[X, Y]);
left_up ->
io:format("mouse left up: "
"X=~p, Y=~p~n",
[X, Y]);
_Else ->
io:format("mouse event: type=~p, "
"X=~p, Y=~p~n",
[TypeA, X, Y])
end,
carry_on;
% The returned rectangle is always {0, 0, 0, 0}. Not good!?
#wxMove{type=TypeA, pos={Ws, Hs}, rect={Xr, Yr, Wr, Hr}} ->
case TypeA of
move ->
io:format("window move: "
"pos=[~p, ~p], rect: X=~p, Y=~p, W=~p, H=~p~n",
[Ws, Hs, Xr, Yr, Wr, Hr]);
_Else ->
io:format("window move: unknown type = ~p: "
"pos=[~p, ~p], rect: X=~p, Y=~p, W=~p, H=~p~n",
[TypeA, Ws, Hs, Xr, Yr, Wr, Hr])
end,
carry_on;
% The returned rectangle is always {0, 0, 0, 0}. Not good!?
#wxSize{type=TypeA, size={Ws, Hs}, rect={Xr, Yr, Wr, Hr}} ->
case TypeA of
size ->
io:format("window size: "
"size=[~p, ~p], rect: X=~p, Y=~p, W=~p, H=~p~n",
[Ws, Hs, Xr, Yr, Wr, Hr]);
_Else ->
io:format("window size: unknown type = ~p: "
"size=[~p, ~p], rect: X=~p, Y=~p, W=~p, H=~p~n",
[TypeA, Ws, Hs, Xr, Yr, Wr, Hr])
end,
carry_on;
#wxShow{type=TypeA} ->
case TypeA of
show ->
io:format("window show~n");
_Else ->
io:format("window show: unknown type=~p ", [TypeA])
end,
carry_on;
#wxWindowCreate{type=TypeA} ->
case TypeA of
create ->
io:format("window create~n");
_Else ->
io:format("window create: unknown type=~p ", [TypeA])
end,
carry_on;
#wxWindowDestroy{type=TypeA} ->
case TypeA of
destroy ->
io:format("window destroy~n");
_Else ->
io:format("window destroy: unknown type=~p ", [TypeA])
end,
carry_on;
% No matching event class.
_Else ->
carry_on
end,
% Use return value of previous "case" to decide whether to carry on.
% This will hopefully permit tail recursion, while also
% returning to the caller is the inner "case" terminates.
if
CarryOn == carry_on ->
handleWindowA(FrameA);
CarryOn == exit_normal ->
ok;
true ->
ok
end;
% All other event classes which are "connected".
Evt ->
io:format("Process ~p received event ~p~n", [self(), Evt]),
handleWindowA(FrameA)
end.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% This function is intended to be spawned from startMobSimA.
% It calls the event handler for the window process.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
startWindowA() ->
% Create a server object.
% See http://erlang.org/doc/man/wx.html#new-0
ServerA = wx:new(),
% Get some info on the object.
% See http://erlang.org/doc/man/wx.html#get_env-0
EnvA = wx:get_env(),
io:format("wx env = ~p~n", [EnvA]),
% See http://erlang.org/doc/man/erlang.html#ports-0
Plist = erlang:ports(),
io:format("Port list = ~p~n", [Plist]),
% See http://erlang.org/doc/man/wx.html#batch-1
% "Batches all wx commands used in the fun. Improves performance of the
% command processing by grabbing the wxWidgets thread so that no event
% processing will be done before the complete batch of commands is invoked."
% I don't understand that yet.
% See also http://erlang.org/doc/reference_manual/expressions.html#funs
% See also http://erlang.org/doc/reference_manual/data_types.html#id66389
% wx:batch/2 apparently returns the return value from the batched function.
FrameA = wx:batch(fun() -> createWindowA(ServerA) end),
% Show the frame.
io:format("Show wx frame~n", []),
wxWindow:show(FrameA),
% Go into a loop.
io:format("Start wx event handler~n", []),
handleWindowA(FrameA),
% Wait for a while.
% timer:sleep(5000),
% Destroy the wx server.
% There can only be one, I think. So there's no need to specify which one.
io:format("Destroy wx server~n", []),
wx:destroy(),
ok.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% A mobile device client process.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procMobSimA(Ntimes, PIDserver, Tsleep) when Ntimes > 0 ->
io:format("procMobSimA ~p sending msg ~p to ~p~n",
[self(), Ntimes, PIDserver]),
{ pidMobSimWindowA, PIDserver } ! { msg, self(), Ntimes },
% io:format("procMobSimA ~p waiting for response [~p]~n", [self(), Ntimes]),
% receive
% { resp, PIDserverRX, NtimesRX } ->
% io:format("procMobSimA ~p received response ~p from ~p~n",
% [self(), NtimesRX, PIDserverRX])
% end,
io:format("procMobSimA ~p sleep ~p~n", [self(), Tsleep]),
timer:sleep(Tsleep),
procMobSimA(Ntimes - 1, PIDserver, Tsleep);
procMobSimA(Ntimes, PIDserver, _Tsleep) when Ntimes =< 0 ->
io:format("procMobSimA ~p sending fin to server ~p [~p]~n",
[self(), PIDserver, Ntimes]),
{ pidMobSimWindowA, PIDserver } ! { fin, self(), Ntimes },
io:format("procMobSimA ~p END~n", [self()]).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Entry point for this module.
% This starts a window server for the mobile clients to send messages to.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Test on machine running "erl -sname serverD":
%
% (serverD@hostA)38> mobsim1:startMobSimA().
% mobsim1 process <0.103.0> spawning wxWindow process
% true
% wx env = {wx_env,#Port<0.11>,<0.122.0>,0}
% ....
%
% On second machine, running for example "erl -sname clientD":
%
% (clientD@hostA)147> mobsim1:startMobileA(serverD@hostA).
% procMobSimA <0.1624.0> sending msg 5 to serverD@hostA
% <0.1624.0>
% procMobSimA <0.1624.0> sleep 2500
% procMobSimA <0.1624.0> sending msg 4 to serverD@hostA
% procMobSimA <0.1624.0> sleep 2500
% procMobSimA <0.1624.0> sending msg 3 to serverD@hostA
% procMobSimA <0.1624.0> sleep 2500
% procMobSimA <0.1624.0> sending msg 2 to serverD@hostA
% procMobSimA <0.1624.0> sleep 2500
% procMobSimA <0.1624.0> sending msg 1 to serverD@hostA
% procMobSimA <0.1624.0> sleep 2500
% procMobSimA <0.1624.0> sending fin to server serverD@hostA [0]
% procMobSimA <0.1624.0> END
%
% Then should see messages on the server side.
% Process <0.120.0> received event {msg,<8292.1624.0>,5}
% Process <0.120.0> received event {msg,<8292.1624.0>,4}
% Process <0.120.0> received event {msg,<8292.1624.0>,3}
% Process <0.120.0> received event {msg,<8292.1624.0>,2}
% Process <0.120.0> received event {msg,<8292.1624.0>,1}
% Process <0.120.0> received event {fin,<8292.1624.0>,0}
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
startMobSimA() ->
io:format("mobsim1 process ~p spawning wxWindow process~n", [self()]),
register(pidMobSimWindowA, spawn(mobsim1, startWindowA, [])).
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
% Start a mobile devices, which is a client for the window process.
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
startMobileA(PIDserver) ->
spawn(mobsim1, procMobSimA, [5, PIDserver, 2500]).