summaryrefslogtreecommitdiff
path: root/linkmsg.go
blob: 97ed141800dc50b0e299bec94fcda8087e179119 (plain)
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
package tanja

import (
	"bufio"
	"encoding/json"
	"errors"
	. "strconv"
)

type linkMsgPatternsync bool

type linkMsgRegister struct {
	pid int32
	pat Tuple
}

type linkMsgRegdone struct{}

type linkMsgUnregister int32

type linkMsgTuple struct {
	tid int32
	tup Tuple
}

type linkMsgReply struct {
	tid int32
	tup Tuple
}

type linkMsgClose int32

type linkMsgJSON struct {
	m interface{}
}

// Make sure JSON arrays are read as tuples. For anything else, just use the
// generic to-interface{} conversion.
func (j *linkMsgJSON) UnmarshalJSON(data []byte) (err error) {
	if data[0] == '[' {
		var t Tuple
		err = json.Unmarshal(data, &t)
		j.m = t
	} else {
		err = json.Unmarshal(data, &j.m)
	}
	return
}

func linkMsgRead(rd *bufio.Reader) (msg interface{}, err error) {
	var line []byte
	var prefix bool
	line, prefix, err = rd.ReadLine()
	if prefix || err != nil {
		if err == nil {
			err = errors.New("Read buffer overflow.")
		}
		return
	}
	var m []linkMsgJSON
	if err = json.Unmarshal(line, &m); err != nil || len(m) < 1 {
		if len(m) < 1 {
			err = errors.New("Invalid message type.")
		}
		return
	}
	// Validate and convert
	typ, ok := m[0].m.(float64)
	if !ok {
		err = errors.New("Invalid message type.")
		return
	}
	// Copy-pasting is bad, mkay?
	switch int32(typ) {
	case 1: // patternsync
		if len(m) != 2 {
			err = errors.New("Invalid number of arguments for the patternsync message.")
		} else if val, ok := m[1].m.(bool); ok {
			msg = linkMsgPatternsync(val)
		} else {
			err = errors.New("Invalid type of second argument to the patternsync message.")
		}
	case 2: // register
		var pid float64
		var pat Tuple
		if len(m) != 3 {
			err = errors.New("Invalid number of arguments for the register message.")
		} else if pid, ok = m[1].m.(float64); !ok {
			err = errors.New("Invalid type of second argument to the patternsync message.")
		} else if pat, ok = m[2].m.(Tuple); !ok {
			err = errors.New("Invalid type of third argument to the patternsync message.")
		} else {
			msg = &linkMsgRegister{int32(pid), pat}
		}
	case 3: // regdone
		if len(m) != 1 {
			err = errors.New("Invalid number of arguments for the regdone message.")
		} else {
			msg = linkMsgRegdone{}
		}
	case 4: // unregister
		if len(m) != 2 {
			err = errors.New("Invalid number of arguments for the unregister message.")
		} else if val, ok := m[1].m.(float64); ok {
			msg = linkMsgUnregister(int32(val))
		} else {
			err = errors.New("Invalid type of second argument to the unregister message.")
		}
	case 5: // tuple
		var tid float64
		var tup Tuple
		if len(m) != 3 {
			err = errors.New("Invalid number of arguments for the tuple message.")
		} else if tid, ok = m[1].m.(float64); !ok {
			err = errors.New("Invalid type of second argument to the tuple message.")
		} else if tup, ok = m[2].m.(Tuple); !ok {
			err = errors.New("Invalid type of third argument to the tuple message.")
		} else {
			msg = &linkMsgTuple{int32(tid), tup}
		}
	case 6: // reply
		var tid float64
		var tup Tuple
		if len(m) != 3 {
			err = errors.New("Invalid number of arguments for the reply message.")
		} else if tid, ok = m[1].m.(float64); !ok {
			err = errors.New("Invalid type of second argument to the reply message.")
		} else if tup, ok = m[2].m.(Tuple); !ok {
			err = errors.New("Invalid type of third argument to the reply message.")
		} else {
			msg = &linkMsgReply{int32(tid), tup}
		}
	case 7: // close
		if len(m) != 2 {
			err = errors.New("Invalid number of arguments for the close message.")
		} else if val, ok := m[1].m.(float64); ok {
			msg = linkMsgClose(int32(val))
		} else {
			err = errors.New("Invalid type of second argument to the close message.")
		}
	}
	// Ignore unknown messages by returning nil without error
	return
}

func linkMsgFmt(msg interface{}, buf []byte) []byte {
	// Somewhat low-level, but oh well
	switch m := msg.(type) {
	case linkMsgPatternsync:
		if m {
			buf = append(buf, []byte("[1,true]\n")...)
		} else {
			buf = append(buf, []byte("[1,false]\n")...)
		}
	case *linkMsgRegister:
		buf = append(buf, []byte("[2,")...)
		buf = append(buf, []byte(FormatInt(int64(m.pid), 10))...)
		buf = append(buf, ',')
		dat, _ := json.Marshal(m.pat)
		buf = append(buf, dat...)
		buf = append(buf, []byte("]\n")...)
	case linkMsgRegdone:
		buf = append(buf, []byte("[3]\n")...)
	case linkMsgUnregister:
		buf = append(buf, []byte("[4,")...)
		buf = append(buf, []byte(FormatInt(int64(m), 10))...)
		buf = append(buf, []byte("]\n")...)
	case *linkMsgTuple:
		buf = append(buf, []byte("[5,")...)
		buf = append(buf, []byte(FormatInt(int64(m.tid), 10))...)
		buf = append(buf, ',')
		dat, _ := json.Marshal(m.tup)
		buf = append(buf, dat...)
		buf = append(buf, []byte("]\n")...)
	case *linkMsgReply:
		buf = append(buf, []byte("[6,")...)
		buf = append(buf, []byte(FormatInt(int64(m.tid), 10))...)
		buf = append(buf, ',')
		dat, _ := json.Marshal(m.tup)
		buf = append(buf, dat...)
		buf = append(buf, []byte("]\n")...)
	case linkMsgClose:
		buf = append(buf, []byte("[7,")...)
		buf = append(buf, []byte(FormatInt(int64(m), 10))...)
		buf = append(buf, []byte("]\n")...)
	}
	return buf
}