diff options
Diffstat (limited to 'lib/agent')
-rw-r--r-- | lib/agent/apiv1-targets.go | 105 | ||||
-rw-r--r-- | lib/agent/apiv1.go | 6 | ||||
-rw-r--r-- | lib/agent/projects.go | 2 |
3 files changed, 112 insertions, 1 deletions
diff --git a/lib/agent/apiv1-targets.go b/lib/agent/apiv1-targets.go index 5a7862a..cc08822 100644 --- a/lib/agent/apiv1-targets.go +++ b/lib/agent/apiv1-targets.go @@ -18,6 +18,7 @@ package agent import ( + "encoding/json" "fmt" "net/http" @@ -52,6 +53,110 @@ func (s *APIService) targetsPassthroughInit(svr *XdsServer) error { return nil } +// targetsEventsForwardInit Register events forwarder for targets +func (s *APIService) targetsEventsForwardInit(svr *XdsServer) error { + + if !svr.Connected { + return fmt.Errorf("Cannot register events: XDS Server %v not connected", svr.ID) + } + + // Forward Target events from XDS-server to client + if _, err := svr.EventOn(xsapiv1.EVTTargetAdd, xaapiv1.EVTTargetAdd, s._targetsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetAdd, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetRemove, xaapiv1.EVTTargetRemove, s._targetsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetRemove, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetStateChange, xaapiv1.EVTTargetStateChange, s._targetsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetStateChange, err) + return err + } + + return nil +} + +func (s *APIService) _targetsEventCB(privD interface{}, data interface{}) error { + evt := xsapiv1.EventMsg{} + d, err := json.Marshal(data) + if err != nil { + s.Log.Errorf("Cannot marshal XDS Server Target event err=%v, data=%v", err, data) + return err + } + if err = json.Unmarshal(d, &evt); err != nil { + s.Log.Errorf("Cannot unmarshal XDS Server Target event err=%v, d=%v", err, string(d)) + return err + } + + // assume that xsapiv1.TargetConfig == xaapiv1.TargetConfig + target, err := evt.DecodeTargetEvent() + if err != nil { + s.Log.Errorf("Cannot decode XDS Server Target event: err=%v, data=%v", err, data) + return err + } + + evtName := privD.(string) + + if err := s.events.Emit(evtName, target, ""); err != nil { + s.Log.Warningf("Cannot notify %s (from server): %v", evtName, err) + return err + } + return nil +} + +// terminalsEventsForwardInit Register events forwarder for terminals +func (s *APIService) terminalsEventsForwardInit(svr *XdsServer) error { + + if !svr.Connected { + return fmt.Errorf("Cannot register events: XDS Server %v not connected", svr.ID) + } + + // Forward Terminal events from XDS-server to client + if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalAdd, xaapiv1.EVTTargetTerminalAdd, s._terminalsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalAdd, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalRemove, xaapiv1.EVTTargetTerminalRemove, s._terminalsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalRemove, err) + return err + } + if _, err := svr.EventOn(xsapiv1.EVTTargetTerminalStateChange, xaapiv1.EVTTargetTerminalStateChange, s._terminalsEventCB); err != nil { + s.Log.Errorf("XDS Server EventOn '%s' failed: %v", xsapiv1.EVTTargetTerminalStateChange, err) + return err + } + + return nil +} + +func (s *APIService) _terminalsEventCB(privD interface{}, data interface{}) error { + evt := xsapiv1.EventMsg{} + d, err := json.Marshal(data) + if err != nil { + s.Log.Errorf("Cannot marshal XDS Server Target event err=%v, data=%v", err, data) + return err + } + if err = json.Unmarshal(d, &evt); err != nil { + s.Log.Errorf("Cannot unmarshal XDS Server Target event err=%v, d=%v", err, string(d)) + return err + } + + // assume that xsapiv1.TargetConfig == xaapiv1.TargetConfig + target, err := evt.DecodeTerminalEvent() + if err != nil { + s.Log.Errorf("Cannot decode XDS Server Target event: err=%v, data=%v", err, data) + return err + } + + evtName := privD.(string) + + if err := s.events.Emit(evtName, target, ""); err != nil { + s.Log.Warningf("Cannot notify %s (from server): %v", evtName, err) + return err + } + return nil +} + // GetServerFromTargetID Retrieve XDS Server definition from a target ID func (s *APIService) GetServerFromTargetID(targetID, termID string) (*XdsServer, string, error) { diff --git a/lib/agent/apiv1.go b/lib/agent/apiv1.go index 730e7c0..97165b3 100644 --- a/lib/agent/apiv1.go +++ b/lib/agent/apiv1.go @@ -137,6 +137,12 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro if err := s.sdksEventsForwardInit(server); err != nil { s.Log.Errorf("XDS Server %v - sdk events forwarding error: %v", server.ID, err) } + if err := s.targetsEventsForwardInit(server); err != nil { + s.Log.Errorf("XDS Server %v - target events forwarding error: %v", server.ID, err) + } + if err := s.terminalsEventsForwardInit(server); err != nil { + s.Log.Errorf("XDS Server %v - terminal events forwarding error: %v", server.ID, err) + } // Load projects if err := s.projects.Init(server); err != nil { diff --git a/lib/agent/projects.go b/lib/agent/projects.go index ff28f96..0bd5315 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -26,10 +26,10 @@ import ( "time" st "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/syncthing" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1" "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent/lib/xaapiv1" common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" - "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1" "github.com/franciscocpg/reflectme" "github.com/syncthing/syncthing/lib/sync" ) |