diff options
Diffstat (limited to 'lib/agent')
-rw-r--r-- | lib/agent/apiv1.go | 42 | ||||
-rw-r--r-- | lib/agent/projects.go | 1 | ||||
-rw-r--r-- | lib/agent/xdsserver.go | 37 |
3 files changed, 49 insertions, 31 deletions
diff --git a/lib/agent/apiv1.go b/lib/agent/apiv1.go index d0e5a1c..a66f451 100644 --- a/lib/agent/apiv1.go +++ b/lib/agent/apiv1.go @@ -117,6 +117,30 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro // Declare passthrough routes s.sdksPassthroughInit(svr) + + // Register callback on Connection + svr.ConnectOn(func(server *XdsServer) error { + + // Add server to list + s.xdsServers[server.ID] = svr + + // Register event forwarder + if err := s.sdksEventsForwardInit(server); err != nil { + s.Log.Errorf("XDS Server %v - sdk event forwarding error: %v", server.ID, err) + } + + // Load projects + if err := s.projects.Init(server); err != nil { + s.Log.Errorf("XDS Server %v - project init error: %v", server.ID, err) + } + + // Registered to all events + if err := server.EventRegister(xsapiv1.EVTAll, ""); err != nil { + s.Log.Errorf("XDS Server %v - register all events error: %v", server.ID, err) + } + + return nil + }) } // Established connection @@ -127,24 +151,6 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro delete(s.xdsServers, cfg.ID) } - // Add to map - s.xdsServers[svr.ID] = svr - - // Register event forwarder - s.sdksEventsForwardInit(svr) - - // Load projects - if err == nil && svr.Connected { - err = s.projects.Init(svr) - } - - // Registered to all events - if err == nil && svr.Connected { - if err = svr.EventRegister(xsapiv1.EVTAll, ""); err != nil { - s.Log.Errorf("XDS Server %v - register all events error: %v", svr.ID, err) - } - } - return svr, err } diff --git a/lib/agent/projects.go b/lib/agent/projects.go index 7393364..4e8b0f6 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -54,6 +54,7 @@ func NewProjects(ctx *Context, st *st.SyncThing) *Projects { // Init Load Projects configuration func (p *Projects) Init(server *XdsServer) error { + svrList := make(map[string]*XdsServer) // If server not set, load for all servers if server == nil { diff --git a/lib/agent/xdsserver.go b/lib/agent/xdsserver.go index 3ec6123..346bdb9 100644 --- a/lib/agent/xdsserver.go +++ b/lib/agent/xdsserver.go @@ -54,16 +54,20 @@ type XdsServer struct { sockEventsLock *sync.Mutex // Private fields - client *common.HTTPClient - ioSock *sio_client.Client - logOut io.Writer - apiRouter *gin.RouterGroup - cmdList map[string]interface{} + client *common.HTTPClient + ioSock *sio_client.Client + logOut io.Writer + apiRouter *gin.RouterGroup + cmdList map[string]interface{} + cbOnConnect OnConnectedCB } // EventCB Event emitter callback type EventCB func(privData interface{}, evtData interface{}) error +// OnConnectedCB connect callback +type OnConnectedCB func(svr *XdsServer) error + // caller Used to chain event listeners type caller struct { id uuid.UUID @@ -135,6 +139,12 @@ func (xs *XdsServer) Connect() error { return err } +// ConnectOn Register a callback on events reception +func (xs *XdsServer) ConnectOn(f OnConnectedCB) error { + xs.cbOnConnect = f + return nil +} + // IsTempoID returns true when server as a temporary id func (xs *XdsServer) IsTempoID() bool { return strings.HasPrefix(xs.ID, _IDTempoPrefix) @@ -536,15 +546,10 @@ func (xs *XdsServer) _CreateConnectHTTP() error { // _Reconnect Re-established connection func (xs *XdsServer) _Reconnect() error { + + // Note that ConnectOn callback will be called (see apiv1.go file) err := xs._Connect(true) - if err == nil { - // Reload projects list for this server - err = xs.projects.Init(xs) - } - if err == nil { - // Register again to all events - err = xs.EventRegister(xsapiv1.EVTAll, "") - } + return err } @@ -575,6 +580,12 @@ func (xs *XdsServer) _Connect(reConn bool) error { } xs.Connected = true + + // Call OnConnect callback + if xs.cbOnConnect != nil { + xs.cbOnConnect(xs) + } + xs._NotifyState() return nil } |