Skip to content

Commit 85d0f95

Browse files
committed
Moved command handlers in their own method
1 parent bd78450 commit 85d0f95

File tree

1 file changed

+115
-101
lines changed

1 file changed

+115
-101
lines changed

discovery_server.go

Lines changed: 115 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -85,120 +85,134 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
8585

8686
switch cmd {
8787
case "HELLO":
88-
if d.initialized {
89-
d.outputError("hello", "HELLO already called")
90-
continue
91-
}
92-
re := regexp.MustCompile(`(\d+) "([^"]+)"`)
93-
matches := re.FindStringSubmatch(fullCmd[6:])
94-
if len(matches) != 3 {
95-
d.outputError("hello", "Invalid HELLO command")
96-
continue
97-
}
98-
d.userAgent = matches[2]
99-
if v, err := strconv.ParseInt(matches[1], 10, 64); err != nil {
100-
d.outputError("hello", "Invalid protocol version: "+matches[2])
101-
continue
102-
} else {
103-
d.reqProtocolVersion = int(v)
104-
}
105-
if err := d.impl.Hello(d.userAgent, 1); err != nil {
106-
d.outputError("hello", err.Error())
107-
continue
108-
}
109-
d.output(&genericMessageJSON{
110-
EventType: "hello",
111-
ProtocolVersion: 1, // Protocol version 1 is the only supported for now...
112-
Message: "OK",
113-
})
114-
d.initialized = true
115-
88+
d.hello(fullCmd[6:])
11689
case "START":
117-
if d.started {
118-
d.outputError("start", "Discovery already STARTed")
119-
continue
120-
}
121-
if d.syncStarted {
122-
d.outputError("start", "Discovery already START_SYNCed, cannot START")
123-
continue
124-
}
125-
if err := d.impl.Start(); err != nil {
126-
d.outputError("start", "Cannot START: "+err.Error())
127-
continue
128-
}
129-
d.started = true
130-
d.outputOk("start")
131-
90+
d.start()
13291
case "LIST":
133-
if !d.started {
134-
d.outputError("list", "Discovery not STARTed")
135-
continue
136-
}
137-
if d.syncStarted {
138-
d.outputError("list", "discovery already START_SYNCed, LIST not allowed")
139-
continue
140-
}
141-
if ports, err := d.impl.List(); err != nil {
142-
d.outputError("list", "LIST error: "+err.Error())
143-
continue
144-
} else {
145-
type listOutputJSON struct {
146-
EventType string `json:"eventType"`
147-
Ports []*Port `json:"ports"`
148-
}
149-
d.output(&listOutputJSON{
150-
EventType: "list",
151-
Ports: ports,
152-
})
153-
}
154-
92+
d.list()
15593
case "START_SYNC":
156-
if d.syncStarted {
157-
d.outputError("start_sync", "Discovery already START_SYNCed")
158-
continue
159-
}
160-
if d.started {
161-
d.outputError("start_sync", "Discovery already STARTed, cannot START_SYNC")
162-
continue
163-
}
164-
if c, err := d.impl.StartSync(d.syncEvent); err != nil {
165-
d.outputError("start_sync", "Cannot START_SYNC: "+err.Error())
166-
continue
167-
} else {
168-
d.syncCloseChan = c
169-
d.syncStarted = true
170-
d.outputOk("start_sync")
171-
}
172-
94+
d.startSync()
17395
case "STOP":
174-
if !d.syncStarted && !d.started {
175-
d.outputError("stop", "Discovery already STOPped")
176-
continue
177-
}
178-
if err := d.impl.Stop(); err != nil {
179-
d.outputError("stop", "Cannot STOP: "+err.Error())
180-
continue
181-
}
182-
if d.started {
183-
d.started = false
184-
}
185-
if d.syncStarted {
186-
d.syncCloseChan <- true
187-
close(d.syncCloseChan)
188-
d.syncStarted = false
189-
}
190-
d.outputOk("stop")
191-
96+
d.stop()
19297
case "QUIT":
19398
d.outputOk("quit")
19499
return nil
195-
196100
default:
197101
d.outputError("command_error", fmt.Sprintf("Command %s not supported", cmd))
198102
}
199103
}
200104
}
201105

106+
func (d *DiscoveryServer) hello(cmd string) {
107+
if d.initialized {
108+
d.outputError("hello", "HELLO already called")
109+
return
110+
}
111+
re := regexp.MustCompile(`(\d+) "([^"]+)"`)
112+
matches := re.FindStringSubmatch(cmd)
113+
if len(matches) != 3 {
114+
d.outputError("hello", "Invalid HELLO command")
115+
return
116+
}
117+
d.userAgent = matches[2]
118+
if v, err := strconv.ParseInt(matches[1], 10, 64); err != nil {
119+
d.outputError("hello", "Invalid protocol version: "+matches[2])
120+
return
121+
} else {
122+
d.reqProtocolVersion = int(v)
123+
}
124+
if err := d.impl.Hello(d.userAgent, 1); err != nil {
125+
d.outputError("hello", err.Error())
126+
return
127+
}
128+
d.output(&genericMessageJSON{
129+
EventType: "hello",
130+
ProtocolVersion: 1, // Protocol version 1 is the only supported for now...
131+
Message: "OK",
132+
})
133+
d.initialized = true
134+
}
135+
136+
func (d *DiscoveryServer) start() {
137+
if d.started {
138+
d.outputError("start", "Discovery already STARTed")
139+
return
140+
}
141+
if d.syncStarted {
142+
d.outputError("start", "Discovery already START_SYNCed, cannot START")
143+
return
144+
}
145+
if err := d.impl.Start(); err != nil {
146+
d.outputError("start", "Cannot START: "+err.Error())
147+
return
148+
}
149+
d.started = true
150+
d.outputOk("start")
151+
}
152+
153+
func (d *DiscoveryServer) list() {
154+
if !d.started {
155+
d.outputError("list", "Discovery not STARTed")
156+
return
157+
}
158+
if d.syncStarted {
159+
d.outputError("list", "discovery already START_SYNCed, LIST not allowed")
160+
return
161+
}
162+
if ports, err := d.impl.List(); err != nil {
163+
d.outputError("list", "LIST error: "+err.Error())
164+
return
165+
} else {
166+
type listOutputJSON struct {
167+
EventType string `json:"eventType"`
168+
Ports []*Port `json:"ports"`
169+
}
170+
d.output(&listOutputJSON{
171+
EventType: "list",
172+
Ports: ports,
173+
})
174+
}
175+
}
176+
177+
func (d *DiscoveryServer) startSync() {
178+
if d.syncStarted {
179+
d.outputError("start_sync", "Discovery already START_SYNCed")
180+
return
181+
}
182+
if d.started {
183+
d.outputError("start_sync", "Discovery already STARTed, cannot START_SYNC")
184+
return
185+
}
186+
if c, err := d.impl.StartSync(d.syncEvent); err != nil {
187+
d.outputError("start_sync", "Cannot START_SYNC: "+err.Error())
188+
return
189+
} else {
190+
d.syncCloseChan = c
191+
d.syncStarted = true
192+
d.outputOk("start_sync")
193+
}
194+
}
195+
196+
func (d *DiscoveryServer) stop() {
197+
if !d.syncStarted && !d.started {
198+
d.outputError("stop", "Discovery already STOPped")
199+
return
200+
}
201+
if err := d.impl.Stop(); err != nil {
202+
d.outputError("stop", "Cannot STOP: "+err.Error())
203+
return
204+
}
205+
if d.started {
206+
d.started = false
207+
}
208+
if d.syncStarted {
209+
d.syncCloseChan <- true
210+
close(d.syncCloseChan)
211+
d.syncStarted = false
212+
}
213+
d.outputOk("stop")
214+
}
215+
202216
func (d *DiscoveryServer) syncEvent(event string, port *Port) {
203217
type syncOutputJSON struct {
204218
EventType string `json:"eventType"`

0 commit comments

Comments
 (0)