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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2012. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
%% ------------------------------------------------------------
%% distrib_draw
%% Shows one way of making two nodes draw on the same
%% area.
%%
%% ------------------------------------------------------------
%% HOW TO USE:
%% 1) start up two nodes with same cookie.
%% They can be on any machines.
%%
%% sid.ericsson.se> erl -cookie kaka -name hans
%%
%% ozzy.ericsson.se> erl -cookie kaka -name greta
%%
%%
%% 2) Make them aware of each other.
%% <greta@ozzy.ericsson.se> net:ping('hans@sid.ericsson.se').
%%
%% 3) Start up distrib_draw from either node.
%% <greta@ozzy.ericsson.se> distrib_draw('hans@sid.ericsson.se',
%% 'greta@ozzy.ericsson.se').
%%
-module(distrib_draw).
-compile([{nowarn_deprecated_function,{gs,canvas,3}},
{nowarn_deprecated_function,{gs,config,2}},
{nowarn_deprecated_function,{gs,line,2}},
{nowarn_deprecated_function,{gs,start,0}},
{nowarn_deprecated_function,{gs,window,3}}]).
-export([start/2,init/0]).
start(Node1,Node2) ->
Pid1=spawn(Node1,distrib_draw,init,[]),
Pid2=spawn(Node2,distrib_draw,init,[]),
Pid1 ! {connect,red,Pid2},
Pid2 ! {connect,green,Pid1}.
init() ->
process_flag(trap_exit,true),
S=gs:start(),
receive
{connect,Color,Pid} ->
link(Pid),
gs:window(win,S,[{buttonpress,true},{buttonrelease,true},
{configure,true},{title,Color},{map,true}]),
gs:canvas(canvas,win,[{bg,grey},{width,300},{height,200}]),
draw0(0,0,Color,Pid)
after
3000 -> exit(timeout)
end.
%% not drawing state
draw0(X0,Y0,Color,Pid) ->
receive
{gs,_,buttonpress,_,[1,X,Y|_]} ->
gs:config(win,{motion,true}),
draw1(X,Y,Color,Pid);
{draw,Coords,Col2} ->
gs:line(canvas,[{coords,Coords},{width,2},{fg,Col2}]),
draw0(X0,Y0,Color,Pid);
{gs,_,configure,_,[300,200|_]} ->
draw0(X0,Y0,Color,Pid);
{gs,_,configure,_,_} ->
gs:config(win,[{width,300},{height,200}]),
draw0(X0,Y0,Color,Pid);
{gs,_,destroy,_,_} ->
exit(normal);
{'EXIT',_,_} ->
exit(normal);
_X -> draw1(X0,Y0,Color,Pid)
end.
%% i'm now drawing
draw1(X0,Y0,Color,Pid) ->
receive
{gs,_,motion,_,[X,Y|_]} ->
Pid ! {draw,[{X0,Y0},{X,Y}],Color},
gs:line(canvas,[{coords,[{X0,Y0},{X,Y}]},{width,2},{fg,Color}]),
draw1(X,Y,Color,Pid);
{draw,Coords,Col2} ->
gs:line(canvas,[{coords,Coords},{width,2},{fg,Col2}]),
draw1(X0,Y0,Color,Pid);
{gs,_,buttonrelease,_,[1,X,Y|_]} ->
gs:config(win,{motion,false}),
draw0(X,Y,Color,Pid);
{gs,_,configure,_,[300,200|_]} ->
draw0(X0,Y0,Color,Pid);
{gs,_,configure,_,_} ->
gs:config(win,[{width,300},{height,200}]),
draw0(X0,Y0,Color,Pid);
{gs,_,destroy,_,_} ->
exit(normal);
{'EXIT',_,_} ->
exit(normal);
_X -> draw1(X0,Y0,Color,Pid)
end.
%% ------------------------------------------------------------
%% end of 'distrib_draw'
|