@@ -55,32 +55,13 @@ type Monitor interface {
5555 // and the protocolVersion negotiated with the client.
5656 Hello (userAgent string , protocolVersion int ) error
5757
58- // StartSync is called to put the monitor in event mode. When the
59- // function returns the monitor must send port events ("add" or "remove")
60- // using the eventCB function.
61- StartSync (eventCB EventCallback , errorCB ErrorCallback ) error
62-
63- // Stop stops the monitor internal subroutines. If the monitor is
64- // in event mode it must stop sending events through the eventCB previously
65- // set.
66- Stop () error
58+ Describe () (* PortDescriptor , error )
6759
6860 // Quit is called just before the server terminates. This function can be
6961 // used by the monitor as a last chance gracefully close resources.
7062 Quit ()
7163}
7264
73- // EventCallback is a callback function to call to transmit port
74- // metadata when the monitor is in "sync" mode and a new event
75- // is detected.
76- type EventCallback func (event string , port * Port )
77-
78- // ErrorCallback is a callback function to signal unrecoverable errors to the
79- // client while the monitor is in event mode. Once the monitor signal an
80- // error it means that no more port-events will be delivered until the client
81- // performs a STOP+START_SYNC cycle.
82- type ErrorCallback func (err string )
83-
8465// A Server is a pluggable monitor protocol handler,
8566// it must be created using the NewServer function.
8667type Server struct {
@@ -89,10 +70,6 @@ type Server struct {
8970 userAgent string
9071 reqProtocolVersion int
9172 initialized bool
92- started bool
93- syncStarted bool
94- cachedPorts map [string ]* Port
95- cachedErr string
9673}
9774
9875// NewServer creates a new monitor server backed by the
@@ -132,14 +109,6 @@ func (d *Server) Run(in io.Reader, out io.Writer) error {
132109 switch cmd {
133110 case "HELLO" :
134111 d .hello (fullCmd [6 :])
135- case "START" :
136- d .start ()
137- case "LIST" :
138- d .list ()
139- case "START_SYNC" :
140- d .startSync ()
141- case "STOP" :
142- d .stop ()
143112 case "QUIT" :
144113 d .impl .Quit ()
145114 d .outputChan <- messageOk ("quit" )
@@ -180,102 +149,6 @@ func (d *Server) hello(cmd string) {
180149 d .initialized = true
181150}
182151
183- func (d * Server ) start () {
184- if d .started {
185- d .outputChan <- messageError ("start" , "Monitor already STARTed" )
186- return
187- }
188- if d .syncStarted {
189- d .outputChan <- messageError ("start" , "Monitor already START_SYNCed, cannot START" )
190- return
191- }
192- d .cachedPorts = map [string ]* Port {}
193- d .cachedErr = ""
194- if err := d .impl .StartSync (d .eventCallback , d .errorCallback ); err != nil {
195- d .outputChan <- messageError ("start" , "Cannot START: " + err .Error ())
196- return
197- }
198- d .started = true
199- d .outputChan <- messageOk ("start" )
200- }
201-
202- func (d * Server ) eventCallback (event string , port * Port ) {
203- id := port .Address + "|" + port .Protocol
204- if event == "add" {
205- d .cachedPorts [id ] = port
206- }
207- if event == "remove" {
208- delete (d .cachedPorts , id )
209- }
210- }
211-
212- func (d * Server ) errorCallback (msg string ) {
213- d .cachedErr = msg
214- }
215-
216- func (d * Server ) list () {
217- if ! d .started {
218- d .outputChan <- messageError ("list" , "Monitor not STARTed" )
219- return
220- }
221- if d .syncStarted {
222- d .outputChan <- messageError ("list" , "monitor already START_SYNCed, LIST not allowed" )
223- return
224- }
225- if d .cachedErr != "" {
226- d .outputChan <- messageError ("list" , d .cachedErr )
227- return
228- }
229- ports := []* Port {}
230- for _ , port := range d .cachedPorts {
231- ports = append (ports , port )
232- }
233- d .outputChan <- & message {
234- EventType : "list" ,
235- Ports : & ports ,
236- }
237- }
238-
239- func (d * Server ) startSync () {
240- if d .syncStarted {
241- d .outputChan <- messageError ("start_sync" , "Monitor already START_SYNCed" )
242- return
243- }
244- if d .started {
245- d .outputChan <- messageError ("start_sync" , "Monitor already STARTed, cannot START_SYNC" )
246- return
247- }
248- if err := d .impl .StartSync (d .syncEvent , d .errorEvent ); err != nil {
249- d .outputChan <- messageError ("start_sync" , "Cannot START_SYNC: " + err .Error ())
250- return
251- }
252- d .syncStarted = true
253- d .outputChan <- messageOk ("start_sync" )
254- }
255-
256- func (d * Server ) stop () {
257- if ! d .syncStarted && ! d .started {
258- d .outputChan <- messageError ("stop" , "Monitor already STOPped" )
259- return
260- }
261- if err := d .impl .Stop (); err != nil {
262- d .outputChan <- messageError ("stop" , "Cannot STOP: " + err .Error ())
263- return
264- }
265- d .started = false
266- if d .syncStarted {
267- d .syncStarted = false
268- }
269- d .outputChan <- messageOk ("stop" )
270- }
271-
272- func (d * Server ) syncEvent (event string , port * Port ) {
273- d .outputChan <- & message {
274- EventType : event ,
275- Port : port ,
276- }
277- }
278-
279152func (d * Server ) errorEvent (msg string ) {
280153 d .outputChan <- messageError ("start_sync" , msg )
281154}
0 commit comments