diff --git a/tunnel/default.go b/tunnel/default.go new file mode 100644 index 00000000..a7386ffd --- /dev/null +++ b/tunnel/default.go @@ -0,0 +1,67 @@ +package tunnel + +import ( + "sync" + + "github.com/micro/go-micro/transport" +) + +type tun struct { + options Options + sync.RWMutex + connected bool + closed chan bool +} + +func newTunnel(opts ...Option) Tunnel { + // initialize default options + options := DefaultOptions() + + for _, o := range opts { + o(&options) + } + + t := &tun{ + options: options, + closed: make(chan bool), + } + + return t +} + +func (t *tun) Id() string { + return t.options.Id +} + +func (t *tun) Address() string { + return t.options.Address +} + +func (t *tun) Transport() transport.Transport { + return t.options.Transport +} + +func (t *tun) Options() transport.Options { + return transport.Options{} +} + +func (t *tun) Connect() error { + return nil +} + +func (t *tun) Close() error { + return nil +} + +func (t *tun) Status() Status { + select { + case <-t.closed: + return Closed + default: + return Connected + } +} + +func (t *tun) String() string { + return "micro" +} diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index e208157b..2adc4447 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -5,11 +5,33 @@ import ( "github.com/micro/go-micro/transport" ) +// Status is tunnel status +type Status int + +const ( + // Connected means the tunnel is alive + Connected Status = iota + // Closed meands the tunnel has been disconnected + Closed +) + // Tunnel creates a p2p network tunnel. type Tunnel interface { - transport.Transport + // Id returns tunnel id + Id() string + // Address returns tunnel address + Address() string + // Tramsport returns tunnel transport + Transport() transport.Transport // Connect connects the tunnel Connect() error // Close closes the tunnel Close() error + // Status returns tunnel status + Status() Status +} + +// NewTunnel creates a new tunnel on top of a link +func NewTunnel(opts ...Option) Tunnel { + return newTunnel(opts...) }