Commit c95f2373 authored by Kirill Smelkov's avatar Kirill Smelkov

xnet: Networker += Name()

Networker.Name should return name of access-point Networker represents
on the network.

We will need this information in the next patch for tracing dial events,
when there is no connection established yet, and thus dialer location
has to be taken from somewhere. It is also generally a good idea for
Networker to have a name.

For NetPlain networker the name is local hostname.
parent 8c6b8ad0
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
// Copyright (C) 2017-2018 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
......@@ -22,7 +22,9 @@ package xnet
import (
"context"
"fmt"
"net"
"os"
"crypto/tls"
)
......@@ -32,6 +34,9 @@ type Networker interface {
// Network returns name of the network
Network() string
// Name returns name of the access-point on the network
Name() string
// XXX +Addr() net.Addr -> address of this access-point on underlying network ?
// Dial connects to addr on underlying network
......@@ -46,26 +51,41 @@ type Networker interface {
}
var hostname string
func init() {
host, err := os.Hostname()
if err != nil {
panic(fmt.Errorf("cannot detect hostname: %s", err))
}
hostname = host
}
// NetPlain creates Networker corresponding to regular network accessors from std package net.
//
// network is "tcp", "tcp4", "tcp6", "unix", etc...
func NetPlain(network string) Networker {
return netPlain(network)
return &netPlain{network, hostname}
}
type netPlain string
type netPlain struct {
network, hostname string
}
func (n *netPlain) Network() string {
return n.network
}
func (n netPlain) Network() string {
return string(n)
func (n *netPlain) Name() string {
return n.hostname
}
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{}
return d.DialContext(ctx, string(n), addr)
return d.DialContext(ctx, n.network, addr)
}
func (n netPlain) Listen(laddr string) (net.Listener, error) {
return net.Listen(string(n), laddr)
func (n *netPlain) Listen(laddr string) (net.Listener, error) {
return net.Listen(n.network, laddr)
}
// NetTLS wraps underlying networker with TLS layer according to config.
......@@ -86,6 +106,10 @@ func (n *netTLS) Network() string {
return n.inner.Network() + "+tls"
}
func (n *netTLS) Name() string {
return n.inner.Name()
}
func (n *netTLS) Dial(ctx context.Context, addr string) (net.Conn, error) {
c, err := n.inner.Dial(ctx, addr)
if err != nil {
......
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
// Copyright (C) 2017-2018 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
......@@ -424,3 +424,6 @@ func (n *Network) Network() string { return NetPrefix + n.name }
// Network returns full network name of underlying network
func (h *Host) Network() string { return h.network.Network() }
// Name returns host name.
func (h *Host) Name() string { return h.name }
......@@ -101,6 +101,8 @@ func TestPipeNet(t *testing.T) {
assertEq(t, .Network(), "pipet")
assertEq(t, .Network(), "pipet")
assertEq(t, .Name(), "α")
assertEq(t, .Name(), "β")
_, err := .Dial(context.Background(), ":0")
assertEq(t, err, &net.OpError{Op: "dial", Net: "pipet", Addr: xaddr("α:0"), Err: errConnRefused})
......
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
// Copyright (C) 2017-2018 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
......@@ -83,6 +83,10 @@ func (nt *netTrace) Network() string {
return nt.inner.Network() // XXX + "+trace" ?
}
func (nt *netTrace) Name() string {
return nt.inner.Name()
}
func (nt *netTrace) Dial(ctx context.Context, addr string) (net.Conn, error) {
// XXX +TraceNetDialPost ?
c, err := nt.inner.Dial(ctx, addr)
......
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