session close added
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
const http = require('http')
|
||||
|
||||
module.exports = class http_client {
|
||||
constructor(ip, port) {
|
||||
this.#ip = ip
|
||||
this.#port = port
|
||||
}
|
||||
async post(data, callback_answer, callback_error) {
|
||||
// Build the post string from an object
|
||||
|
||||
// An object of options to indicate where to post to
|
||||
var post_options = {
|
||||
host: this.#ip,
|
||||
port: this.#port,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'applicaiton/json',
|
||||
'Content-Length': Buffer.byteLength(data)
|
||||
}
|
||||
}
|
||||
// Set up the request
|
||||
var request = http.request(post_options, function(res) {
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', async function (answer) {
|
||||
callback_answer(answer)
|
||||
})
|
||||
})
|
||||
request.on('error', function(e) {
|
||||
callback_error(e)
|
||||
})
|
||||
// post the data
|
||||
request.write(data)
|
||||
request.end()
|
||||
}
|
||||
#ip
|
||||
#port
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const node_time = require('node-datetime')
|
||||
const util = require('util')
|
||||
|
||||
module.exports = class logger {
|
||||
constructor(filepath) {
|
||||
this.#filepath = filepath
|
||||
this.#filename = path.basename(filepath)
|
||||
var dirname = path.dirname(filepath)
|
||||
|
||||
if (!fs.existsSync(dirname))
|
||||
fs.mkdirSync(dirname, {recursive:true})
|
||||
}
|
||||
error(...arg) {
|
||||
this.#log('error', arg)
|
||||
}
|
||||
log(...arg) {
|
||||
this.#log('', arg)
|
||||
}
|
||||
|
||||
#log(message = '', ...arg) {
|
||||
var args = ''
|
||||
arg.forEach(value => {
|
||||
args += util.format(value) + ' '
|
||||
})
|
||||
args = (node_time.create().format('Y-m-d H:M:S')).toString() + ((message != '') ? ': ' + message : '') + ': ' + args
|
||||
console.log(this.#filename + ': ' + args)
|
||||
fs.appendFileSync(this.#filepath, args + '\n')
|
||||
}
|
||||
|
||||
#filepath
|
||||
#filename
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
class port {
|
||||
constructor(value) {
|
||||
this.#value = value
|
||||
this.#free = true
|
||||
}
|
||||
get_value() {
|
||||
return this.#value
|
||||
}
|
||||
set_free() {
|
||||
this.#free = true
|
||||
}
|
||||
set_busy() {
|
||||
this.#free = false
|
||||
}
|
||||
is_free() {
|
||||
return this.#free
|
||||
}
|
||||
#value
|
||||
#free
|
||||
}
|
||||
|
||||
module.exports = class port_alloc {
|
||||
constructor(port_begin, count) {
|
||||
this.#ports = new Array()
|
||||
for (var i = port_begin; i < port_begin + count; ++i) {
|
||||
this.#ports.push(new port(i))
|
||||
}
|
||||
}
|
||||
get() {
|
||||
for (var i in this.#ports) {
|
||||
if (this.#ports[i].is_free()) {
|
||||
this.#ports[i].set_busy()
|
||||
return this.#ports[i].get_value()
|
||||
}
|
||||
}
|
||||
throw new Error('no free ports')
|
||||
}
|
||||
free(busy_port) {
|
||||
for (var i in this.#ports) {
|
||||
if (this.#ports[i].get_value() == busy_port) {
|
||||
this.#ports[i].set_free()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
#ports
|
||||
}
|
||||
Reference in New Issue
Block a user