github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/noop/connection.go (about)

     1  /*
     2   * Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package noop
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"time"
    24  
    25  	"github.com/mysteriumnetwork/node/core/connection"
    26  	"github.com/mysteriumnetwork/node/core/connection/connectionstate"
    27  )
    28  
    29  // NewConnection creates a new noop connnection
    30  func NewConnection() (connection.Connection, error) {
    31  	return &Connection{
    32  		stateCh: make(chan connectionstate.State, 100),
    33  	}, nil
    34  }
    35  
    36  // Connection which does no real tunneling
    37  type Connection struct {
    38  	isRunning bool
    39  	stateCh   chan connectionstate.State
    40  }
    41  
    42  var _ connection.Connection = &Connection{}
    43  
    44  // State returns connection state channel.
    45  func (c *Connection) State() <-chan connectionstate.State {
    46  	return c.stateCh
    47  }
    48  
    49  // Statistics returns connection statistics channel.
    50  func (c *Connection) Statistics() (connectionstate.Statistics, error) {
    51  	return connectionstate.Statistics{At: time.Now()}, nil
    52  }
    53  
    54  // Reconnect restarts a connection with a new options.
    55  func (c *Connection) Reconnect(ctx context.Context, options connection.ConnectOptions) error {
    56  	return fmt.Errorf("not supported")
    57  }
    58  
    59  // Start implements the connection.Connection interface
    60  func (c *Connection) Start(ctx context.Context, params connection.ConnectOptions) error {
    61  	c.isRunning = true
    62  
    63  	c.stateCh <- connectionstate.Connecting
    64  
    65  	time.Sleep(5 * time.Second)
    66  	c.stateCh <- connectionstate.Connected
    67  	return nil
    68  }
    69  
    70  // Stop implements the connection.Connection interface
    71  func (c *Connection) Stop() {
    72  	if !c.isRunning {
    73  		return
    74  	}
    75  
    76  	c.isRunning = false
    77  	c.stateCh <- connectionstate.Disconnecting
    78  	time.Sleep(2 * time.Second)
    79  	c.stateCh <- connectionstate.NotConnected
    80  	close(c.stateCh)
    81  }
    82  
    83  // GetConfig returns the consumer configuration for session creation
    84  func (c *Connection) GetConfig() (connection.ConsumerConfig, error) {
    85  	return nil, nil
    86  }