github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/relay/relay.go (about) 1 /* 2 * The Clear BSD License 3 * 4 * Copyright (c) 2019 Insolar Technologies 5 * 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: 9 * 10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 * Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 * 14 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 * 16 */ 17 18 package relay 19 20 import ( 21 "github.com/insolar/insolar/core" 22 "github.com/insolar/insolar/network/transport/host" 23 24 "errors" 25 ) 26 27 // State is alias for relaying state 28 type State int 29 30 const ( 31 // Unknown unknown relay state. 32 Unknown = State(iota + 1) 33 // Started is relay type means relaying started. 34 Started 35 // Stopped is relay type means relaying stopped. 36 Stopped 37 // Error is relay type means error state change. 38 Error 39 // NoAuth - this error returns if host tries to send relay request but not authenticated. 40 NoAuth 41 ) 42 43 // Relay Interface for relaying 44 type Relay interface { 45 // AddClient add client to relay list. 46 AddClient(host *host.Host) error 47 // RemoveClient removes client from relay list. 48 RemoveClient(host *host.Host) error 49 // ClientsCount - clients count. 50 ClientsCount() int 51 // NeedToRelay returns true if origin host is proxy for target host. 52 NeedToRelay(targetAddress string) bool 53 } 54 55 type relay struct { 56 clients []*host.Host 57 } 58 59 // NewRelay constructs relay list. 60 func NewRelay() Relay { 61 return &relay{ 62 clients: make([]*host.Host, 0), 63 } 64 } 65 66 // AddClient add client to relay list. 67 func (r *relay) AddClient(host *host.Host) error { 68 if _, n := r.findClient(host.NodeID); n != nil { 69 return errors.New("client exists already") 70 } 71 r.clients = append(r.clients, host) 72 return nil 73 } 74 75 // RemoveClient removes client from relay list. 76 func (r *relay) RemoveClient(host *host.Host) error { 77 idx, n := r.findClient(host.NodeID) 78 if n == nil { 79 return errors.New("client not found") 80 } 81 r.clients = append(r.clients[:idx], r.clients[idx+1:]...) 82 return nil 83 } 84 85 // ClientsCount - returns clients count. 86 func (r *relay) ClientsCount() int { 87 return len(r.clients) 88 } 89 90 // NeedToRelay returns true if origin host is proxy for target host. 91 func (r *relay) NeedToRelay(targetAddress string) bool { 92 for i := 0; i < r.ClientsCount(); i++ { 93 if r.clients[i].Address.String() == targetAddress { 94 return true 95 } 96 } 97 return false 98 } 99 100 func (r *relay) findClient(id core.RecordRef) (int, *host.Host) { 101 for idx, hostIterator := range r.clients { 102 if hostIterator.NodeID.Equal(id) { 103 return idx, hostIterator 104 } 105 } 106 return -1, nil 107 }