forked from Terry-Mao/gopush-cluster
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle.go
140 lines (132 loc) · 4.22 KB
/
handle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gopush-cluster.
// gopush-cluster is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gopush-cluster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gopush-cluster. If not, see <http://www.gnu.org/licenses/>.
package main
import (
myrpc "github.com/Terry-Mao/gopush-cluster/rpc"
"github.com/golang/glog"
"net/http"
"strconv"
"time"
)
// GetServer handle for server get
func GetServer0(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
key := params.Get("key")
callback := params.Get("callback")
protoStr := params.Get("proto")
res := map[string]interface{}{"ret": OK}
defer retWrite(w, r, res, callback, time.Now())
if key == "" {
res["ret"] = ParamErr
return
}
proto, err := strconv.Atoi(protoStr)
if err != nil {
glog.Errorf("strconv.Atoi(\"%s\") error(%v)", protoStr, err)
res["ret"] = ParamErr
return
}
// Match a push-server with the value computed through ketama algorithm
node := myrpc.GetComet(key)
if node == nil {
res["ret"] = NotFoundServer
return
}
addr := node.Addr[proto]
if addr == nil || len(addr) == 0 {
res["ret"] = NotFoundServer
return
}
server := ""
// Select the best ip
if Conf.Router != "" {
server = routerCN.SelectBest(r.RemoteAddr, addr)
glog.V(1).Infof("select the best ip:\"%s\" match with remoteAddr:\"%s\" , from ip list:\"%v\"", server, r.RemoteAddr, addr)
}
if server == "" {
glog.V(1).Infof("remote addr: \"%s\" chose the ip: \"%s\"", r.RemoteAddr, addr[0])
server = addr[0]
}
res["data"] = map[string]interface{}{"server": server}
return
}
// GetOfflineMsg get offline mesage http handler.
func GetOfflineMsg0(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
key := params.Get("key")
midStr := params.Get("mid")
pmidStr := params.Get("pmid")
callback := params.Get("callback")
res := map[string]interface{}{"ret": OK}
defer retWrite(w, r, res, callback, time.Now())
if key == "" || midStr == "" || pmidStr == "" {
res["ret"] = ParamErr
return
}
mid, err := strconv.ParseInt(midStr, 10, 64)
if err != nil {
res["ret"] = ParamErr
glog.Errorf("strconv.ParseInt(\"%s\", 10, 64) error(%v)", midStr, err)
return
}
pmid, err := strconv.ParseInt(pmidStr, 10, 64)
if err != nil {
res["ret"] = ParamErr
glog.Errorf("strconv.ParseInt(\"%s\", 10, 64) error(%v)", pmidStr, err)
return
}
// RPC get offline messages
reply := &myrpc.MessageGetResp{}
args := &myrpc.MessageGetArgs{MsgId: mid, PubMsgId: pmid, Key: key}
client := myrpc.MessageRPC.Get()
if client == nil {
res["ret"] = InternalErr
return
}
if err := client.Call(myrpc.MessageServiceGet, args, reply); err != nil {
glog.Errorf("myrpc.MessageRPC.Call(\"%s\", \"%v\", reply) error(%v)", myrpc.MessageServiceGet, args, err)
res["ret"] = InternalErr
return
}
omsgs := []*myrpc.OldMessage{}
for _, msg := range reply.Msgs {
omsgs = append(omsgs, &myrpc.OldMessage{GroupId: msg.GroupId, MsgId: msg.MsgId, Msg: string(msg.Msg)})
}
opmsgs := []*myrpc.OldMessage{}
for _, msg := range reply.PubMsgs {
opmsgs = append(opmsgs, &myrpc.OldMessage{GroupId: msg.GroupId, MsgId: msg.MsgId, Msg: string(msg.Msg)})
}
res["data"] = map[string]interface{}{"msgs": omsgs, "pmsgs": opmsgs}
return
}
// GetTime get server time http handler.
func GetTime0(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", 405)
return
}
params := r.URL.Query()
callback := params.Get("callback")
res := map[string]interface{}{"ret": OK}
defer retWrite(w, r, res, callback, time.Now())
res["data"] = map[string]interface{}{"timeid": time.Now().UnixNano()}
return
}