Trying to implement Dial for usage with http.Transport

This commit is contained in:
Henry
2014-02-10 21:01:55 +01:00
parent a534c1afaa
commit ce26df96ac
5 changed files with 194 additions and 39 deletions

View File

@ -4,26 +4,25 @@ import (
"fmt"
)
type Result int
const (
ResultOk Result = iota //Operation completed successfully
ResultCantReachPeer //The peer exists, but cannot be reached
ResultDuplicatedDest //The specified Destination is already in use
ResultI2PError //A generic I2P error (e.g. I2CP disconnection, etc.)
ResultInvalidKey //The specified key is not valid (bad format, etc.)
ResultKeyNotFound //The naming system can't resolve the given name
ResultPeerNotFound //The peer cannot be found on the network
ResultTimeout // Timeout while waiting for an event (e.g. peer answer)
ResultOk = "OK" //Operation completed successfully
ResultCantReachPeer = "CANT_REACH_PEER" //The peer exists, but cannot be reached
ResultDuplicatedId = "DUPLICATED_ID" //If the nickname is already associated with a session :
ResultDuplicatedDest = "DUPLICATED_DEST" //The specified Destination is already in use
ResultI2PError = "I2P_ERROR" //A generic I2P error (e.g. I2CP disconnection, etc.)
ResultInvalidKey = "INVALID_KEY" //The specified key is not valid (bad format, etc.)
ResultKeyNotFound = "KEY_NOT_FOUND" //The naming system can't resolve the given name
ResultPeerNotFound = "PEER_NOT_FOUND" //The peer cannot be found on the network
ResultTimeout = "TIMEOUT" // Timeout while waiting for an event (e.g. peer answer)
)
type ReplyError struct {
Result Result
Result string
Reply *Reply
}
func (r ReplyError) Error() string {
return fmt.Sprintf("ReplyError: Result:%d - Reply:%+v", r.Reply)
return fmt.Sprintf("ReplyError: Result:%s - Reply:%+v", r.Result, r.Reply)
}
func (c *Client) Lookup(name string) (addr string, err error) {
@ -40,36 +39,33 @@ func (c *Client) Lookup(name string) (addr string, err error) {
line string
r *Reply
)
for {
line, err = c.fromSam.ReadString('\n')
if err != nil {
return
}
r, err = parseReply(line)
if err != nil {
break
}
line, err = c.fromSam.ReadString('\n')
if err != nil {
return
}
if r.Topic != "NAMING" || r.Type != "REPLY" {
err = fmt.Errorf("Unknown Reply: %+v\n", r)
break
}
r, err = parseReply(line)
if err != nil {
return
}
switch r.Pairs["RESULT"] {
case "OK":
addr = r.Pairs["VALUE"]
return
case "KEY_NOT_FOUND":
err = ReplyError{ResultKeyNotFound, r}
}
if r.Topic != "NAMING" || r.Type != "REPLY" {
err = fmt.Errorf("Unknown Reply: %+v\n", r)
return
}
if r.Pairs["NAME"] != name {
err = fmt.Errorf("i2p Replyied with: %+v\n", r)
break
}
switch r.Pairs["RESULT"] {
case "OK":
addr = r.Pairs["VALUE"]
return
case "KEY_NOT_FOUND":
err = ReplyError{ResultKeyNotFound, r}
return
}
break
if r.Pairs["NAME"] != name {
err = fmt.Errorf("i2p Replyied with: %+v\n", r)
}
return