@@ -4,21 +4,113 @@ import (
44 "flag"
55 "log"
66 "sync"
7+ "io/ioutil"
78 "os"
89 "os/signal"
910 "syscall"
1011 "net/http"
1112 "fmt"
1213 "time"
13- //"github.com/chenyf/echoserver/engine "
14+ "encoding/json "
1415 "github.com/chenyf/gibbon/comet"
1516)
1617
18+ type CommandRequest struct {
19+ Uid string `json:"uid"`
20+ Cmd string `json:"cmd"`
21+ }
22+
23+ type CommandResponse struct {
24+ Status int `json:"status"`
25+ Error string `json:"error"`
26+ }
27+
1728func getStatus (w http.ResponseWriter , r * http.Request ) {
1829 size := comet .DevMap .Size ()
1930 fmt .Fprintf (w , "total register device: %d\n " , size )
2031}
2132
33+ func postRouterCommand (w http.ResponseWriter , r * http.Request ) {
34+ var response CommandResponse
35+ response .Status = 1
36+ if r .Method != "POST" {
37+ response .Error = "must using 'POST' method\n "
38+ b , _ := json .Marshal (response )
39+ fmt .Fprintf (w , string (b ))
40+ return
41+ }
42+ r .ParseForm ()
43+ rid := r .FormValue ("rid" )
44+ if rid == "" {
45+ response .Error = "missing 'rid'"
46+ b , _ := json .Marshal (response )
47+ fmt .Fprintf (w , string (b ))
48+ return
49+ }
50+
51+ uid := r .FormValue ("uid" )
52+ if uid == "" {
53+ response .Error = "missing 'uid'"
54+ b , _ := json .Marshal (response )
55+ fmt .Fprintf (w , string (b ))
56+ return
57+ }
58+
59+ /*
60+ uid := r.FormValue("uid")
61+ if uid == "" { fmt.Fprintf(w, "missing 'uid'\n"); return; }
62+ tid := r.FormValue("tid")
63+ if tid == "" { fmt.Fprintf(w, "missing 'tid'\n"); return; }
64+ sign := r.FormValue("sign")
65+ if sign == "" { fmt.Fprintf(w, "missing 'sign'\n"); return; }
66+ tm := r.FormValue("tm")
67+ if tm == "" { fmt.Fprintf(w, "missing 'tm'\n"); return; }
68+ pmtt := r.FormValue("pmtt")
69+ if pmtt == "" { fmt.Fprintf(w, "missing 'pmtt'\n"); return; }
70+ */
71+
72+ if r .Body == nil {
73+ response .Error = "missing POST data"
74+ b , _ := json .Marshal (response )
75+ fmt .Fprintf (w , string (b ))
76+ return
77+ }
78+
79+ if ! comet .DevMap .Check (rid ) {
80+ response .Error = fmt .Sprintf ("device (%s) offline" , rid )
81+ b , _ := json .Marshal (response )
82+ fmt .Fprintf (w , string (b ))
83+ return
84+ }
85+ client := comet .DevMap .Get (rid ).(* comet.Client )
86+
87+ body , err := ioutil .ReadAll (r .Body )
88+ r .Body .Close ()
89+ if err != nil {
90+ response .Error = "invalid POST body"
91+ b , _ := json .Marshal (response )
92+ fmt .Fprintf (w , string (b ))
93+ return
94+ }
95+
96+ cmdRequest := CommandRequest {
97+ Uid : uid ,
98+ Cmd : string (body ),
99+ }
100+
101+ bCmd , _ := json .Marshal (cmdRequest )
102+ reply := make (chan * comet.Message )
103+ client .SendMessage (comet .MSG_REQUEST , bCmd , reply )
104+ select {
105+ case msg := <- reply :
106+ fmt .Fprintf (w , string (msg .Data ))
107+ case <- time .After (10 * time .Second ):
108+ response .Error = "recv response timeout"
109+ b , _ := json .Marshal (response )
110+ fmt .Fprintf (w , string (b ))
111+ }
112+ }
113+
22114func getCommand (w http.ResponseWriter , r * http.Request ) {
23115
24116 r .ParseForm ()
@@ -34,13 +126,11 @@ func getCommand(w http.ResponseWriter, r *http.Request) {
34126 cmd := r .FormValue ("cmd" )
35127 client := comet .DevMap .Get (devid ).(* comet.Client )
36128 reply := make (chan * comet.Message )
37- seqid := client .SendMessage (comet .MSG_REQUEST , []byte (cmd ), reply )
129+ client .SendMessage (comet .MSG_REQUEST , []byte (cmd ), reply )
38130 select {
39131 case msg := <- reply :
40- delete (client .MsgFoo , seqid )
41132 fmt .Fprintf (w , "recv reply (%s)\n " , string (msg .Data ))
42133 case <- time .After (10 * time .Second ):
43- delete (client .MsgFoo , seqid )
44134 fmt .Fprintf (w , "recv timeout\n " )
45135 }
46136}
@@ -84,6 +174,7 @@ func main() {
84174 }()
85175 waitGroup .Add (1 )
86176 go func () {
177+ http .HandleFunc ("/router/command" , postRouterCommand )
87178 http .HandleFunc ("/command" , getCommand )
88179 http .HandleFunc ("/status" , getStatus )
89180 err := http .ListenAndServe ("0.0.0.0:9999" , nil )
0 commit comments