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
|
'use strict'
const dgram = require('dgram')
function Udp (terminal) {
this.stack = []
this.port = null
this.options = { default: 49161, orca: 49160 }
this.start = function () {
console.info('UDP Starting..')
this.select()
}
this.clear = function () {
this.stack = []
}
this.run = function () {
for (const id in this.stack) {
this.play(this.stack[id])
}
}
this.send = function (msg) {
this.stack.push(msg)
}
this.play = function (data) {
this.server.send(Buffer.from(`${data}`), this.port, '127.0.0.1', (err) => {
if (err) { console.warn(err) }
})
}
this.select = function (port = this.options.default) {
if (port < 1000) { console.warn('Unavailable port'); return }
this.port = port
this.update()
}
this.update = function () {
console.log(`UDP Port: ${this.port}`)
terminal.controller.clearCat('default', 'UDP')
for (const id in this.options) {
terminal.controller.add('default', 'UDP', `${id.charAt(0).toUpperCase() + id.substr(1)}(${this.options[id]}) ${this.port === this.options[id] ? ' — Active' : ''}`, () => { terminal.io.udp.select(this.options[id]) }, '')
}
terminal.controller.commit()
}
this.server = dgram.createSocket('udp4')
this.listener = dgram.createSocket('udp4')
// Input
this.listener.on('message', (msg, rinfo) => {
return this.act(`${msg}`)
})
this.listener.on('listening', () => {
const address = this.listener.address()
console.log(`UDP Listening: ${address.address}:${address.port}`)
})
this.listener.on('error', (err) => {
console.warn(`Server error:\n ${err.stack}`)
this.listener.close()
})
this.act = function (msg) {
const key = `${msg}`.substr(0, 1).toLowerCase()
const val = `${msg}`.substr(1)
const int = parseInt(`${msg}`.substr(1))
if (key === 'p') {
terminal.clock.play()
} else if (key === 's') {
terminal.stop()
} else if (key === 'r') {
terminal.run()
} else if (key === 'g') {
return `${terminal.orca}`
} else if (key === 'f' && Number.isInteger(int)) {
terminal.orca.f = int
} else if (key === 'b' && Number.isInteger(int)) {
terminal.clock.set(int, int, true)
} else if (key === 'a' && Number.isInteger(int)) {
terminal.clock.set(null, int)
} else if (key === 'w' && val.length >= 4 && val.indexOf(':') > -1) {
const pos = val.substr(1).split(':')
terminal.orca.write(parseInt(pos[0]), parseInt(pos[1]), val.substr(0, 1))
} else {
console.warn(`Unknown message: ${msg}`)
}
return 'done.'
}
this.listener.bind(49160)
}
module.exports = Udp
|