Commit 4b41deaa authored by Kirill Smelkov's avatar Kirill Smelkov

X error context for IdentifyMe & Identify

parent 6fd0c9be
...@@ -125,9 +125,10 @@ func NewClient(storLink *NodeLink) (*Client, error) { ...@@ -125,9 +125,10 @@ func NewClient(storLink *NodeLink) (*Client, error) {
// first identify ourselves to peer // first identify ourselves to peer
storType, err := IdentifyMe(storLink, CLIENT) storType, err := IdentifyMe(storLink, CLIENT)
if err != nil { if err != nil {
return nil, err // XXX err ctx return nil, err
} }
if storType != STORAGE { if storType != STORAGE {
// XXX + "newclient" to err ctx ?
return nil, fmt.Errorf("%v: peer is not storage (identifies as %v)", storLink, storType) return nil, fmt.Errorf("%v: peer is not storage (identifies as %v)", storLink, storType)
} }
...@@ -139,7 +140,7 @@ func NewClient(storLink *NodeLink) (*Client, error) { ...@@ -139,7 +140,7 @@ func NewClient(storLink *NodeLink) (*Client, error) {
// XXX -> server could reuse goroutines -> so not so bad ? // XXX -> server could reuse goroutines -> so not so bad ?
storConn, err := storLink.NewConn() storConn, err := storLink.NewConn()
if err != nil { if err != nil {
return nil, err // XXX err ctx ? return nil, err // XXX err ctx
} }
return &Client{storLink, storConn}, nil return &Client{storLink, storConn}, nil
......
...@@ -612,7 +612,7 @@ func handshake(ctx context.Context, conn net.Conn, version uint32) (err error) { ...@@ -612,7 +612,7 @@ func handshake(ctx context.Context, conn net.Conn, version uint32) (err error) {
txWg.Add(1) txWg.Add(1)
go func() { go func() {
var b [4]byte var b [4]byte
binary.BigEndian.PutUint32(b[:], version /*+ 33*/) // XXX -> hton32 ? binary.BigEndian.PutUint32(b[:], version) // XXX -> hton32 ?
_, err := conn.Write(b[:]) _, err := conn.Write(b[:])
// XXX EOF -> ErrUnexpectedEOF ? // XXX EOF -> ErrUnexpectedEOF ?
errch <- err errch <- err
......
...@@ -93,27 +93,33 @@ func ListenAndServe(ctx context.Context, net_, laddr string, srv Server) error { ...@@ -93,27 +93,33 @@ func ListenAndServe(ctx context.Context, net_, laddr string, srv Server) error {
// it expects peer to send RequestIdentification packet and replies with AcceptIdentification if identification passes. // it expects peer to send RequestIdentification packet and replies with AcceptIdentification if identification passes.
// returns information about identified node or error. // returns information about identified node or error.
func IdentifyPeer(link *NodeLink, myNodeType NodeType) (nodeInfo RequestIdentification /*TODO -> NodeInfo*/, err error) { func IdentifyPeer(link *NodeLink, myNodeType NodeType) (nodeInfo RequestIdentification /*TODO -> NodeInfo*/, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("%s: identify: %s", link, err)
}
}()
// the first conn must come with RequestIdentification packet // the first conn must come with RequestIdentification packet
conn, err := link.Accept() conn, err := link.Accept()
if err != nil { if err != nil {
return nodeInfo, err // XXX err ctx return nodeInfo, err
} }
defer func() { defer func() {
err2 := conn.Close() err2 := conn.Close()
if err == nil { if err == nil {
err = err2 // XXX err ctx err = err2
// XXX also clear nodeInfo ? // XXX also clear nodeInfo ?
} }
}() }()
pkt, err := RecvAndDecode(conn) pkt, err := RecvAndDecode(conn)
if err != nil { if err != nil {
return nodeInfo, err // XXX err ctx return nodeInfo, err
} }
switch pkt := pkt.(type) { switch pkt := pkt.(type) {
default: default:
return nodeInfo, fmt.Errorf("expected RequestIdentification ; got %T", pkt) return nodeInfo, fmt.Errorf("unexpected request: %T", pkt)
// XXX also handle Error // XXX also handle Error
...@@ -140,6 +146,12 @@ func IdentifyPeer(link *NodeLink, myNodeType NodeType) (nodeInfo RequestIdentifi ...@@ -140,6 +146,12 @@ func IdentifyPeer(link *NodeLink, myNodeType NodeType) (nodeInfo RequestIdentifi
// IdentifyMe identifies local node to remote peer // IdentifyMe identifies local node to remote peer
func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, err error) { func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("%s: request identification: %s", link, err)
}
}()
conn, err := link.NewConn() conn, err := link.NewConn()
if err != nil { if err != nil {
return peerType, err return peerType, err
...@@ -147,7 +159,7 @@ func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, e ...@@ -147,7 +159,7 @@ func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, e
defer func() { defer func() {
err2 := conn.Close() err2 := conn.Close()
if err == nil && err2 != nil { if err == nil && err2 != nil {
err = err2 // XXX err ctx err = err2
// XXX also reset peerType // XXX also reset peerType
} }
}() }()
...@@ -161,6 +173,7 @@ func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, e ...@@ -161,6 +173,7 @@ func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, e
}) })
if err != nil { if err != nil {
fmt.Printf("eee -> %v\n", err)
return peerType, err return peerType, err
} }
...@@ -171,7 +184,7 @@ func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, e ...@@ -171,7 +184,7 @@ func IdentifyMe(link *NodeLink, nodeType NodeType /*XXX*/) (peerType NodeType, e
switch pkt := pkt.(type) { switch pkt := pkt.(type) {
default: default:
return peerType, fmt.Errorf("expected AcceptIdentification ; got %T", pkt) return peerType, fmt.Errorf("unexpected answer: %T", pkt)
// XXX also handle Error // XXX also handle Error
......
...@@ -87,8 +87,7 @@ func (stor *Storage) ServeLink(ctx context.Context, link *NodeLink) { ...@@ -87,8 +87,7 @@ func (stor *Storage) ServeLink(ctx context.Context, link *NodeLink) {
for { for {
conn, err := link.Accept() conn, err := link.Accept()
if err != nil { if err != nil {
// XXX both link and accept op should be generated in link.Accept fmt.Printf("stor: %v\n", err) // XXX err ctx
fmt.Printf("stor: %v: accept: %v\n", link, err) // XXX err ctx
break break
} }
......
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