github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/node.go (about) 1 /* 2 * Copyright (C) 2017 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 cmd 19 20 import ( 21 "github.com/rs/zerolog/log" 22 23 "github.com/mysteriumnetwork/node/core/connection" 24 "github.com/mysteriumnetwork/node/core/node/event" 25 "github.com/mysteriumnetwork/node/tequilapi" 26 ) 27 28 // NATPinger allows to send nat pings as well as stop it 29 type NATPinger interface { 30 Stop() 31 } 32 33 // Publisher is responsible for publishing given events 34 type Publisher interface { 35 Publish(topic string, data interface{}) 36 } 37 38 // SleepNotifier notifies node about pending sleep events 39 type SleepNotifier interface { 40 Start() 41 Stop() 42 } 43 44 // NewNode function creates new Mysterium node by given options 45 func NewNode(connectionManager connection.MultiManager, tequilapiServer tequilapi.APIServer, publisher Publisher, uiServer UIServer, notifier SleepNotifier) *Node { 46 return &Node{ 47 connectionManager: connectionManager, 48 httpAPIServer: tequilapiServer, 49 publisher: publisher, 50 uiServer: uiServer, 51 sleepNotifier: notifier, 52 } 53 } 54 55 // Node represent entrypoint for Mysterium node with top level components 56 type Node struct { 57 connectionManager connection.MultiManager 58 httpAPIServer tequilapi.APIServer 59 publisher Publisher 60 uiServer UIServer 61 sleepNotifier SleepNotifier 62 } 63 64 // Start starts Mysterium node (Tequilapi service, fetches location) 65 func (node *Node) Start() error { 66 go node.sleepNotifier.Start() 67 node.httpAPIServer.StartServing() 68 69 node.uiServer.Serve() 70 node.publisher.Publish(event.AppTopicNode, event.Payload{Status: event.StatusStarted}) 71 72 return nil 73 } 74 75 // Wait blocks until Mysterium node is stopped 76 func (node *Node) Wait() error { 77 defer node.publisher.Publish(event.AppTopicNode, event.Payload{Status: event.StatusStopped}) 78 return node.httpAPIServer.Wait() 79 } 80 81 // Kill stops Mysterium node 82 func (node *Node) Kill() error { 83 err := node.connectionManager.Disconnect(-1) 84 if err != nil { 85 switch err { 86 case connection.ErrNoConnection: 87 log.Info().Msg("No active connection - proceeding") 88 default: 89 return err 90 } 91 } else { 92 log.Info().Msg("Connection closed") 93 } 94 95 node.httpAPIServer.Stop() 96 log.Info().Msg("API stopped") 97 98 node.uiServer.Stop() 99 log.Info().Msg("Web UI server stopped") 100 101 node.sleepNotifier.Stop() 102 log.Info().Msg("Sleep notifier stopped") 103 104 return nil 105 }