Commit b13e8150 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 9e223d74
......@@ -23,6 +23,7 @@ import (
"fmt"
"net"
"strconv"
"strings"
"crypto/tls"
"../xcommon/pipenet"
......@@ -35,6 +36,9 @@ import (
// connections, and 2) dial peers. For this reason the interface is not split
// into Dialer and Listener.
type Network interface {
// Network returns name of the network
Network() string
// Dial connects to addr on underlying network
// see net.Dial for semantic details
Dial(ctx context.Context, addr string) (net.Conn, error)
......@@ -53,6 +57,10 @@ func NetPlain(network string) Network {
type netPlain string
func (n netPlain) Network() string {
return string(n)
}
func (n netPlain) Dial(ctx context.Context, addr string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, string(n), addr)
......@@ -80,6 +88,10 @@ type netTLS struct {
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) {
c, err := n.inner.Dial(ctx, addr)
if err != nil {
......@@ -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
func Addr(addr net.Addr) (Address, error) {
addrstr := addr.String()
func AddrString(network, addr string) (Address, error) {
// 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
switch addr.Network() {
default:
return Address{Host: addrstr, Port: 0}, nil
if strings.HasPrefix(network, "tcp") || strings.HasPrefix(network, "udp") {
// networks that have host:port split
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
host, portstr, err := net.SplitHostPort(addrstr)
host, portstr, err := net.SplitHostPort(addr)
if err != nil {
return Address{}, err
}
// XXX also lookup portstr in /etc/services (net.LookupPort) ?
port, err := strconv.ParseUint(portstr, 10, 16)
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: 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
......
......@@ -37,7 +37,7 @@ type Storage struct {
clusterName string
net neo.Network // network we are sending/receiving on
masterAddr string // address of master XXX -> Address ?
masterAddr string // address of master
// ---- 8< ----
zstor zodb.IStorage // underlying ZODB storage XXX temp ?
......@@ -48,7 +48,7 @@ type Storage struct {
// 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 {
// convert serveAddr into neo format
addr, err := neo.ParseAddress(serveAddr)
addr, err := neo.AddrString(net.Network(), serveAddr)
if err != nil {
panic(err) // XXX
}
......
......@@ -122,7 +122,7 @@ func New(name string) *Network {
// Connection requests created by Dials could be accepted via Accept.
func (n *Network) Listen(laddr string) (net.Listener, 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
......@@ -195,7 +195,7 @@ func (l *listener) Accept() (net.Conn, error) {
select {
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:
// someone dialed us - let's connect
......@@ -219,7 +219,7 @@ func (l *listener) Accept() (net.Conn, error) {
// It tries to connect to Accept called on listener corresponding to addr.
func (n *Network) Dial(ctx context.Context, addr string) (net.Conn, 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)
......@@ -328,7 +328,7 @@ func (e *entry) empty() bool {
// addr returns address corresponding to entry
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 }
......@@ -340,5 +340,5 @@ func (l *listener) Addr() net.Addr {
return l.entry.addr()
}
// Name returns full network name of this network
func (n *Network) Name() string { return NetPrefix + n.name }
// Network returns full network name of this network
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