Commit b13e8150 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 9e223d74
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
"strings"
"crypto/tls" "crypto/tls"
"../xcommon/pipenet" "../xcommon/pipenet"
...@@ -35,6 +36,9 @@ import ( ...@@ -35,6 +36,9 @@ import (
// connections, and 2) dial peers. For this reason the interface is not split // connections, and 2) dial peers. For this reason the interface is not split
// into Dialer and Listener. // into Dialer and Listener.
type Network interface { type Network interface {
// Network returns name of the network
Network() string
// Dial connects to addr on underlying network // Dial connects to addr on underlying network
// see net.Dial for semantic details // see net.Dial for semantic details
Dial(ctx context.Context, addr string) (net.Conn, error) Dial(ctx context.Context, addr string) (net.Conn, error)
...@@ -53,6 +57,10 @@ func NetPlain(network string) Network { ...@@ -53,6 +57,10 @@ func NetPlain(network string) Network {
type netPlain string type netPlain string
func (n netPlain) Network() string {
return string(n)
}
func (n netPlain) Dial(ctx context.Context, addr string) (net.Conn, error) { func (n netPlain) Dial(ctx context.Context, addr string) (net.Conn, error) {
d := net.Dialer{} d := net.Dialer{}
return d.DialContext(ctx, string(n), addr) return d.DialContext(ctx, string(n), addr)
...@@ -80,6 +88,10 @@ type netTLS struct { ...@@ -80,6 +88,10 @@ type netTLS struct {
config *tls.Config config *tls.Config
} }
func (n *netTLS) Network() string {
return n.inner.Network() + "+tls" // XXX is this a good idea?
}
func (n *netTLS) Dial(ctx context.Context, addr string) (net.Conn, error) { func (n *netTLS) Dial(ctx context.Context, addr string) (net.Conn, error) {
c, err := n.inner.Dial(ctx, addr) c, err := n.inner.Dial(ctx, addr)
if err != nil { if err != nil {
...@@ -98,31 +110,33 @@ func (n *netTLS) Listen(laddr string) (net.Listener, error) { ...@@ -98,31 +110,33 @@ func (n *netTLS) Listen(laddr string) (net.Listener, error) {
// ---------------------------------------- // ----------------------------------------
// Addr converts net.Addr into NEO Address // Addr converts network address string into NEO Address
// TODO make neo.Address just string without host:port split // TODO make neo.Address just string without host:port split
func Addr(addr net.Addr) (Address, error) { func AddrString(network, addr string) (Address, error) {
addrstr := addr.String()
// e.g. on unix, pipenet, etc networks there is no host/port split - the address there // e.g. on unix, pipenet, etc networks there is no host/port split - the address there
// is single string -> we put it into .Host and set .Port=0 to indicate such cases // is single string -> we put it into .Host and set .Port=0 to indicate such cases
switch addr.Network() { if strings.HasPrefix(network, "tcp") || strings.HasPrefix(network, "udp") {
default:
return Address{Host: addrstr, Port: 0}, nil
// networks that have host:port split // networks that have host:port split
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6": host, portstr, err := net.SplitHostPort(addr)
host, portstr, err := net.SplitHostPort(addrstr)
if err != nil { if err != nil {
return Address{}, err return Address{}, err
} }
// XXX also lookup portstr in /etc/services (net.LookupPort) ? // XXX also lookup portstr in /etc/services (net.LookupPort) ?
port, err := strconv.ParseUint(portstr, 10, 16) port, err := strconv.ParseUint(portstr, 10, 16)
if err != nil { if err != nil {
return Address{}, &net.AddrError{Err: "invalid port", Addr: addrstr} return Address{}, &net.AddrError{Err: "invalid port", Addr: addr}
} }
return Address{Host: host, Port: uint16(port)}, nil return Address{Host: host, Port: uint16(port)}, nil
} }
return Address{Host: addr, Port: 0}, nil
}
// Addr converts net.Addre into NEO Address
func Addr(addr net.Addr) (Address, error) {
return AddrString(addr.Network(), addr.String())
} }
// String formats Address to networked address string // String formats Address to networked address string
......
...@@ -37,7 +37,7 @@ type Storage struct { ...@@ -37,7 +37,7 @@ type Storage struct {
clusterName string clusterName string
net neo.Network // network we are sending/receiving on net neo.Network // network we are sending/receiving on
masterAddr string // address of master XXX -> Address ? masterAddr string // address of master
// ---- 8< ---- // ---- 8< ----
zstor zodb.IStorage // underlying ZODB storage XXX temp ? zstor zodb.IStorage // underlying ZODB storage XXX temp ?
...@@ -48,7 +48,7 @@ type Storage struct { ...@@ -48,7 +48,7 @@ type Storage struct {
// To actually start running the node - call Run. XXX text // To actually start running the node - call Run. XXX text
func NewStorage(cluster string, masterAddr string, serveAddr string, net neo.Network, zstor zodb.IStorage) *Storage { func NewStorage(cluster string, masterAddr string, serveAddr string, net neo.Network, zstor zodb.IStorage) *Storage {
// convert serveAddr into neo format // convert serveAddr into neo format
addr, err := neo.ParseAddress(serveAddr) addr, err := neo.AddrString(net.Network(), serveAddr)
if err != nil { if err != nil {
panic(err) // XXX panic(err) // XXX
} }
......
...@@ -122,7 +122,7 @@ func New(name string) *Network { ...@@ -122,7 +122,7 @@ func New(name string) *Network {
// Connection requests created by Dials could be accepted via Accept. // Connection requests created by Dials could be accepted via Accept.
func (n *Network) Listen(laddr string) (net.Listener, error) { func (n *Network) Listen(laddr string) (net.Listener, error) {
lerr := func(err error) error { lerr := func(err error) error {
return &net.OpError{Op: "listen", Net: n.Name(), Addr: &Addr{n.Name(), laddr}, Err: err} return &net.OpError{Op: "listen", Net: n.Network(), Addr: &Addr{n.Network(), laddr}, Err: err}
} }
// laddr must be empty or int >= 0 // laddr must be empty or int >= 0
...@@ -195,7 +195,7 @@ func (l *listener) Accept() (net.Conn, error) { ...@@ -195,7 +195,7 @@ func (l *listener) Accept() (net.Conn, error) {
select { select {
case <-l.down: case <-l.down:
return nil, &net.OpError{Op: "accept", Net: n.Name(), Addr: l.Addr(), Err: errNetClosed} return nil, &net.OpError{Op: "accept", Net: n.Network(), Addr: l.Addr(), Err: errNetClosed}
case resp := <-l.dialq: case resp := <-l.dialq:
// someone dialed us - let's connect // someone dialed us - let's connect
...@@ -219,7 +219,7 @@ func (l *listener) Accept() (net.Conn, error) { ...@@ -219,7 +219,7 @@ func (l *listener) Accept() (net.Conn, error) {
// It tries to connect to Accept called on listener corresponding to addr. // It tries to connect to Accept called on listener corresponding to addr.
func (n *Network) Dial(ctx context.Context, addr string) (net.Conn, error) { func (n *Network) Dial(ctx context.Context, addr string) (net.Conn, error) {
derr := func(err error) error { derr := func(err error) error {
return &net.OpError{Op: "dial", Net: n.Name(), Addr: &Addr{n.Name(), addr}, Err: err} return &net.OpError{Op: "dial", Net: n.Network(), Addr: &Addr{n.Network(), addr}, Err: err}
} }
port, err := strconv.Atoi(addr) port, err := strconv.Atoi(addr)
...@@ -328,7 +328,7 @@ func (e *entry) empty() bool { ...@@ -328,7 +328,7 @@ func (e *entry) empty() bool {
// addr returns address corresponding to entry // addr returns address corresponding to entry
func (e *entry) addr() *Addr { func (e *entry) addr() *Addr {
return &Addr{network: e.network.Name(), addr: fmt.Sprintf("%d", e.port)} return &Addr{network: e.network.Network(), addr: fmt.Sprintf("%d", e.port)}
} }
func (a *Addr) Network() string { return a.network } func (a *Addr) Network() string { return a.network }
...@@ -340,5 +340,5 @@ func (l *listener) Addr() net.Addr { ...@@ -340,5 +340,5 @@ func (l *listener) Addr() net.Addr {
return l.entry.addr() return l.entry.addr()
} }
// Name returns full network name of this network // Network returns full network name of this network
func (n *Network) Name() string { return NetPrefix + n.name } func (n *Network) Network() string { return NetPrefix + n.name }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment