This repository was archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js.html
172 lines (136 loc) · 4.32 KB
/
node.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: chord/node.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: chord/node.js</h1>
<section>
<article>
<pre class="prettyprint source"><code>ChordNode = function(peer, chord) {
if (!(this instanceof ChordNode)) {
return new ChordNode(peer, chord);
}
this._peer = peer;
this._successor = null;
this._predecessor = null;
this._chord = chord;
this._pending = {};
this._seqnr = 0;
this._chord.registerDeliveryCallback('chord-protocol', this._onmessage.bind(this));
return this;
};
ChordNode.prototype = {
/**
* Public API
**/
message_types: {
FIND_SUCCESSOR: 1,
SUCCESSOR: 2,
GET_NODE_ID: 3,
NODE_ID: 4,
},
find_successor: function(id, cb) {
this._send_request({
type: this.message_types.FIND_SUCCESSOR,
id: id.toString()
}, function(msg) {
cb(msg.successor);
});
},
get_node_id: function(cb) {
if (this._id) {
cb(this._id);
return;
}
this._send_request({
type: this.message_types.GET_NODE_ID
}, function(msg) {
this._id = BigInteger(msg.id); // TODO(max) sanity checks
cb(this._id);
}.bind(this));
},
/**
* Internal API
**/
_send_successor: function(seqnr) {
var msg = {
type: this.message_types.SUCCESSOR,
successor: this._successor,
seqnr: seqnr
};
this._send(msg);
},
_send_node_id: function(seqnr) {
var msg = {
type: this.message_types.NODE_ID,
id: this._id.toString(),
seqnr: seqnr
};
this._send(msg);
},
_send_request: function(msg, cb) {
msg.seqnr = this._seqnr++; // TODO(max): handle overflows
this._pending[msg.seqnr] = cb;
this._send(msg);
},
_send: function(msg) {
this._chord.route(this._peer.id, 'chord-protocol', msg);
},
_onmessage: function(msg, sender) {
var cb = this._pending[msg.seqnr];
// if we find a callback this message is a response to a request of ours
if (typeof(cb) === 'function') {
this._handle_response(msg, cb);
} else {
this._handle_request(msg);
}
},
_handle_response: function(msg, callback) {
delete this._pending[msg.seqnr];
callback(msg);
},
_handle_request: function(msg) {
if (typeof(msg.seqnr) === "undefined") {
return; // ignore message without sequence number
}
switch (msg.type) {
case this.message_types.FIND_SUCCESSOR:
this._send_successor(msg.seqnr);
break;
case this.message_types.GET_NODE_ID:
this._send_node_id(msg.seqnr);
break;
default:
//unknown request
break;
}
},
};
if (typeof(module) !== 'undefined') {
module.exports = ChordNode;
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="BOPlishClient.html">BOPlishClient</a></li><li><a href="BopURI.html">BopURI</a></li><li><a href="Chord.html">Chord</a></li><li><a href="ConnectionManager.html">ConnectionManager</a></li><li><a href="Peer.html">Peer</a></li><li><a href="Router.html">Router</a></li><li><a href="sha1.html">sha1</a></li></ul><h3>Global</h3><ul><li><a href="global.html#Helper">Helper</a></li></ul>
</nav>
<br clear="both">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Fri Aug 15 2014 17:48:09 GMT+0200 (CEST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>