github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/gate/client/wsp/client.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package wsp
    20  
    21  import (
    22  	"context"
    23  	"net/http"
    24  
    25  	"github.com/gorilla/websocket"
    26  	"go.uber.org/atomic"
    27  
    28  	"github.com/e154/smart-home/adaptors"
    29  	"github.com/e154/smart-home/api"
    30  	"github.com/e154/smart-home/common/logger"
    31  	"github.com/e154/smart-home/system/jwt_manager"
    32  	"github.com/e154/smart-home/system/stream"
    33  )
    34  
    35  var (
    36  	log = logger.MustGetLogger("wsp client")
    37  )
    38  
    39  // Client connects to one or more Server using HTTP websockets.
    40  // The Server can then send HTTP requests to execute.
    41  type Client struct {
    42  	cfg        *Config
    43  	client     *http.Client
    44  	dialer     *websocket.Dialer
    45  	pools      map[string]*Pool
    46  	api        *api.Api
    47  	stream     *stream.Stream
    48  	isStarted  *atomic.Bool
    49  	adaptors   *adaptors.Adaptors
    50  	jwtManager jwt_manager.JwtManager
    51  }
    52  
    53  // NewClient creates a new Client.
    54  func NewClient(cfg *Config, api *api.Api, stream *stream.Stream, adaptors *adaptors.Adaptors, jwtManager jwt_manager.JwtManager) *Client {
    55  	return &Client{
    56  		cfg:        cfg,
    57  		client:     &http.Client{},
    58  		dialer:     &websocket.Dialer{},
    59  		pools:      make(map[string]*Pool),
    60  		api:        api,
    61  		stream:     stream,
    62  		isStarted:  atomic.NewBool(false),
    63  		adaptors:   adaptors,
    64  		jwtManager: jwtManager,
    65  	}
    66  }
    67  
    68  // Start the Proxy
    69  func (c *Client) Start(ctx context.Context) {
    70  	//log.Info("Start")
    71  	if !c.isStarted.CompareAndSwap(false, true) {
    72  		return
    73  	}
    74  	for _, target := range c.cfg.Targets {
    75  		pool := NewPool(c, target, c.cfg.SecretKey, c.api, c.stream, c.adaptors, c.jwtManager)
    76  		c.pools[target] = pool
    77  		go pool.Start(ctx)
    78  	}
    79  }
    80  
    81  // Shutdown the Proxy
    82  func (c *Client) Shutdown() {
    83  	//log.Info("Shutdown")
    84  	if !c.isStarted.CompareAndSwap(true, false) {
    85  		return
    86  	}
    87  	for _, pool := range c.pools {
    88  		pool.Shutdown()
    89  	}
    90  }