-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim-target.js
177 lines (162 loc) · 4.25 KB
/
sim-target.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const cloudant = require('@cloudant/cloudant')
const WebSocket = require('ws')
let db = null
let wss = null
let server = null
let portwebsocket = 8888
let hostcloudant = null
let dbnamecloudant = null
const init = (config, websocketserver) => {
let cloudantindex = config.TARGET_CLOUDANT.lastIndexOf('/')
hostcloudant = config.TARGET_CLOUDANT.substring(0, cloudantindex)
dbnamecloudant = config.TARGET_CLOUDANT.substring(cloudantindex + 1)
server = websocketserver
return Promise.all([initCloudant(), initSocket()])
.then(connections => {
if (!connections[0] && !connections[1]) {
console.warn('no cloudant or web socket connection established')
}
return Promise.resolve()
})
}
const initCloudant = () => {
db = null
return Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
console.log(`trying to access db at ${hostcloudant}/${dbnamecloudant}`)
let c = cloudant(hostcloudant)
c.db.get(dbnamecloudant, (err, body) => {
if (err) {
console.error(err)
resolve(false)
} else {
console.log(`accessed db at ${hostcloudant}/${dbnamecloudant}`)
console.log(body)
db = c.db.use(dbnamecloudant)
resolve(true)
}
})
})
})
.catch(err => {
console.error(err)
db = null
return Promise.resolve(false)
})
}
const initSocket = (retryCount) => {
return Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
if (!wss) {
if (server) {
wss = new WebSocket.Server({ server: server })
} else {
wss = new WebSocket.Server({ port: portwebsocket })
}
wss
.on('connection', (ws, req) => {
console.log('websocket connection:', req ? req.connection.remoteAddress : '')
ws.on('pong', () => {
console.log('websocket pong')
})
ws.send('connection received')
resolve(true)
})
.on('close', (code, reason) => {
console.log(`websocket closed: ${reason}`)
resolve(false)
})
.on('listening', () => {
console.log('websocket listening on', wss)
resolve(true)
})
.on('error', err => {
console.error(err)
resolve(false)
})
if (server) {
setTimeout(() => {
resolve(true)
}, 1000)
}
} else {
resolve(true)
}
})
})
.catch(err => {
console.error(err)
return Promise.resolve(false)
})
}
const sendMessage = (msg) => {
return Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
// console.log('sendMessage:', msg)
let data = null
msg['ts'] = (new Date()).getTime()
if (wss) {
data = JSON.stringify(msg)
console.log('sending data to websocket')
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(data)
}
})
}
if (db) {
data = msg
console.log('sending data to cloudant')
db.insert(data, (err, body) => {
if (err) {
console.error(err)
reject(err)
} else {
resolve()
}
})
}
if (!data || !db) {
resolve()
}
})
})
.catch(err => {
console.error(err)
return Promise.reject(err)
})
}
const close = () => {
return Promise.resolve()
.then(() => {
if (wss) {
wss.close(() => {
console.log('close: websocket terminated')
wss = null
return Promise.resolve()
})
} else {
return Promise.resolve()
}
})
.catch(err => {
console.error(err)
return Promise.reject(err)
})
}
const numConnections = () => {
let clients = 0
if (wss) {
clients = wss.clients.size
}
return clients
}
module.exports = {
init: init,
send: sendMessage,
close: close,
numClients: numConnections
}