github.com/braveheart12/insolar-09-08-19@v0.8.7/network/hostnetwork/transport_resolvable.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 hostnetwork 19 20 import ( 21 "context" 22 23 "github.com/insolar/insolar/core" 24 "github.com/insolar/insolar/network" 25 "github.com/insolar/insolar/network/transport/packet/types" 26 "github.com/pkg/errors" 27 ) 28 29 // TransportResolvable is implementation of HostNetwork interface that is capable of address resolving. 30 type TransportResolvable struct { 31 internalTransport network.InternalTransport 32 resolver network.RoutingTable 33 } 34 35 // Start listening to network requests. 36 func (tr *TransportResolvable) Start(ctx context.Context) { 37 tr.internalTransport.Start(ctx) 38 } 39 40 // Stop listening to network requests. 41 func (tr *TransportResolvable) Stop() { 42 tr.internalTransport.Stop() 43 } 44 45 // PublicAddress returns public address that can be published for all nodes. 46 func (tr *TransportResolvable) PublicAddress() string { 47 return tr.internalTransport.PublicAddress() 48 } 49 50 // GetNodeID get current node ID. 51 func (tr *TransportResolvable) GetNodeID() core.RecordRef { 52 return tr.internalTransport.GetNodeID() 53 } 54 55 // SendRequest send request to a remote node. 56 func (tr *TransportResolvable) SendRequest(ctx context.Context, request network.Request, receiver core.RecordRef) (network.Future, error) { 57 h, err := tr.resolver.Resolve(receiver) 58 if err != nil { 59 return nil, errors.Wrap(err, "error resolving NodeID -> Address") 60 } 61 return tr.internalTransport.SendRequestPacket(ctx, request, h) 62 } 63 64 // RegisterPacketHandler register a handler function to process incoming requests of a specific type. 65 func (tr *TransportResolvable) RegisterRequestHandler(t types.PacketType, handler network.RequestHandler) { 66 f := func(ctx context.Context, request network.Request) (network.Response, error) { 67 tr.resolver.AddToKnownHosts(request.GetSenderHost()) 68 return handler(ctx, request) 69 } 70 tr.internalTransport.RegisterPacketHandler(t, f) 71 } 72 73 // NewRequestBuilder create packet Builder for an outgoing request with sender set to current node. 74 func (tr *TransportResolvable) NewRequestBuilder() network.RequestBuilder { 75 return tr.internalTransport.NewRequestBuilder() 76 } 77 78 // BuildResponse create response to an incoming request with Data set to responseData. 79 func (tr *TransportResolvable) BuildResponse(ctx context.Context, request network.Request, responseData interface{}) network.Response { 80 return tr.internalTransport.BuildResponse(ctx, request, responseData) 81 } 82 83 func NewHostTransport(transport network.InternalTransport, resolver network.RoutingTable) network.HostNetwork { 84 return &TransportResolvable{internalTransport: transport, resolver: resolver} 85 }