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
|
implement Wbserve;
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Chans, Display, Image, Rect, Point : import draw;
Wbserve : module {
init : fn (ctxt : ref Draw->Context, args : list of string);
};
WBW : con 600;
WBH : con 400;
savefile := "";
init(nil: ref Draw->Context, args: list of string)
{
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
if (draw == nil)
badmod(Draw->PATH);
if (args == nil || tl args == nil)
error("usage: wbsrv mntpt [savefile]");
args = tl args;
mntpt := hd args;
args = tl args;
display := Display.allocate(nil);
if (display == nil)
error(sys->sprint("cannot allocate display: %r"));
bg: ref Draw->Image;
if (args != nil) {
savefile = hd args;
bg = display.open(savefile);
}
r := Rect(Point(0,0), Point(WBW, WBH));
wb := display.newimage(r, Draw->CMAP8, 0, Draw->White);
if (wb == nil)
error(sys->sprint("cannot allocate whiteboard image: %r"));
if (bg != nil) {
wb.draw(bg.r, bg, nil, Point(0,0));
bg = nil;
}
nextmsg = ref Msg (nil, nil);
sys->bind("#s", mntpt, Sys->MBEFORE);
bit := sys->file2chan(mntpt, "wb.bit");
strokes := sys->file2chan(mntpt, "strokes");
spawn srv(wb, bit, strokes);
if (savefile != nil)
spawn saveit(display, wb);
}
srv(wb: ref Image, bit, strokes: ref Sys->FileIO)
{
nwbbytes := draw->bytesperline(wb.r, wb.depth) * wb.r.dy();
bithdr := sys->aprint("%11s %11d %11d %11d %11d ", wb.chans.text(), 0, 0, WBW, WBH);
for (;;) alt {
(offset, count, fid, r) := <-bit.read =>
if (r == nil) {
closeclient(fid);
continue;
}
c := getclient(fid);
if (c == nil) {
# new client
c = newclient(fid);
data := array [len bithdr + nwbbytes] of byte;
data[0:] = bithdr;
wb.readpixels(wb.r, data[len bithdr:]);
c.bitdata = data;
}
if (offset >= len c.bitdata) {
rreply(r, (nil, nil));
continue;
}
rreply(r, (c.bitdata[offset:], nil));
(offset, data, fid, w) := <-bit.write =>
if (w != nil)
wreply(w, (0, "permission denied"));
(offset, count, fid, r) := <-strokes.read =>
if (r == nil) {
closeclient(fid);
continue;
}
c := getclient(fid);
if (c == nil) {
c = newclient(fid);
c.nextmsg = nextmsg;
}
d := c.nextmsg.data;
if (d == nil) {
c.pending = r;
c.pendlen = count;
continue;
}
c.nextmsg = c.nextmsg.next;
rreply(r, (d, nil));
(offset, data, fid, w) := <-strokes.write =>
if (w == nil) {
closeclient(fid);
continue;
}
err := drawstrokes(wb, data);
if (err != nil) {
wreply(w, (0, err));
continue;
}
wreply(w, (len data, nil));
writeclients(data);
}
}
rreply(rc: chan of (array of byte, string), reply: (array of byte, string))
{
alt {
rc <-= reply =>;
* =>;
}
}
wreply(wc: chan of (int, string), reply: (int, string))
{
alt {
wc <-= reply=>;
* =>;
}
}
export(fd : ref Sys->FD, done : chan of int)
{
sys->export(fd, "/", Sys->EXPWAIT);
done <-= 1;
}
Msg : adt {
data : array of byte;
next : cyclic ref Msg;
};
Client : adt {
fid : int;
bitdata : array of byte; # bit file client
nextmsg : ref Msg; # strokes file client
pending : Sys->Rread;
pendlen : int;
};
nextmsg : ref Msg;
clients : list of ref Client;
newclient(fid : int) : ref Client
{
c := ref Client(fid, nil, nil, nil, 0);
clients = c :: clients;
return c;
}
getclient(fid : int) : ref Client
{
for(cl := clients; cl != nil; cl = tl cl)
if((c := hd cl).fid == fid)
return c;
return nil;
}
closeclient(fid : int)
{
nl: list of ref Client;
for(cl := clients; cl != nil; cl = tl cl)
if((hd cl).fid != fid)
nl = hd cl :: nl;
clients = nl;
}
writeclients(data : array of byte)
{
nm := ref Msg(nil, nil);
nextmsg.data = data;
nextmsg.next = nm;
for(cl := clients; cl != nil; cl = tl cl){
if ((c := hd cl).pending != nil) {
n := c.pendlen;
if (n > len data)
n = len data;
alt{
c.pending <-= (data[0:n], nil) => ;
* => ;
}
c.pending = nil;
c.nextmsg = nm;
}
}
nextmsg = nm;
}
# data: colour width p0 p1 pn*
pencol: int;
pen: ref Image;
drawstrokes(wb: ref Image, data : array of byte) : string
{
(n, toks) := sys->tokenize(string data, " ");
if (n < 6 || n & 1)
return "bad data";
colour, width, x, y : int;
(colour, toks) = (int hd toks, tl toks);
(width, toks) = (int hd toks, tl toks);
(x, toks) = (int hd toks, tl toks);
(y, toks) = (int hd toks, tl toks);
if (pen == nil || colour != pencol) {
pencol = colour;
pen = wb.display.newimage(Rect(Point(0,0), Point(1,1)), Draw->CMAP8, 1, pencol);
}
p0 := Point(x, y);
while (toks != nil) {
(x, toks) = (int hd toks, tl toks);
(y, toks) = (int hd toks, tl toks);
p1 := Point(x, y);
# could use poly() instead of line()
wb.line(p0, p1, Draw->Enddisc, Draw->Enddisc, width, pen, pen.r.min);
p0 = p1;
}
return nil;
}
error(e: string)
{
sys->fprint(stderr(), "wbsrv: %s\n", e);
raise "fail:error";
}
stderr(): ref Sys->FD
{
return sys->fildes(2);
}
badmod(path: string)
{
sys->fprint(stderr(), "wbsrv: cannot load %s: %r\n", path);
exit;
}
saveit(display: ref Display, img: ref Image)
{
for (;;) {
sys->sleep(300000);
fd := sys->open(savefile, sys->OWRITE);
if (fd == nil)
exit;
display.writeimage(fd, img);
}
}
|