github.com/braveheart12/insolar-09-08-19@v0.8.7/network/controller/controller.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 controller
    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/network"
    27  	"github.com/insolar/insolar/network/controller/bootstrap"
    28  	"github.com/insolar/insolar/network/controller/common"
    29  	"github.com/insolar/insolar/network/transport/packet/types"
    30  )
    31  
    32  // Controller contains network logic.
    33  type Controller struct {
    34  	Bootstrapper  bootstrap.NetworkBootstrapper `inject:""`
    35  	RPCController RPCController                 `inject:""`
    36  
    37  	network network.HostNetwork
    38  }
    39  
    40  func (c *Controller) SetLastIgnoredPulse(number core.PulseNumber) {
    41  	c.Bootstrapper.SetLastPulse(number)
    42  }
    43  
    44  func (c *Controller) GetLastIgnoredPulse() core.PulseNumber {
    45  	return c.Bootstrapper.GetLastPulse()
    46  }
    47  
    48  // SendParcel send message to nodeID.
    49  func (c *Controller) SendMessage(nodeID core.RecordRef, name string, msg core.Parcel) ([]byte, error) {
    50  	return c.RPCController.SendMessage(nodeID, name, msg)
    51  }
    52  
    53  // RemoteProcedureRegister register remote procedure that will be executed when message is received.
    54  func (c *Controller) RemoteProcedureRegister(name string, method core.RemoteProcedure) {
    55  	c.RPCController.RemoteProcedureRegister(name, method)
    56  }
    57  
    58  // SendCascadeMessage sends a message from MessageBus to a cascade of nodes.
    59  func (c *Controller) SendCascadeMessage(data core.Cascade, method string, msg core.Parcel) error {
    60  	return c.RPCController.SendCascadeMessage(data, method, msg)
    61  }
    62  
    63  // Bootstrap init bootstrap process: 1. Connect to discovery node; 2. Reconnect to new discovery node if redirected.
    64  func (c *Controller) Bootstrap(ctx context.Context) (*network.BootstrapResult, error) {
    65  	return c.Bootstrapper.Bootstrap(ctx)
    66  }
    67  
    68  // Inject inject components.
    69  func (c *Controller) Init(ctx context.Context) error {
    70  	c.network.RegisterRequestHandler(types.Ping, func(ctx context.Context, request network.Request) (network.Response, error) {
    71  		return c.network.BuildResponse(ctx, request, nil), nil
    72  	})
    73  	return nil
    74  }
    75  
    76  // ConfigureOptions convert daemon configuration to controller options
    77  func ConfigureOptions(conf configuration.Configuration) *common.Options {
    78  	config := conf.Host
    79  	return &common.Options{
    80  		InfinityBootstrap:   config.InfinityBootstrap,
    81  		TimeoutMult:         time.Duration(config.TimeoutMult) * time.Second,
    82  		MinTimeout:          time.Duration(config.MinTimeout) * time.Second,
    83  		MaxTimeout:          time.Duration(config.MaxTimeout) * time.Second,
    84  		PingTimeout:         1 * time.Second,
    85  		PacketTimeout:       10 * time.Second,
    86  		BootstrapTimeout:    10 * time.Second,
    87  		HandshakeSessionTTL: time.Duration(config.HandshakeSessionTTL) * time.Millisecond,
    88  		FakePulseDuration:   time.Duration(conf.Pulsar.PulseTime) * time.Millisecond,
    89  	}
    90  }
    91  
    92  // NewNetworkController create new network controller.
    93  func NewNetworkController(net network.HostNetwork) network.Controller {
    94  	return &Controller{network: net}
    95  }