Commit | Line | Data |
---|---|---|
36e42cb8 AC |
1 | (* HCoop Domtool (http://hcoop.sourceforge.net/) |
2 | * Copyright (c) 2006, Adam Chlipala | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version 2 | |
7 | * of the License, or (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | *) | |
18 | ||
19 | (* Network messages *) | |
20 | ||
21 | structure Msg :> MSG = struct | |
22 | ||
23 | open OpenSSL MsgTypes Slave | |
24 | ||
25 | val a2i = fn Add => 0 | |
26 | | Delete => 1 | |
27 | | Modify => 2 | |
28 | ||
29 | val i2a = fn 0 => Add | |
30 | | 1 => Delete | |
31 | | 2 => Modify | |
32 | | _ => raise OpenSSL.OpenSSL "Bad action number to deserialize" | |
33 | ||
34 | fun send (bio, m) = | |
35 | case m of | |
36 | MsgOk => OpenSSL.writeInt (bio, 1) | |
37 | | MsgError s => (OpenSSL.writeInt (bio, 2); | |
38 | OpenSSL.writeString (bio, s)) | |
39 | | MsgConfig s => (OpenSSL.writeInt (bio, 3); | |
40 | OpenSSL.writeString (bio, s)) | |
41 | | MsgFile {action, domain, dir, file} => | |
42 | (OpenSSL.writeInt (bio, 4); | |
43 | OpenSSL.writeInt (bio, a2i action); | |
44 | OpenSSL.writeString (bio, domain); | |
45 | OpenSSL.writeString (bio, dir); | |
46 | OpenSSL.writeString (bio, file)) | |
47 | | MsgDoFiles => OpenSSL.writeInt (bio, 5) | |
48 | ||
49 | fun checkIt v = | |
50 | case v of | |
51 | NONE => raise OpenSSL.OpenSSL "Bad Msg format" | |
52 | | _ => v | |
53 | ||
54 | fun recv bio = | |
55 | case OpenSSL.readInt bio of | |
56 | NONE => NONE | |
57 | | SOME n => | |
58 | checkIt (case n of | |
59 | 1 => SOME MsgOk | |
60 | | 2 => Option.map MsgError (OpenSSL.readString bio) | |
61 | | 3 => Option.map MsgConfig (OpenSSL.readString bio) | |
62 | | 4 => (case (OpenSSL.readInt bio, | |
63 | OpenSSL.readString bio, | |
64 | OpenSSL.readString bio, | |
65 | OpenSSL.readString bio) of | |
66 | (SOME action, SOME domain, SOME dir, SOME file) => | |
67 | SOME (MsgFile {action = i2a action, | |
68 | domain = domain, | |
69 | dir = dir, | |
70 | file = file}) | |
71 | | _ => NONE) | |
72 | | 5 => SOME MsgDoFiles | |
73 | | _ => NONE) | |
74 | ||
75 | end |