]> git.lizzy.rs Git - go-anidb.git/commitdiff
udpapi: Finish(?) partially-done refactor, fixes compilation
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Sat, 13 Jul 2013 02:12:55 +0000 (23:12 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Sat, 13 Jul 2013 02:12:55 +0000 (23:12 -0300)
I didn't break the API with the previous commit :(

udp/auth.go
udp/comm.go
udp/misc.go
udp/packet.go
udp/reply.go
udp/sendqueue.go

index 4f052e166dbba8d06f8c1def1ecc787469f27eaf..c78e8001eba9a633233aff072c86770136888121 100755 (executable)
@@ -1,5 +1,9 @@
 package udpapi
 
+import (
+       "strings"
+)
+
 // Authenticates the supplied user with the supplied password. Blocks until we have a reply.
 // Needed before almost any other API command can be used.
 //
@@ -17,12 +21,12 @@ func (a *AniDBUDP) Auth(user, password, udpKey string) (err error) {
        }
 
        a.session = ""
-       if udpkey != "" {
+       if udpKey != "" {
                if err = a.encrypt(user, udpKey); err != nil {
                        return err
                }
        }
-       r := <-a.SendRecv("AUTH", paramMap{
+       r := <-a.SendRecv("AUTH", ParamMap{
                "user":      user,
                "pass":      password,
                "protover":  3,
@@ -44,13 +48,13 @@ func (a *AniDBUDP) Auth(user, password, udpKey string) (err error) {
 //
 // http://wiki.anidb.net/w/UDP_API_Definition#LOGOUT:_Logout
 func (a *AniDBUDP) Logout() (err error) {
-       r := <-a.SendRecv("LOGOUT", paramMap{})
+       r := <-a.SendRecv("LOGOUT", ParamMap{})
        a.session = ""
        return r.Error()
 }
 
 func (a *AniDBUDP) encrypt(user, udpKey string) (err error) {
-       if reply := <-a.SendRecv("ENCRYPT", paramMap{"user": user, "type": 1}); reply.Error() != nil {
+       if reply := <-a.SendRecv("ENCRYPT", ParamMap{"user": user, "type": 1}); reply.Error() != nil {
                return reply.Error()
        } else {
                switch reply.Code() {
index 1c8139e97701e884f7425ef7604e6a64ca932616..a966789e866c77f8428118f3011f53e356fe1499 100755 (executable)
@@ -8,14 +8,9 @@
 package udpapi
 
 import (
-       "bytes"
        "compress/zlib"
-       "crypto/aes"
-       "crypto/cipher"
-       "crypto/md5"
        "fmt"
        "io"
-       "io/ioutil"
        "log"
        "net"
        "sort"
@@ -104,18 +99,19 @@ func (a *AniDBUDP) SendRecv(command string, args ParamMap) <-chan APIReply {
                args["s"] = a.session
        }
        for k, v := range args {
-               v = strings.Replace(v, "\n", "<br/>", -1)
-               args[k] = strings.Replace(v, "&", "&amp;", -1)
+               s := fmt.Sprint(v)
+               s = strings.Replace(s, "\n", "<br/>", -1)
+               args[k] = strings.Replace(s, "&", "&amp;", -1)
        }
 
+       ch := make(chan APIReply, 1)
+
        if err := a.dial(); err != nil {
                ch <- newErrorWrapper(err)
                close(ch)
                return ch
        }
 
-       ch := make(chan APIReply, 1)
-
        a.routerLock.Lock()
        a.tagRouter[tag] = ch
        a.routerLock.Unlock()
@@ -192,13 +188,7 @@ func (a *AniDBUDP) send(command string, args ParamMap) chan bool {
 
        p := makePacket([]byte(str), a.ecb)
 
-       sendPacket(p, a.sendCh)
-}
-
-type packet struct {
-       b    []byte
-       err  error
-       sent chan bool
+       return sendPacket(p, a.sendCh)
 }
 
 func (a *AniDBUDP) sendLoop() {
@@ -286,7 +276,7 @@ func (a *AniDBUDP) recvLoop() {
                                        if c >= 720 && c < 799 {
                                                // notices that need PUSHACK
                                                id := strings.Fields(r.Text())[0]
-                                               a.send("PUSHACK", paramMap{"nid": id})
+                                               a.send("PUSHACK", ParamMap{"nid": id})
 
                                                a.Notifications <- r
                                        } else if c == 799 {
index c2901361cf2803406ff07b53311c08263a4717d3..f106717d640904213841ec260d9009d9593a72e4 100755 (executable)
@@ -1,5 +1,10 @@
 package udpapi
 
+import (
+       "strconv"
+       "time"
+)
+
 type UptimeReply struct {
        APIReply
        Uptime time.Duration
@@ -12,9 +17,9 @@ type UptimeReply struct {
 //
 // http://wiki.anidb.net/w/UDP_API_Definition#UPTIME:_Retrieve_Server_Uptime
 func (a *AniDBUDP) Uptime() <-chan UptimeReply {
-       ch := make(chan *UptimeReply, 2)
+       ch := make(chan UptimeReply, 2)
        go func() {
-               reply := <-a.SendRecv("UPTIME", paramMap{})
+               reply := <-a.SendRecv("UPTIME", ParamMap{})
 
                r := UptimeReply{APIReply: reply}
                if r.Error() == nil {
@@ -39,9 +44,9 @@ type PingReply struct {
 //
 // http://wiki.anidb.net/w/UDP_API_Definition#PING:_Ping_Command
 func (a *AniDBUDP) Ping() <-chan PingReply {
-       ch := make(chan *PingReply, 2)
+       ch := make(chan PingReply, 2)
        go func() {
-               reply := <-a.SendRecv("PING", paramMap{"nat": 1})
+               reply := <-a.SendRecv("PING", ParamMap{"nat": 1})
 
                r := PingReply{APIReply: reply}
                if r.Error() == nil {
index 22a693f745d8bf997c2a0a0021ebcd261111ad1e..279af06fe00aa0de7649640e0a04a5d87f923980 100755 (executable)
@@ -8,6 +8,12 @@ import (
        "net"
 )
 
+type packet struct {
+       b    []byte
+       err  error
+       sent chan bool
+}
+
 func getPacket(conn *net.UDPConn, ecb *ecbState) (buf []byte, err error) {
        buf = make([]byte, 1500)
        n, err := conn.Read(buf)
index 38b344ce78f29b61da792c36622f7f42b7aead08..7b391f87499da7c7f5fde74071c2ef4df3848bf6 100755 (executable)
@@ -2,17 +2,17 @@ package udpapi
 
 import (
        "errors"
-       "reflect"
+       "fmt"
        "strconv"
        "strings"
 )
 
-type apiError struct {
+type APIError struct {
        Code int
        Desc string
 }
 
-func (err *apiError) Error() string {
+func (err *APIError) Error() string {
        return fmt.Sprint(err.Code, err.Desc)
 }
 
@@ -47,7 +47,7 @@ func (_ *errorWrapper) Tag() string {
        return ""
 }
 
-func (_ *errorWrapper) Code() int {
+func (w *errorWrapper) Code() int {
        switch e := w.err.(type) {
        case *APIError:
                return e.Code
@@ -128,11 +128,11 @@ func newGenericReply(raw []byte) (r *genericReply) {
                text = strings.Join(parts[1:], " ")
        }
 
-       var e *apiError = nil
+       var e *APIError = nil
        // 720-799 range is for notifications
        // 799 is an API server shutdown notice, so I guess it's okay to be an error
        if code < 200 || (code > 299 && code < 720) || code > 798 {
-               e = &apiError{Code: int(code), Desc: text}
+               e = &APIError{Code: int(code), Desc: text}
        }
 
        return &genericReply{
index b469ad94e719d92f6173ca72a1e8dc47efb4bde9..87276daf23ccea0a4d9e050516acfafb137bb9d6 100755 (executable)
@@ -4,11 +4,6 @@ import (
        "time"
 )
 
-type packet struct {
-       /*...*/
-       sent chan bool
-}
-
 type enqueuedPacket struct {
        packet
        queue chan packet
@@ -35,9 +30,10 @@ const (
        throttleDecInterval = 10 * time.Second
 )
 
-func sendPacket(p packet, c chan packet) {
+func sendPacket(p packet, c chan packet) chan bool {
        p.sent = make(chan bool, 2)
        globalQueue.enqueue <- enqueuedPacket{packet: p, queue: c}
+       return p.sent
 }
 
 func (gq *sendQueueState) sendQueueDispatch() {