From fca89e06efc7489d9cea74d60f28a78945304e08 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 31 Jul 2019 15:22:57 +0100 Subject: [PATCH] Some network inspiration --- network/default.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ network/network.go | 18 ++++++++++++++++++ network/options.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 network/default.go create mode 100644 network/network.go create mode 100644 network/options.go diff --git a/network/default.go b/network/default.go new file mode 100644 index 00000000..d384a5f4 --- /dev/null +++ b/network/default.go @@ -0,0 +1,45 @@ +package network + +import ( + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/server" +) + +type network struct { + name string + options Options +} + +func (n *network) Name() string { + return n.name +} + +func (n *network) Connect() error { + return n.options.Server.Start() +} + +func (n *network) Close() error { + return n.options.Server.Stop() +} + +// NewNetwork returns a new network node +func NewNetwork(opts ...Option) Network { + options := Options{ + Name: DefaultName, + Client: client.DefaultClient, + Server: server.DefaultServer, + } + + for _, o := range opts { + o(&options) + } + + // set the server name + options.Server.Init( + server.Name(options.Name), + ) + + return &network{ + options: options, + } +} diff --git a/network/network.go b/network/network.go new file mode 100644 index 00000000..bf1a069b --- /dev/null +++ b/network/network.go @@ -0,0 +1,18 @@ +// Package network is for building peer to peer networks +package network + +// Network is a +type Network interface { + // Name of the network + Name() string + // Connect starts the network node + Connect() error + // Close shutsdown the network node + Close() error +} + +var ( + DefaultName = "go.micro.network" + + DefaultNetwork = NewNetwork() +) diff --git a/network/options.go b/network/options.go new file mode 100644 index 00000000..9dcbb1ed --- /dev/null +++ b/network/options.go @@ -0,0 +1,35 @@ +package network + +import ( + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/server" +) + +type Option func(*Options) + +type Options struct { + Name string + Client client.Client + Server server.Server +} + +// The network name +func Name(n string) Option { + return func(o *Options) { + o.Name = n + } +} + +// The network client +func Client(c client.Client) Option { + return func(o *Options) { + o.Client = c + } +} + +// The network server +func Server(s server.Server) Option { + return func(o *Options) { + o.Server = s + } +}