-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmappings.js
180 lines (154 loc) · 5.77 KB
/
mappings.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//A circuit that loops through us for each hop.
exports.BASE_CIRC_ID = 0;
var circuit_map = {};
var stream_to_socket_map = {};
var node_to_socket_map = {};
var circ_id_partition = {};
exports.addCircIDPartition= function(nodeID, value) {
circ_id_partition[nodeID] = value;
}
exports.getCircIDPartition = function(nodeID) {
if (nodeID in circ_id_partition) {
return circ_id_partition[nodeID];
}
return true;
}
exports.addNodeToSocketMapping = function(nid, socket) {
//TODO: check for overwrites?
if (nid in node_to_socket_map && node_to_socket_map[nid] != null) {
// ignore
} else {
node_to_socket_map[nid] = socket;
}
}
exports.removeNodeToSocketMapping = function(nid) {
if (nid in node_to_socket_map) {
delete node_to_socket_map[nid];
}
}
exports.getNodeToSocketMapping = function(nid) {
return node_to_socket_map[nid];
}
exports.getAllNodeIDs = function() {
return Object.keys(node_to_socket_map);
}
exports.addCircuitMapping = function(srcID, srcCircID, destID, destCircID) {
if (exports.getCircuitMapping(srcID, srcCircID) != null) {
console.log("Overwriting mapping");
}
if (!(srcID in circuit_map)) {
circuit_map[srcID] = {};
}
circuit_map[srcID][srcCircID] = {nid:destID, circid:destCircID};
}
exports.removeCircuitMapping = function(nodeID, circID){
if (nodeID in circuit_map) {
if (circID != undefined && circID in circuit_map[nodeID]) {
delete circuit_map[nodeID][circID];
} else {
delete circuit_map[nodeID];
}
//throw "Trying to delete circuit_id that's not in the map";
}
//throw "Trying to delete circuit_id from a node that's not in the map";
}
exports.getCircuitMapping = function(srcID, srcCircID) {
if (srcID in circuit_map) {
if (srcCircID in circuit_map[srcID]) {
return circuit_map[srcID][srcCircID];
}
}
return null;
}
exports.getAllCircuitMappings = function(nodeID) {
if (nodeID in circuit_map) {
return Object.keys(circuit_map[nodeID]);
} else {
return null;
}
}
/*exports.addStreamToStreamMapping = function(srcID, srcCircID, srcStreamID, destID, destCircID, destStreamID) {
if (exports.getCircuitMapping(srcID, srcCircID) == null) {
throw "Trying to map stream to stream, but this is the last node in the circuit.";
}
if (exports.getCircuitMapping(destID, destCircID) == null) {
throw "Trying to map stream to stream, but this is the first node in the circuit.";
}
circuit_map[srcID][srcCircID][srcStreamID] = destStreamID;
circuit_map[destID][destCircID][destStreamID] = srcStreamID;
}
exports.removeCircuitMapping(nodeID, circID) {
if (nodeID in circuit_map) {
if (circID in circuit_map[nodeID]) {
delete circuit_map[nodeID][circID];
}
throw "Trying to delete circuit_id that's not in the map";
}
throw "Trying to delete circuit_id from a node that's not in the map";
}
exports.getStreamToStreamMapping = function(srcID, srcCircID, srcStreamID) {
if (exports.getCircuitMapping(srcID, srcCircID) == null) {
throw "Trying to get stream to stream mapping, but we don't have an appropriate circuit mapping.";
}
if (srcStreamID in circuit_map[srcID][srcCircID]) {
return circuit_map[srcID][srcCircID][srcStreamID];
}
return null;
}*/
//TODO: DO WE NEED FOLLOWING TWO FUNCTIONS?
/*function addStreamToHostMapping(srcID, srcCircID, srcStreamID, _host, _hostPort) {
if (getCircuitMapping(srcID, srcCircID) != null) {
throw "Trying to get stream to host mapping, but we are not the last node in the circuit.";
}
if (getStreamToHostMapping(srcID, srcCircID, srcStreamID) != null) {
throw "A mapping already exists for this stream.";
}
stream_to_host_map[srcID][srcCircID][srcStreamID] = {host:_host, hostPort:_hostPort};
}
function getStreamToHostMapping(srcID, srcCircID, srcStreamID) {
if (srcID in stream_to_host_map) {
if (srcCircID in stream_to_host_map[srcID]) {
if (srcStreamID in stream_to_host_map[srcID][srcCircID]) {
return stream_to_host_map[srcID][srcCircID][srcStreamID];
}
}
}
return null;
}*/
exports.addStreamToSocketMapping = function(srcID, srcCircID, srcStreamID, socket) {
// console.log(exports.getStreamToSocketMapping(srcID, srcCircID, srcStreamID));
if (exports.getStreamToSocketMapping(srcID, srcCircID, srcStreamID) != null) {
throw "A mapping already exists for this stream.";
}
// console.log(stream_to_socket_map[srcID]);
if (stream_to_socket_map[srcID] == null) {
stream_to_socket_map[srcID] = {};
}
// console.log(stream_to_socket_map[srcID][srcCircID]);
if (stream_to_socket_map[srcID][srcCircID] == null) {
stream_to_socket_map[srcID][srcCircID] = {};
}
stream_to_socket_map[srcID][srcCircID][srcStreamID] = socket;
//stream_to_socket_map[socket] = {nid: srcID, circid: srcCircID, streamid: srcStreamID};
}
exports.getStreamToSocketMapping = function(srcID, srcCircID, srcStreamID) {
if (srcID in stream_to_socket_map) {
// console.log("id in map");
if (srcCircID in stream_to_socket_map[srcID]) {
// console.log("circid in map");
if (srcStreamID in stream_to_socket_map[srcID][srcCircID]) {
// console.log("stream id in map");
return stream_to_socket_map[srcID][srcCircID][srcStreamID];
}
}
}
// console.log("its null");
return null;
}
exports.removeStreamToSocketMapping = function(srcID, srcCircID, srcStreamID) {
if (srcID in stream_to_socket_map) {
if (srcCircID in stream_to_socket_map[srcID]) {
delete stream_to_socket_map[srcID][srcCircID][srcStreamID];
}
}
}