forked from ethereum/mist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketManager.js
More file actions
79 lines (63 loc) · 1.63 KB
/
socketManager.js
File metadata and controls
79 lines (63 loc) · 1.63 KB
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
const _ = global._;
const Q = require('bluebird');
const log = require('./utils/logger').create('Sockets');
const Web3IpcSocket = require('./sockets/web3Ipc');
const Web3HttpSocket = require('./sockets/web3Http');
/**
* `Socket` manager.
*/
class SocketManager {
constructor() {
this._sockets = {};
}
/**
* Get socket with given id, creating it if it does not exist.
*
* @return {Socket}
*/
create(id, type) {
log.debug(`Create socket, id=${id}, type=${type}`);
switch (type) {
case 'ipc':
this._sockets[id] = new Web3IpcSocket(this, id);
break;
case 'http':
this._sockets[id] = new Web3HttpSocket(this, id);
break;
default:
throw new Error(`Unrecognized socket type: ${type}`);
}
return this._sockets[id];
}
/**
* Get socket with given id, creating it if it does not exist.
*
* @return {Socket}
*/
get(id, type) {
if (!this._sockets[id]) {
this.create(id, type);
}
return this._sockets[id];
}
/**
* @return {Promise}
*/
destroyAll() {
log.info('Destroy all sockets');
return Q.all(_.map(this._sockets, (s, id) => {
this.remove(id);
return s.destroy();
}));
}
/**
* Remove socket with given id from this manager.
*
* Usually called by `Socket` instances when they're destroyed.
*/
remove(id) {
log.debug(`Remove socket, id=${id}`);
delete this._sockets[id];
}
}
module.exports = new SocketManager();