github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/connection/connectionstate/event.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 connectionstate
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  
    25  	"github.com/mysteriumnetwork/node/core/discovery/proposal"
    26  	"github.com/mysteriumnetwork/node/core/location/locationstate"
    27  	"github.com/mysteriumnetwork/node/identity"
    28  	"github.com/mysteriumnetwork/node/session"
    29  )
    30  
    31  // Topic represents the different topics a consumer can subscribe to
    32  const (
    33  	// AppTopicConnectionState represents the session state change topic
    34  	AppTopicConnectionState = "State"
    35  	// AppTopicConnectionStatistics represents the session stats topic
    36  	AppTopicConnectionStatistics = "Statistics"
    37  	// AppTopicConnectionSession represents the session lifetime changes
    38  	AppTopicConnectionSession = "Session"
    39  )
    40  
    41  // AppEventConnectionState is the struct we'll emit on a AppEventConnectionState topic event
    42  type AppEventConnectionState struct {
    43  	UUID        string
    44  	State       State
    45  	SessionInfo Status
    46  }
    47  
    48  // State represents list of possible connection states
    49  type State string
    50  
    51  const (
    52  	// NotConnected means no connection exists
    53  	NotConnected = State("NotConnected")
    54  	// Connecting means that connection is startCalled but not yet fully established
    55  	Connecting = State("Connecting")
    56  	// Connected means that fully established connection exists
    57  	Connected = State("Connected")
    58  	// Disconnecting means that connection close is in progress
    59  	Disconnecting = State("Disconnecting")
    60  	// Reconnecting means that connection is lost but underlying service is trying to reestablish it
    61  	Reconnecting = State("Reconnecting")
    62  	// Unknown means that we could not map the underlying transport state to our state
    63  	Unknown = State("Unknown")
    64  	// Canceled means that connection initialization was started, but failed never reaching Connected state
    65  	Canceled = State("Canceled")
    66  	// StateIPNotChanged means that consumer ip not changed after connection is created
    67  	StateIPNotChanged = State("IPNotChanged")
    68  	// StateConnectionFailed means that underlying connection is failed
    69  	StateConnectionFailed = State("ConnectionFailed")
    70  	// StateOnHold means that underlying connection failed, but manager keeps it not removed to prevent traffic leaks.
    71  	StateOnHold = State("OnHold")
    72  )
    73  
    74  // Status holds connection state, session id and proposal of the connection
    75  type Status struct {
    76  	StartedAt        time.Time
    77  	ConsumerID       identity.Identity
    78  	ConsumerLocation locationstate.Location
    79  	HermesID         common.Address
    80  	State            State
    81  	SessionID        session.ID
    82  	Proposal         proposal.PricedServiceProposal
    83  }
    84  
    85  // Duration returns elapsed time from marked session start
    86  func (s *Status) Duration() time.Duration {
    87  	if s.StartedAt.IsZero() {
    88  		return time.Duration(0)
    89  	}
    90  
    91  	return time.Since(s.StartedAt)
    92  }
    93  
    94  const (
    95  	// SessionCreatedStatus represents a session creation event
    96  	SessionCreatedStatus = "Created"
    97  	// SessionEndedStatus represents a session end
    98  	SessionEndedStatus = "Ended"
    99  )
   100  
   101  // AppEventConnectionSession represents a session related event
   102  type AppEventConnectionSession struct {
   103  	Status      string
   104  	SessionInfo Status
   105  }
   106  
   107  // AppEventConnectionStatistics represents a session statistics event
   108  type AppEventConnectionStatistics struct {
   109  	UUID        string
   110  	Stats       Statistics
   111  	SessionInfo Status
   112  }