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