forked from nttdots/go-dots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (110 loc) · 3.66 KB
/
main.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 main
import (
"flag"
"os"
"time"
log "github.com/sirupsen/logrus"
common "github.com/nttdots/go-dots/dots_common"
dots_config "github.com/nttdots/go-dots/dots_server/config"
"github.com/nttdots/go-dots/libcoap"
"github.com/nttdots/go-dots/dots_server/controllers"
"github.com/nttdots/go-dots/dots_server/task"
"github.com/nttdots/go-dots/dots_server/models/data"
"github.com/nttdots/go-dots/dots_common/messages"
)
var (
configFile string
defaultConfigFile = "dots_server.yaml"
)
func init() {
flag.StringVar(&configFile, "config", defaultConfigFile, "config yaml file")
}
func main() {
flag.Parse()
common.SetUpLogger()
_, err := dots_config.LoadServerConfig(configFile)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
config := dots_config.GetServerSystemConfig()
log.Debugf("dots server starting with config: %# v", config)
libcoap.Startup()
defer libcoap.Cleanup()
dtlsParam := libcoap.DtlsParam{
&config.SecureFile.CertFile,
nil,
&config.SecureFile.ServerCertFile,
&config.SecureFile.ServerKeyFile,
}
// Thread for monitoring remaining lifetime of mitigation requests
go controllers.ManageExpiredMitigation(config.LifetimeConfiguration.ManageLifetimeInterval)
// Thread for monitoring remaining lifetime of datachannel alias and acl requests
go data_models.ManageExpiredAliasAndAcl(config.LifetimeConfiguration.ManageLifetimeInterval)
log.Debug("listen Signal with DTLS param: %# v", dtlsParam)
signalCtx, err := listenSignal(config.Network.BindAddress, uint16(config.Network.SignalChannelPort), &dtlsParam)
if err != nil {
log.Error(err)
os.Exit(1)
}
defer signalCtx.FreeContext()
err = listenData(
config.Network.BindAddress,
uint16(config.Network.DataChannelPort),
config.SecureFile.CertFile,
config.SecureFile.ServerCertFile,
config.SecureFile.ServerKeyFile)
if err != nil {
log.Error(err)
os.Exit(1)
}
// Thread for handling status changed notification from DB
go listenDB (signalCtx)
// Run Ping task mechanism that monitor client session thread
env := task.NewEnv(signalCtx)
// Create new cache
libcoap.CreateNewCache(int(messages.EXCHANGE_LIFETIME), config.CacheInterval)
// Register ping handler
signalCtx.RegisterPingHandler(func(_ *libcoap.Context, session *libcoap.Session, _ *libcoap.Pdu) {
// This session is alive
session.SetIsAlive(true)
})
// Register nack handler
signalCtx.RegisterNackHandler(func(_ *libcoap.Context, _ *libcoap.Session, sent *libcoap.Pdu, reason libcoap.NackReason) {
if (reason == libcoap.NackRst){
// Pong message
env.HandleResponse(sent)
} else if (reason == libcoap.NackTooManyRetries){
// Ping timeout
env.HandleTimeout(sent)
} else {
// Unsupported type
log.Infof("nack_handler gets fired with unsupported reason type : %+v.", reason)
}
})
// Register event handler
signalCtx.RegisterEventHandler(func(_ *libcoap.Context, event libcoap.Event, session *libcoap.Session){
if event == libcoap.EventSessionConnected {
// Session connected: Add session to map
log.Debugf("New session connecting to dots server: %+v", session.String())
libcoap.AddNewConnectingSession(session)
} else if event == libcoap.EventSessionDisconnected || event == libcoap.EventSessionError {
// Session disconnected: Remove session from map
log.Debugf("Remove connecting session from dots server: %+v", session.String())
libcoap.RemoveConnectingSession(session)
} else {
// Not support yet
log.Warnf("Unsupported event")
}
})
go env.ManageSessionTraffic()
for {
select {
case e := <- env.EventChannel():
e.Handle(env)
default:
signalCtx.RunOnce(time.Duration(100) * time.Millisecond)
signalCtx.CheckRemovableResources()
}
}
}