github.com/braveheart12/insolar-09-08-19@v0.8.7/network/hostnetwork/transport.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 "time" 23 24 "github.com/insolar/insolar/configuration" 25 "github.com/insolar/insolar/core" 26 "github.com/insolar/insolar/instrumentation/inslogger" 27 "github.com/insolar/insolar/instrumentation/instracer" 28 "github.com/insolar/insolar/log" 29 "github.com/insolar/insolar/network" 30 "github.com/insolar/insolar/network/sequence" 31 "github.com/insolar/insolar/network/transport" 32 "github.com/insolar/insolar/network/transport/host" 33 "github.com/insolar/insolar/network/transport/packet" 34 "github.com/insolar/insolar/network/transport/packet/types" 35 "github.com/insolar/insolar/network/transport/relay" 36 "github.com/pkg/errors" 37 "go.opencensus.io/trace" 38 ) 39 40 type hostTransport struct { 41 transportBase 42 handlers map[types.PacketType]network.RequestHandler 43 } 44 45 type packetWrapper packet.Packet 46 47 func (p *packetWrapper) GetSender() core.RecordRef { 48 return p.Sender.NodeID 49 } 50 51 func (p *packetWrapper) GetSenderHost() *host.Host { 52 return p.Sender 53 } 54 55 func (p *packetWrapper) GetType() types.PacketType { 56 return p.Type 57 } 58 59 func (p *packetWrapper) GetData() interface{} { 60 return p.Data 61 } 62 63 func (p *packetWrapper) GetRequestID() network.RequestID { 64 return p.RequestID 65 } 66 67 type future struct { 68 transport.Future 69 } 70 71 // Response get channel that receives response to sent request 72 func (f future) Response() <-chan network.Response { 73 in := transport.Future(f).Result() 74 out := make(chan network.Response, cap(in)) 75 go func(in <-chan *packet.Packet, out chan<- network.Response) { 76 for packet := range in { 77 out <- (*packetWrapper)(packet) 78 } 79 close(out) 80 }(in, out) 81 return out 82 } 83 84 // GetResponse get response to sent request with `duration` timeout 85 func (f future) GetResponse(duration time.Duration) (network.Response, error) { 86 result, err := f.GetResult(duration) 87 if err != nil { 88 return nil, err 89 } 90 return (*packetWrapper)(result), nil 91 } 92 93 // GetRequest get initiating request. 94 func (f future) GetRequest() network.Request { 95 request := transport.Future(f).Request() 96 return (*packetWrapper)(request) 97 } 98 99 func (h *hostTransport) processMessage(msg *packet.Packet) { 100 ctx, logger := inslogger.WithTraceField(context.Background(), msg.TraceID) 101 logger.Debugf("Got %s request from host %s; RequestID: %d", msg.Type.String(), msg.Sender.String(), msg.RequestID) 102 handler, exist := h.handlers[msg.Type] 103 if !exist { 104 logger.Errorf("No handler set for packet type %s from node %s", 105 msg.Type.String(), msg.Sender.NodeID.String()) 106 return 107 } 108 ctx, span := instracer.StartSpan(ctx, "hostTransport.processMessage") 109 span.AddAttributes( 110 trace.StringAttribute("msg receiver", msg.Receiver.Address.String()), 111 trace.StringAttribute("msg trace", msg.TraceID), 112 trace.StringAttribute("msg type", msg.Type.String()), 113 ) 114 defer span.End() 115 response, err := handler(ctx, (*packetWrapper)(msg)) 116 if err != nil { 117 logger.Errorf("Error handling request %s from node %s: %s", 118 msg.Type.String(), msg.Sender.NodeID.String(), err) 119 return 120 } 121 r := response.(*packetWrapper) 122 err = h.transport.SendResponse(ctx, msg.RequestID, (*packet.Packet)(r)) 123 if err != nil { 124 logger.Error(err) 125 } 126 } 127 128 // SendRequestPacket send request packet to a remote node. 129 func (h *hostTransport) SendRequestPacket(ctx context.Context, request network.Request, receiver *host.Host) (network.Future, error) { 130 inslogger.FromContext(ctx).Debugf("Send %s request to host %s", request.GetType().String(), receiver.String()) 131 f, err := h.transport.SendRequest(ctx, h.buildRequest(ctx, request, receiver)) 132 if err != nil { 133 return nil, err 134 } 135 return future{Future: f}, nil 136 } 137 138 // RegisterPacketHandler register a handler function to process incoming request packets of a specific type. 139 func (h *hostTransport) RegisterPacketHandler(t types.PacketType, handler network.RequestHandler) { 140 _, exists := h.handlers[t] 141 if exists { 142 log.Warnf("Multiple handlers for packet type %s are not supported! New handler will replace the old one!", t) 143 } 144 h.handlers[t] = handler 145 } 146 147 // BuildResponse create response to an incoming request with Data set to responseData. 148 func (h *hostTransport) BuildResponse(ctx context.Context, request network.Request, responseData interface{}) network.Response { 149 sender := request.(*packetWrapper).Sender 150 p := packet.NewBuilder(h.origin).Type(request.GetType()).Receiver(sender).RequestID(request.GetRequestID()). 151 Response(responseData).TraceID(inslogger.TraceID(ctx)).Build() 152 return (*packetWrapper)(p) 153 } 154 155 func NewInternalTransport(conf configuration.Configuration, nodeRef string) (network.InternalTransport, error) { 156 tp, err := transport.NewTransport(conf.Host.Transport, relay.NewProxy()) 157 if err != nil { 158 return nil, errors.Wrap(err, "error creating transport") 159 } 160 origin, err := getOrigin(tp, nodeRef) 161 if err != nil { 162 return nil, errors.Wrap(err, "error getting origin") 163 } 164 result := &hostTransport{handlers: make(map[types.PacketType]network.RequestHandler)} 165 result.sequenceGenerator = sequence.NewGeneratorImpl() 166 result.transport = tp 167 result.origin = origin 168 result.messageProcessor = result.processMessage 169 return result, nil 170 }