-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (95 loc) · 3.36 KB
/
index.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
var logger = require('./lib/logger.js');
var multicastListener = require('./lib/listeners/multicast.js');
var unicastListener = require('./lib/listeners/unicast.js');
var collector = require('./lib/requestCollector');
var config = require('./lib/configloader');
const EventEmitter = require("events");
const util = require('util');
// user supplied config, e.g. jwe key
var initialized = false;
/**
*
* @param options
* @param callback
* @returns {*}
*/
var init = function (options, callback) {
var errors = [];
// do some sync stuff, fill errors[] if errors happen (may partially work)
if (!initialized) {
if (options) {
if (options.key) {
try {
var key = JSON.stringify(options.key);
config.set('encryption:jwekey', key);
logger.debug('Supplied key has been added to the configuration.');
} catch (e) {
var keyParsingError = new Error('Supplied key is not a valid JSON structure.');
logger.error(keyParsingError);
errors.push(keyParsingError);
}
}
//TODO other options
} else {
var noOptionsSuppliedError = new Error('No initialization options were supplied.');
logger.error(noOptionsSuppliedError);
errors.push(noOptionsSuppliedError);
}
} else {
var noOptionsSuppliedError = new Error('User-supplied config has already been initialized!');
logger.error(noOptionsSuppliedError);
errors.push(noOptionsSuppliedError);
}
initialized = true;
if (errors.length > 0) {
logger.trace("CONFIGURATION AFTER USER-INIT: " + JSON.stringify(config.get()));
if (callback) callback(errors);
return errors;
} else {
logger.trace("CONFIGURATION AFTER USER-INIT: " + JSON.stringify(config.get()));
var okmsg = "everything went fine";
if (callback) callback(null, okmsg);
return okmsg;
}
};
multicastListener.start();
unicastListener.start();
function MyEmitter() {
EventEmitter.call(this);
}
util.inherits(MyEmitter, EventEmitter);
const myEmitter = new MyEmitter();
/**
* Catches valid requests from the collector
*/
collector.events.on('validrequest', function (options, acdprequest) {
logger.debug('a valid event from collector occurred!');
myEmitter.emit('validrequest', options, acdprequest);
});
/**
* Catches invalid requests from the collector
*/
collector.events.on('invalidrequest', function (options, acdprequest) {
logger.debug('an invalid event from collector occurred!');
myEmitter.emit('invalidrequest', options, acdprequest);
});
var userCallback;
function notificationHandler(options, acdprequest, callback) {
userCallback
}
/**
*
* @param externalResponseFunction must be a function which takes three parameters (options, acdprequest, callback). Delegates control over the requests to the implementer
*/
function externalResponseHandler(externalResponseFunction) {
if (externalResponseFunction.length != 3) {
logger.error('Response handler function must be constructed with {options},{acdprequest},callback().')
} else {
collector.handleAcdpRequest(externalResponseFunction);
}
}
module.exports = {
init: init,
controller: myEmitter,
respond: externalResponseHandler
};