2 * Copyright (C) 2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
27 socketio_client "github.com/sebd71/go-socket.io-client"
30 // Disconnection Channel used to notify XDS Server disconnection
31 type Disconnection struct {
34 svrConf xaapiv1.ServerCfg
38 type IOSockClient struct {
40 Conn *socketio_client.Client
41 Options *socketio_client.Options
44 ServerDiscoChan chan Disconnection
48 // NewIoSocketClient Create a new IOSockClient
49 func NewIoSocketClient(url, clientID string) (*IOSockClient, error) {
53 sCli := &IOSockClient{
55 EmitMutex: &sync.Mutex{},
56 Options: &socketio_client.Options{
57 Transport: "websocket",
58 Header: make(map[string][]string),
60 ServerDiscoChan: make(chan Disconnection, 1),
62 sCli.Options.Header["XDS-AGENT-SID"] = []string{clientID}
64 sCli.Conn, err = socketio_client.NewClient(url, sCli.Options)
66 return nil, fmt.Errorf("IO.socket connection error: " + err.Error())
69 sCli.Conn.On("connection", func() {
73 sCli.Conn.On("disconnection", func(err error) {
74 Log.Debugf("WS disconnection event with err: %v\n", err)
75 sCli.Connected = false
78 args := xaapiv1.EventRegisterArgs{Name: xaapiv1.EVTServerConfig}
79 if err := HTTPCli.Post("/events/register", args, nil); err != nil {
83 sCli.Conn.On(xaapiv1.EVTServerConfig, func(ev xaapiv1.EventMsg) {
84 svrCfg, err := ev.DecodeServerCfg()
85 if err == nil && !svrCfg.Connected {
86 sCli.ServerDiscoChan <- Disconnection{
87 error: "\nXDS Server (id " + svrCfg.ID + ") disconnected !",
97 // On record a callback on a specific message
98 func (c *IOSockClient) On(message string, f interface{}) (err error) {
99 return c.Conn.On(message, f)
102 // Emit send a message
103 func (c *IOSockClient) Emit(message string, args ...interface{}) (err error) {
105 defer c.EmitMutex.Unlock()
106 return c.Conn.Emit(message, args...)