@@ -4,57 +4,18 @@ const INFILE = process.env['INFILE'],
4
4
OUTFILE = process . env [ 'OUTFILE' ] ,
5
5
REMOTE_SB = process . env [ 'REMOTE_SB' ]
6
6
7
- export default function scuttlebuttServer ( server ) {
8
- const primusServer = new ( require ( 'primus' ) ) ( server , { } ) ,
9
- Dispatcher = require ( './dispatcher' ) . default ,
10
- gossip = new Dispatcher ( )
11
-
12
- const statistics = { }
13
-
14
- var statisticsDirty = true
15
-
16
- // prime statistics for when spark.id is undefined, presumably server messages
17
- statistics [ undefined ] = {
18
- recv : 0 , sent : 0 , s : 'other'
19
- }
20
-
21
- setInterval ( ( ) => {
22
- if ( ! statisticsDirty )
23
- return
24
-
25
- statisticsDirty = false
26
-
27
- /*
28
- // full client statistics
29
- console.log('# ' + (new Date()) + '')
30
- for (let spark in statistics) {
31
- console.log(`${spark}: ${statistics[spark].recv} recv ${statistics[spark].sent} sent (${statistics[spark].s})`)
32
- }
33
- */
34
-
35
- // basic statistics
36
- console . log ( [
37
- ( new Date ( ) ) . toLocaleString ( 'en-AU' ) ,
38
- ': ' ,
39
- ( ( ) => {
40
- let recv = 0 , sent = 0 , connected = 0 , disconnected = 0 , other = 0
41
- for ( let spark in statistics ) {
42
- recv += statistics [ spark ] . recv
43
- sent += statistics [ spark ] . sent
44
-
45
- if ( statistics [ spark ] . s === 'connected' )
46
- connected ++
47
- else if ( statistics [ spark ] . s === 'disconnected' )
48
- disconnected ++
49
- else
50
- other ++
51
- }
7
+ const defaultOptions = {
8
+ connectRedux,
9
+ getStatistics,
10
+ }
52
11
53
- return `recv ${ recv } , sent ${ sent } , (${ connected } 🌏, ${ disconnected } 🔕, ${ other } 👥)`
54
- } ) ( )
55
- ] . join ( '' ) )
12
+ export default function scuttlebuttServer ( server , options ) {
13
+ options = { ...defaultOptions , ...options }
56
14
57
- } , 10000 ) // max 6/minute
15
+ const primusServer = new ( require ( 'primus' ) ) ( server , { } ) ,
16
+ Dispatcher = require ( './dispatcher' ) . default ,
17
+ gossip = new Dispatcher ( ) ,
18
+ onStatistic = getStatistics ( )
58
19
59
20
// connect dispatcher to redux
60
21
connectRedux ( gossip )
@@ -100,23 +61,20 @@ export default function scuttlebuttServer(server) {
100
61
101
62
remoteClient . pipe ( remoteStream ) . pipe ( remoteClient )
102
63
103
- statistics [ 'REMOTE_SB' ] = {
104
- recv : 0 , sent : 0 , s : 'remote'
105
- }
64
+ onStatistic ( 'REMOTE_SB' , 'connect' )
106
65
107
66
remoteClient . on ( 'data' , function recv ( data ) {
108
67
// console.log('[io]', 'REMOTE_SB', '<-', data);
109
- statistics [ 'REMOTE_SB' ] . recv ++
110
- statisticsDirty = true
68
+ onStatistic ( 'REMOTE_SB' , 'recv' )
111
69
} ) ;
112
70
113
71
remoteStream . on ( 'data' , ( data ) => {
114
72
// console.log('[io]', 'REMOTE_SB' || 'origin', '->', data);
115
- statistics [ 'REMOTE_SB' ] . sent ++
116
- statisticsDirty = true
73
+ onStatistic ( 'REMOTE_SB' , 'sent' )
117
74
} )
118
75
119
76
remoteStream . on ( 'error' , ( error ) => {
77
+ onStatistic ( 'REMOTE_SB' , 'error' , error )
120
78
console . log ( '[io]' , 'REMOTE_SB' , 'ERROR:' , error ) ;
121
79
remoteClient . end ( 'Disconnecting due to error' , { reconnect : true } )
122
80
} )
@@ -127,33 +85,29 @@ export default function scuttlebuttServer(server) {
127
85
128
86
// console.log('[io] connection', spark.address, spark.id)
129
87
130
- statistics [ spark . id ] = {
131
- recv : 0 , sent : 0 , s : 'connected'
132
- }
88
+ onStatistic ( spark . id , 'connect' )
133
89
134
90
spark . on ( 'data' , function recv ( data ) {
135
91
// console.log('[io]', spark.id, '<-', data);
136
- statistics [ spark . id ] . recv ++
137
- statisticsDirty = true
92
+ onStatistic ( spark . id , 'recv' )
138
93
stream . write ( data )
139
94
} ) ;
140
95
141
96
stream . on ( 'data' , ( data ) => {
142
97
// console.log('[io]', spark.id || 'origin', '->', data);
143
- statistics [ spark . id ] . sent ++
144
- statisticsDirty = true
98
+ onStatistic ( spark . id , 'sent' )
145
99
spark . write ( data )
146
100
} )
147
101
148
102
stream . on ( 'error' , ( error ) => {
103
+ onStatistic ( spark . id , 'error' , error )
149
104
console . log ( '[io]' , spark . id , 'ERROR:' , error ) ;
150
105
spark . end ( 'Disconnecting due to error' , { reconnect : true } )
151
106
} )
152
107
} )
153
108
154
109
primusServer . on ( 'disconnection' , ( spark ) => {
155
- statistics [ spark . id ] . s = 'disconnected'
156
- statisticsDirty = true
110
+ onStatistic ( spark . id , 'disconnect' )
157
111
// in case you don't want to track zombie connections
158
112
// delete statistics[spark.id]
159
113
} )
@@ -171,3 +125,71 @@ function connectRedux(gossip) {
171
125
// store.subscribe(render)
172
126
// setInterval(function () { dispatch({ type: 'TICK' }) }, 1000)
173
127
}
128
+
129
+ function getStatistics ( ) {
130
+ const statistics = { }
131
+
132
+ var statisticsDirty = true
133
+
134
+ // prime statistics for when spark.id is undefined, presumably server messages
135
+ statistics [ undefined ] = {
136
+ recv : 0 , sent : 0 , s : 'other'
137
+ }
138
+
139
+ setInterval ( ( ) => {
140
+ if ( ! statisticsDirty )
141
+ return
142
+
143
+ statisticsDirty = false
144
+
145
+ /*
146
+ // full client statistics
147
+ console.log('# ' + (new Date()) + '')
148
+ for (let spark in statistics) {
149
+ console.log(`${spark}: ${statistics[spark].recv} recv ${statistics[spark].sent} sent (${statistics[spark].s})`)
150
+ }
151
+ */
152
+
153
+ // basic statistics
154
+ console . log ( [
155
+ ( new Date ( ) ) . toLocaleString ( 'en-AU' ) ,
156
+ ': ' ,
157
+ ( ( ) => {
158
+ let recv = 0 , sent = 0 , connected = 0 , disconnected = 0 , other = 0
159
+ for ( let spark in statistics ) {
160
+ recv += statistics [ spark ] . recv
161
+ sent += statistics [ spark ] . sent
162
+
163
+ if ( statistics [ spark ] . s === 'connected' )
164
+ connected ++
165
+ else if ( statistics [ spark ] . s === 'disconnected' )
166
+ disconnected ++
167
+ else
168
+ other ++
169
+ }
170
+
171
+ return `recv ${ recv } , sent ${ sent } , (${ connected } 🌏, ${ disconnected } 🔕, ${ other } 👥)`
172
+ } ) ( )
173
+ ] . join ( '' ) )
174
+
175
+ } , 10000 ) // max 6/minute
176
+
177
+ return ( source , event , extra ) => {
178
+ statisticsDirty = true
179
+ if ( event === 'connect' ) {
180
+ statistics [ source ] = {
181
+ recv : 0 , sent : 0 , s : 'connected'
182
+ }
183
+ } else if ( event === 'disconnect' ) {
184
+ statistics [ source ] = {
185
+ recv : 0 , sent : 0 , s : 'disconnected'
186
+ }
187
+ } else if ( event === 'error' ) {
188
+ statistics [ source ] = {
189
+ recv : 0 , sent : 0 , s : 'error' , err : extra ,
190
+ }
191
+ } else if ( event === 'recv' || event === 'sent' ) {
192
+ statistics [ source ] [ event ] ++
193
+ }
194
+ }
195
+ }
0 commit comments