Skip to content

Commit bd78450

Browse files
committed
Factored out some common operations
1 parent 74365e0 commit bd78450

File tree

1 file changed

+37
-106
lines changed

1 file changed

+37
-106
lines changed

discovery_server.go

Lines changed: 37 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -72,62 +72,38 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
7272
for {
7373
fullCmd, err := reader.ReadString('\n')
7474
if err != nil {
75-
d.output(&genericMessageJSON{
76-
EventType: "command_error",
77-
Error: true,
78-
Message: err.Error(),
79-
})
75+
d.outputError("command_error", err.Error())
8076
return err
8177
}
8278
split := strings.Split(fullCmd, " ")
8379
cmd := strings.ToUpper(strings.TrimSpace(split[0]))
8480

8581
if !d.initialized && cmd != "HELLO" {
86-
d.output(&genericMessageJSON{
87-
EventType: "command_error",
88-
Error: true,
89-
Message: fmt.Sprintf("First command must be HELLO, but got '%s'", cmd),
90-
})
82+
d.outputError("command_error", fmt.Sprintf("First command must be HELLO, but got '%s'", cmd))
9183
continue
9284
}
9385

9486
switch cmd {
9587
case "HELLO":
9688
if d.initialized {
97-
d.output(&genericMessageJSON{
98-
EventType: "hello",
99-
Error: true,
100-
Message: "HELLO already called",
101-
})
89+
d.outputError("hello", "HELLO already called")
10290
continue
10391
}
10492
re := regexp.MustCompile(`(\d+) "([^"]+)"`)
10593
matches := re.FindStringSubmatch(fullCmd[6:])
10694
if len(matches) != 3 {
107-
d.output(&genericMessageJSON{
108-
EventType: "hello",
109-
Error: true,
110-
Message: "Invalid HELLO command",
111-
})
95+
d.outputError("hello", "Invalid HELLO command")
11296
continue
11397
}
11498
d.userAgent = matches[2]
11599
if v, err := strconv.ParseInt(matches[1], 10, 64); err != nil {
116-
d.output(&genericMessageJSON{
117-
EventType: "hello",
118-
Error: true,
119-
Message: "Invalid protocol version: " + matches[2],
120-
})
100+
d.outputError("hello", "Invalid protocol version: "+matches[2])
121101
continue
122102
} else {
123103
d.reqProtocolVersion = int(v)
124104
}
125105
if err := d.impl.Hello(d.userAgent, 1); err != nil {
126-
d.output(&genericMessageJSON{
127-
EventType: "hello",
128-
Error: true,
129-
Message: err.Error(),
130-
})
106+
d.outputError("hello", err.Error())
131107
continue
132108
}
133109
d.output(&genericMessageJSON{
@@ -139,58 +115,31 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
139115

140116
case "START":
141117
if d.started {
142-
d.output(&genericMessageJSON{
143-
EventType: "start",
144-
Error: true,
145-
Message: "Discovery already STARTed",
146-
})
118+
d.outputError("start", "Discovery already STARTed")
147119
continue
148120
}
149121
if d.syncStarted {
150-
d.output(&genericMessageJSON{
151-
EventType: "start",
152-
Error: true,
153-
Message: "Discovery already START_SYNCed, cannot START",
154-
})
122+
d.outputError("start", "Discovery already START_SYNCed, cannot START")
155123
continue
156124
}
157125
if err := d.impl.Start(); err != nil {
158-
d.output(&genericMessageJSON{
159-
EventType: "start",
160-
Error: true,
161-
Message: "Cannot START: " + err.Error(),
162-
})
126+
d.outputError("start", "Cannot START: "+err.Error())
163127
continue
164128
}
165129
d.started = true
166-
d.output(&genericMessageJSON{
167-
EventType: "start",
168-
Message: "OK",
169-
})
130+
d.outputOk("start")
170131

171132
case "LIST":
172133
if !d.started {
173-
d.output(&genericMessageJSON{
174-
EventType: "list",
175-
Error: true,
176-
Message: "Discovery not STARTed",
177-
})
134+
d.outputError("list", "Discovery not STARTed")
178135
continue
179136
}
180137
if d.syncStarted {
181-
d.output(&genericMessageJSON{
182-
EventType: "list",
183-
Error: true,
184-
Message: "discovery already START_SYNCed, LIST not allowed",
185-
})
138+
d.outputError("list", "discovery already START_SYNCed, LIST not allowed")
186139
continue
187140
}
188141
if ports, err := d.impl.List(); err != nil {
189-
d.output(&genericMessageJSON{
190-
EventType: "list",
191-
Error: true,
192-
Message: "LIST error: " + err.Error(),
193-
})
142+
d.outputError("list", "LIST error: "+err.Error())
194143
continue
195144
} else {
196145
type listOutputJSON struct {
@@ -205,52 +154,29 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
205154

206155
case "START_SYNC":
207156
if d.syncStarted {
208-
d.output(&genericMessageJSON{
209-
EventType: "start_sync",
210-
Error: true,
211-
Message: "Discovery already START_SYNCed",
212-
})
157+
d.outputError("start_sync", "Discovery already START_SYNCed")
213158
continue
214159
}
215160
if d.started {
216-
d.output(&genericMessageJSON{
217-
EventType: "start_sync",
218-
Error: true,
219-
Message: "Discovery already STARTed, cannot START_SYNC",
220-
})
161+
d.outputError("start_sync", "Discovery already STARTed, cannot START_SYNC")
221162
continue
222163
}
223164
if c, err := d.impl.StartSync(d.syncEvent); err != nil {
224-
d.output(&genericMessageJSON{
225-
EventType: "start_sync",
226-
Error: true,
227-
Message: "Cannot START_SYNC: " + err.Error(),
228-
})
165+
d.outputError("start_sync", "Cannot START_SYNC: "+err.Error())
229166
continue
230167
} else {
231168
d.syncCloseChan = c
232169
d.syncStarted = true
233-
d.output(&genericMessageJSON{
234-
EventType: "start_sync",
235-
Message: "OK",
236-
})
170+
d.outputOk("start_sync")
237171
}
238172

239173
case "STOP":
240174
if !d.syncStarted && !d.started {
241-
d.output(&genericMessageJSON{
242-
EventType: "stop",
243-
Error: true,
244-
Message: "Discovery already STOPped",
245-
})
175+
d.outputError("stop", "Discovery already STOPped")
246176
continue
247177
}
248178
if err := d.impl.Stop(); err != nil {
249-
d.output(&genericMessageJSON{
250-
EventType: "stop",
251-
Error: true,
252-
Message: "Cannot STOP: " + err.Error(),
253-
})
179+
d.outputError("stop", "Cannot STOP: "+err.Error())
254180
continue
255181
}
256182
if d.started {
@@ -261,24 +187,14 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
261187
close(d.syncCloseChan)
262188
d.syncStarted = false
263189
}
264-
d.output(&genericMessageJSON{
265-
EventType: "stop",
266-
Message: "OK",
267-
})
190+
d.outputOk("stop")
268191

269192
case "QUIT":
270-
d.output(&genericMessageJSON{
271-
EventType: "quit",
272-
Message: "OK",
273-
})
193+
d.outputOk("quit")
274194
return nil
275195

276196
default:
277-
d.output(&genericMessageJSON{
278-
EventType: "command_error",
279-
Error: true,
280-
Message: fmt.Sprintf("Command %s not supported", cmd),
281-
})
197+
d.outputError("command_error", fmt.Sprintf("Command %s not supported", cmd))
282198
}
283199
}
284200
}
@@ -301,6 +217,21 @@ type genericMessageJSON struct {
301217
ProtocolVersion int `json:"protocolVersion,omitempty"`
302218
}
303219

220+
func (d *DiscoveryServer) outputOk(event string) {
221+
d.output(&genericMessageJSON{
222+
EventType: event,
223+
Message: "OK",
224+
})
225+
}
226+
227+
func (d *DiscoveryServer) outputError(event, msg string) {
228+
d.output(&genericMessageJSON{
229+
EventType: event,
230+
Error: true,
231+
Message: msg,
232+
})
233+
}
234+
304235
func (d *DiscoveryServer) output(msg interface{}) {
305236
data, err := json.MarshalIndent(msg, "", " ")
306237
if err != nil {

0 commit comments

Comments
 (0)