github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/packet/builder.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 packet
    19  
    20  import (
    21  	"github.com/insolar/insolar/network"
    22  	"github.com/insolar/insolar/network/transport/host"
    23  	"github.com/insolar/insolar/network/transport/packet/types"
    24  )
    25  
    26  // Builder allows lazy building of packets.
    27  // Each operation returns new copy of a builder.
    28  type Builder struct {
    29  	actions []func(packet *Packet)
    30  }
    31  
    32  // NewBuilder returns empty packet builder.
    33  func NewBuilder(sender *host.Host) Builder {
    34  	cb := Builder{}
    35  	cb.actions = append(cb.actions, func(packet *Packet) {
    36  		packet.Sender = sender
    37  		packet.RemoteAddress = sender.Address.String()
    38  	})
    39  	return cb
    40  }
    41  
    42  // Build returns configured packet.
    43  func (cb Builder) Build() (packet *Packet) {
    44  	packet = &Packet{}
    45  	for _, action := range cb.actions {
    46  		action(packet)
    47  	}
    48  	return
    49  }
    50  
    51  // Receiver sets packet receiver.
    52  func (cb Builder) Receiver(host *host.Host) Builder {
    53  	cb.actions = append(cb.actions, func(packet *Packet) {
    54  		packet.Receiver = host
    55  	})
    56  	return cb
    57  }
    58  
    59  // Type sets packet type.
    60  func (cb Builder) Type(packetType types.PacketType) Builder {
    61  	cb.actions = append(cb.actions, func(packet *Packet) {
    62  		packet.Type = packetType
    63  	})
    64  	return cb
    65  }
    66  
    67  // Request adds request data to packet.
    68  func (cb Builder) Request(request interface{}) Builder {
    69  	cb.actions = append(cb.actions, func(packet *Packet) {
    70  		packet.Data = request
    71  	})
    72  	return cb
    73  }
    74  
    75  func (cb Builder) RequestID(id network.RequestID) Builder {
    76  	cb.actions = append(cb.actions, func(packet *Packet) {
    77  		packet.RequestID = id
    78  	})
    79  	return cb
    80  }
    81  
    82  func (cb Builder) TraceID(traceID string) Builder {
    83  	cb.actions = append(cb.actions, func(packet *Packet) {
    84  		packet.TraceID = traceID
    85  	})
    86  	return cb
    87  }
    88  
    89  // Response adds response data to packet
    90  func (cb Builder) Response(response interface{}) Builder {
    91  	cb.actions = append(cb.actions, func(packet *Packet) {
    92  		packet.Data = response
    93  		packet.IsResponse = true
    94  	})
    95  	return cb
    96  }
    97  
    98  // Error adds error description to packet.
    99  func (cb Builder) Error(err error) Builder {
   100  	cb.actions = append(cb.actions, func(packet *Packet) {
   101  		packet.Error = err
   102  	})
   103  	return cb
   104  }