forked from brocaar/chirpstack-application-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join_server.go
129 lines (104 loc) · 3.23 KB
/
join_server.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
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/brocaar/lora-app-server/internal/join"
"github.com/brocaar/lorawan/backend"
)
// JoinServerAPI implements the join-server API as documented in the LoRaWAN
// backend interfaces specification.
type JoinServerAPI struct{}
// NewJoinServerAPI create a new JoinServerAPI.
func NewJoinServerAPI() http.Handler {
return &JoinServerAPI{}
}
// ServeHTTP implements the http.Handler interface.
func (a *JoinServerAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var basePL backend.BasePayload
b, err := ioutil.ReadAll(r.Body)
if err != nil {
a.returnError(w, http.StatusInternalServerError, backend.Other, "read body error")
return
}
err = json.Unmarshal(b, &basePL)
if err != nil {
a.returnError(w, http.StatusBadRequest, backend.Other, err.Error())
return
}
log.WithFields(log.Fields{
"message_type": basePL.MessageType,
"sender_id": basePL.SenderID,
"receiver_id": basePL.ReceiverID,
"transaction_id": basePL.TransactionID,
}).Info("js: request received")
switch basePL.MessageType {
case backend.JoinReq:
a.handleJoinReq(w, b)
case backend.RejoinReq:
a.handleRejoinReq(w, b)
default:
a.returnError(w, http.StatusBadRequest, backend.Other, fmt.Sprintf("invalid MessageType: %s", basePL.MessageType))
}
}
func (a *JoinServerAPI) returnError(w http.ResponseWriter, code int, resultCode backend.ResultCode, msg string) {
log.WithFields(log.Fields{
"error": msg,
}).Error("js: error handling request")
w.WriteHeader(code)
pl := backend.Result{
ResultCode: resultCode,
Description: msg,
}
b, err := json.Marshal(pl)
if err != nil {
log.WithError(err).Error("marshal json error")
return
}
w.Write(b)
}
func (a *JoinServerAPI) returnPayload(w http.ResponseWriter, code int, pl interface{}) {
w.WriteHeader(code)
b, err := json.Marshal(pl)
if err != nil {
log.WithError(err).Error("marshal json error")
return
}
w.Write(b)
}
func (a *JoinServerAPI) handleJoinReq(w http.ResponseWriter, b []byte) {
var joinReqPL backend.JoinReqPayload
err := json.Unmarshal(b, &joinReqPL)
if err != nil {
a.returnError(w, http.StatusBadRequest, backend.Other, err.Error())
return
}
ans := join.HandleJoinRequest(joinReqPL)
log.WithFields(log.Fields{
"message_type": ans.BasePayload.MessageType,
"sender_id": ans.BasePayload.SenderID,
"receiver_id": ans.BasePayload.ReceiverID,
"transaction_id": ans.BasePayload.TransactionID,
"result_code": ans.Result.ResultCode,
}).Info("js: sending response")
a.returnPayload(w, http.StatusOK, ans)
}
func (a *JoinServerAPI) handleRejoinReq(w http.ResponseWriter, b []byte) {
var rejoinReqPL backend.RejoinReqPayload
err := json.Unmarshal(b, &rejoinReqPL)
if err != nil {
a.returnError(w, http.StatusBadRequest, backend.Other, err.Error())
return
}
ans := join.HandleRejoinRequest(rejoinReqPL)
log.WithFields(log.Fields{
"message_type": ans.BasePayload.MessageType,
"sender_id": ans.BasePayload.SenderID,
"receiver_id": ans.BasePayload.ReceiverID,
"transaction_id": ans.BasePayload.TransactionID,
"result_code": ans.Result.ResultCode,
}).Info("js: sending response")
a.returnPayload(w, http.StatusOK, ans)
}