Skip to content

Commit 489a024

Browse files
author
tom zhou
committed
get V2 implementation working
1 parent 00711c5 commit 489a024

File tree

4 files changed

+166
-39
lines changed

4 files changed

+166
-39
lines changed

demos/dese-v2.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) 2014 Tom Zhou<[email protected]>
2+
3+
var WEBPP = require('../lib/iwebpp.io-v2'),
4+
SEP = WEBPP.SEP;
5+
6+
var express = require('express');
7+
8+
// vURL
9+
var vURL = require('../lib/vurl');
10+
11+
// create name-client
12+
var nmcln = new WEBPP({
13+
srvinfo: {
14+
timeout: 20,
15+
endpoints: [{ip: 'iwebpp.com', port: 52686}, {ip: 'iwebpp.com', port: 52868}],
16+
turn: [
17+
{ip: 'iwebpp.com', agent: 52866, proxy: 52688} // every turn-server include proxy and agent port
18+
]
19+
},
20+
usrinfo: {domain: '51dese.com', usrkey: 'dese'},
21+
conmode: SEP.SEP_MODE_CS, // c/s mode as httpp server
22+
vmode: vURL.URL_MODE_HOST
23+
});
24+
25+
nmcln.on('error', function(err){
26+
console.log('iwebpp.io v2 err:'+err);
27+
});
28+
29+
nmcln.on('ready', function(){
30+
31+
/////////////////////////////////////////////////////////////////
32+
// file share App
33+
var app = express();
34+
35+
app.use(express.directory(__dirname + '/dese'));
36+
app.use(express.static(__dirname + '/dese'));
37+
app.use(function(req, res){
38+
res.end('invalid path');
39+
});
40+
/////////////////////////////////////////////////////////////////
41+
42+
// hook app on business server
43+
nmcln.bsrv.srv.on('request', app);
44+
45+
// monitor network performance
46+
nmcln.bsrv.srv.on('connection', function(socket){
47+
48+
var intl = setInterval(function(){
49+
///console.log('socket network performance:'+JSON.stringify(socket.netPerf));
50+
if (socket) {
51+
var perf = socket.netPerf;
52+
53+
console.log('socket network Bandwidth :'+JSON.stringify(perf.mbpsBandwidth)+' Mb/s');
54+
console.log('socket network RTT :'+JSON.stringify(perf.msRTT)+' ms');
55+
console.log('socket network PktSndPeriod :'+JSON.stringify(perf.usPktSndPeriod)+' us');
56+
console.log('socket network SendRate :'+JSON.stringify(perf.mbpsSendRate)+' Mb/s');
57+
console.log('socket network RecvRate :'+JSON.stringify(perf.mbpsRecvRate)+' Mb/s');
58+
console.log('socket network CongestionWindow:'+JSON.stringify(perf.pktCongestionWindow));
59+
console.log('socket network RecvACK :'+JSON.stringify(perf.pktRecvACK));
60+
console.log('socket network RecvNACK :'+JSON.stringify(perf.pktRecvNAK));
61+
console.log('socket network AvailRcvBuf :'+JSON.stringify(perf.byteAvailRcvBuf));
62+
console.log('socket network AvailSndBuf :'+JSON.stringify(perf.byteAvailSndBuf)+'\n\n');
63+
}
64+
}, 10000); // every 10000
65+
66+
socket.on('close', function(){
67+
clearInterval(intl);
68+
console.log('socket closed');
69+
});
70+
socket.on('error', function(){
71+
clearInterval(intl);
72+
console.log('socket error');
73+
});
74+
socket.on('end', function(){
75+
clearInterval(intl);
76+
console.log('socket end');
77+
});
78+
});
79+
80+
console.log('please access URL:'+nmcln.vurl);
81+
});

lib/iwebpp.io-v2.js

+44-37
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ var nmCln = exports = module.exports = function(options, fn){
8484
options = {};
8585
}
8686

87+
// check on version
88+
self.version = 2;
89+
8790
/////////////////////////////////////////////////////////////////
8891
// arguments check
8992
if (options === undefined || options.usrinfo === undefined) {
@@ -315,8 +318,8 @@ nmCln.prototype._getValidIPAddr = function(proto, fn){
315318
{
316319
httpp: true, hole: {addr: laddr},
317320
// SSL related info
318-
rejectUnauthorized: self.secmode ? true : false,
319-
ca: self.secmode ? vCA : false,
321+
///rejectUnauthorized: self.secmode ? true : false,
322+
/// ca: self.secmode ? vCA : false,
320323

321324
// NACL cert info
322325
naclinfo: {
@@ -335,12 +338,15 @@ nmCln.prototype._getValidIPAddr = function(proto, fn){
335338
if (Debug) console.log('connect to NS from '+laddr+' ... timeout');
336339
}, 2000); // 2s timeout
337340

338-
nscon.once('open', function(){
341+
nscon.on('open', function(){
339342
clearTimeout(nstmo);
340343
fn(1, laddr);
341344
nscon.close();
342345
///console.log('connect to NS from '+laddr+' ... ok');
343346
});
347+
nscon.on('error', function(err) {
348+
console.log('connect to NS from '+laddr+' ... err:'+err);
349+
});
344350
}
345351

346352
// 0.2
@@ -407,8 +413,8 @@ nmCln.prototype._LSM_connectTurnAgent = function(fn, tmo){
407413
{
408414
httpp: true, hole: {port: self.port, addr: self.ipaddr},
409415
// SSL related info
410-
rejectUnauthorized: self.secmode ? true : false,
411-
ca: self.secmode ? vCA : false,
416+
///rejectUnauthorized: self.secmode ? true : false,
417+
/// ca: self.secmode ? vCA : false,
412418

413419
// NACL cert info
414420
naclinfo: {
@@ -432,7 +438,7 @@ nmCln.prototype._LSM_connectTurnAgent = function(fn, tmo){
432438
fn('connect TURN agent server timeout');
433439
}, (tmo || 30)*1000); // 30s timeout in default
434440

435-
self.turnagentConn.once('open', function(){
441+
self.turnagentConn.on('open', function(){
436442
if (Debug) console.log('connected to turn agent server successfully');
437443

438444
// 1.1
@@ -533,7 +539,7 @@ nmCln.prototype._LSM_connectTurnAgent = function(fn, tmo){
533539

534540
// 4.
535541
// handle close event with reconnect
536-
self.turnagentConn.once('close', function(){
542+
self.turnagentConn.on('close', function(){
537543
if (Debug) console.log('turn agent client closed');
538544

539545
// 4.1
@@ -612,7 +618,7 @@ nmCln.prototype._LSM_setupSdpSession = function() {
612618
// 2.1.2
613619
// enable vURL if TURN ready
614620
if (self.turnagentReady) {
615-
var vurlproto = 'https://';
621+
var vurlproto = self.secmode ? 'https://' : 'http://';
616622

617623
if (self.vmode === vURL.URL_MODE_PATH) {
618624
// vpath-based turn vURL
@@ -683,6 +689,9 @@ nmCln.prototype._LSM_setupSdpSession = function() {
683689
// 2.2
684690
// return sdp info to user
685691
rsdp = {
692+
// protocol info
693+
version: self.version,
694+
686695
// GID of name-client
687696
gid: self.gid,
688697
vpath: self.vpath,
@@ -988,8 +997,8 @@ nmCln.prototype._LSM_setupSdpSession = function() {
988997
{
989998
httpp: true, hole: {addr: self.ipaddr, port: self.port},
990999
// SSL related info
991-
rejectUnauthorized: self.secmode ? true : false,
992-
ca: self.secmode ? vCA : false,
1000+
///rejectUnauthorized: self.secmode ? true : false,
1001+
/// ca: self.secmode ? vCA : false,
9931002

9941003
// NACL cert info
9951004
naclinfo: {
@@ -1008,8 +1017,8 @@ nmCln.prototype._LSM_setupSdpSession = function() {
10081017
{
10091018
httpp: true, hole: {addr: self.ipaddr},
10101019
// SSL related info
1011-
rejectUnauthorized: self.secmode ? true : false,
1012-
ca: self.secmode ? vCA : false,
1020+
///rejectUnauthorized: self.secmode ? true : false,
1021+
/// ca: self.secmode ? vCA : false,
10131022

10141023
// NACL cert info
10151024
naclinfo: {
@@ -1048,7 +1057,7 @@ nmCln.prototype._LSM_setupSdpSession = function() {
10481057
}
10491058
}, (self.srvinfo.timeout || 20)*1000); // 20s timeout in default
10501059

1051-
conn.once('open', function(){
1060+
conn.on('open', function(){
10521061
///console.log('connection to the first name-server');
10531062

10541063
// increase opened client count
@@ -1060,17 +1069,17 @@ nmCln.prototype._LSM_setupSdpSession = function() {
10601069

10611070
// 3.
10621071
// record binding on port/ip/fd, then start alternate connections
1063-
self.fd = conn._socket.address().fd;
1064-
self.port = conn._socket.address().port;
1072+
self.fd = conn.address().fd;
1073+
self.port = conn.address().port;
10651074
if (Debug) console.log('nmclnt binding on %s:%d with fd:%d', self.ipaddr, self.port, self.fd);
10661075

10671076
for (var i = 1; i < self.srvs.length; i ++) {
10681077
var connalt = new SecureWebSocket(wsproto+self.srvs[i].ip+':'+self.srvs[i].port+SEP.SEP_CTRLPATH_NS,
10691078
{
10701079
httpp: true, hole: {port: self.port, addr: self.ipaddr},
10711080
// SSL related info
1072-
rejectUnauthorized: self.secmode ? true : false,
1073-
ca: self.secmode ? vCA : false,
1081+
///rejectUnauthorized: self.secmode ? true : false,
1082+
/// ca: self.secmode ? vCA : false,
10741083

10751084
// NACL cert info
10761085
naclinfo: {
@@ -1093,7 +1102,7 @@ nmCln.prototype._LSM_setupSdpSession = function() {
10931102

10941103
// 4.
10951104
// all connection ready, then emit nmCln ready event
1096-
connalt.once('open', function(){
1105+
connalt.on('open', function(){
10971106
// increase opened connection count
10981107
self.ocnt++;
10991108

@@ -1128,7 +1137,7 @@ nmCln.prototype._LSM_setupSdpSession = function() {
11281137

11291138
// 6.
11301139
// handle close event with reconnect
1131-
connalt.once('close', function(){
1140+
connalt.on('close', function(){
11321141
if (Debug) console.log('name-server alternate client close');
11331142

11341143
// 6
@@ -1148,7 +1157,7 @@ nmCln.prototype._LSM_setupSdpSession = function() {
11481157
conn.on('message', onMessage);
11491158

11501159
// on close with reconnect
1151-
conn.once('close', function(){
1160+
conn.on('close', function(){
11521161
if (Debug) console.log('name-server primary client close');
11531162

11541163
// trigger reconnect event
@@ -1277,13 +1286,13 @@ nmCln.prototype._LSM_setupBusinessServer = function(){
12771286

12781287
self.bsrv.hpsrv.on('connection', function(client){
12791288
console.log('new ws connection: ' +
1280-
client._socket.remoteAddress+':'+client._socket.remotePort+' -> ' +
1281-
client._socket.address().address+':'+client._socket.address().port);
1289+
client.remoteAddress+':'+client.remotePort+' -> ' +
1290+
client.address().address+':'+client.address().port);
12821291

12831292
// 1.2.2
12841293
// send peer's connection info back
12851294
try {
1286-
var coninfo = {peeraddr: client._socket.remoteAddress, peerport: client._socket.remotePort};
1295+
var coninfo = {peeraddr: client.remoteAddress, peerport: client.remotePort};
12871296

12881297
// V2 use msgpack
12891298
client.send(MSGPACK.encode(coninfo), {binary: true, mask: false}, function(err){
@@ -1307,7 +1316,7 @@ nmCln.prototype._LSM_setupBusinessServer = function(){
13071316
clearTimeout(clrtmo); clrtmo = null;
13081317
}
13091318
});
1310-
client.once('close', function(){
1319+
client.on('close', function(){
13111320
if (Debug) console.log('hole-punch client closed');
13121321

13131322
if (clrtmo) {
@@ -1469,7 +1478,7 @@ nmCln.prototype.createConnection = function(to, fn){
14691478
conn.close();
14701479
}, (to.timeout || 10)*1000); // 10s timeout in default
14711480

1472-
conn.once('open', function(){
1481+
conn.on('open', function(){
14731482
clearTimeout(t);
14741483
fn(null, conn);
14751484

@@ -1486,7 +1495,7 @@ nmCln.prototype.createConnection = function(to, fn){
14861495
console.error('new peer connection failure to '+JSON.stringify(peer)+err);
14871496
});
14881497

1489-
conn.once('close', function(){
1498+
conn.on('close', function(){
14901499
// TBD...
14911500
/*if (self.peer[JSON.stringify(conn.peer)]) {
14921501
self.peer[JSON.stringify(conn.peer)] = null;
@@ -1601,7 +1610,7 @@ nmCln.prototype.punchHole = function(to, fn){
16011610
(to.isInitiator)? 'initiator' : 'peer');
16021611
}, (to.timeout || 6)*1000); // 6s timeout in default
16031612

1604-
conn.once('open', function(){
1613+
conn.on('open', function(){
16051614
clearTimeout(t);
16061615

16071616
console.log('punch hole successfully in %s mode from %s',
@@ -1618,8 +1627,8 @@ nmCln.prototype.punchHole = function(to, fn){
16181627

16191628
// 1.3.1
16201629
// pass connection external ip/port info back to name-server
1621-
fn(null, {moaddr: coninfo.peerip, moport: coninfo.peerport,
1622-
poaddr: conn._socket.remoteAddress, poport: conn._socket.remotePort});
1630+
fn(null, {moaddr: coninfo.peerip, moport: coninfo.peerport,
1631+
poaddr: conn.remoteAddress, poport: conn.remotePort});
16231632

16241633
// 1.3.2
16251634
// maintain or close hole-punch connection
@@ -1670,7 +1679,7 @@ nmCln.prototype.punchHole = function(to, fn){
16701679
(to.isInitiator)? 'initiator' : 'peer');
16711680
});
16721681

1673-
conn.once('close', function(){
1682+
conn.on('close', function(){
16741683
if (Debug)
16751684
console.log('punch hole closed in %s mode from %s',
16761685
(to.mode === SEP.SEP_MODE_PP) ? 'p2p' : 'c/s',
@@ -1790,7 +1799,7 @@ nmCln.prototype.punchHole = function(to, fn){
17901799
(to.isInitiator)? 'initiator' : 'peer');
17911800
}, (to.timeout || 6)*1000); // 6s timeout in default
17921801

1793-
conn.once('open', function(){
1802+
conn.on('open', function(){
17941803
clearTimeout(t);
17951804

17961805
console.log('punch hole successfully in %s mode from %s @symmetric',
@@ -1807,8 +1816,8 @@ nmCln.prototype.punchHole = function(to, fn){
18071816

18081817
// 2.1.4.1
18091818
// pass connection external ip/port info back to name-server
1810-
fn(null, {moaddr: coninfo.peerip, moport: coninfo.peerport,
1811-
poaddr: conn._socket.remoteAddress, poport: conn._socket.remotePort});
1819+
fn(null, {moaddr: coninfo.peerip, moport: coninfo.peerport,
1820+
poaddr: conn.remoteAddress, poport: conn.remotePort});
18121821

18131822
// 2.1.4.2
18141823
// maintain or close hole-punch connection
@@ -1859,7 +1868,7 @@ nmCln.prototype.punchHole = function(to, fn){
18591868
(to.isInitiator)? 'initiator' : 'peer');
18601869
});
18611870

1862-
conn.once('close', function(){
1871+
conn.on('close', function(){
18631872
if (Debug)
18641873
console.log('punch hole closed in %s mode from %s @symmetric',
18651874
(to.mode === SEP.SEP_MODE_PP) ? 'p2p' : 'c/s',
@@ -2328,9 +2337,7 @@ nmCln.prototype.sendOpcMsg = function(opc_msg, fn){
23282337
var con0 = JSON.stringify(self.srvs[0]); // connection to the first name-server
23292338

23302339
// check connection status
2331-
if ((self.conn[con0] && self.conn[con0].socket) &&
2332-
(self.conn[con0].socket.readyState != undefined) &&
2333-
(self.conn[con0].socket.readyState === self.conn[con0].socket.OPEN)) {
2340+
if (self.conn[con0] && self.conn[con0].socket) {
23342341
// send opc message
23352342
try {
23362343
// fill offer message count as sequence number

lib/iwebpp.io.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ var nmCln = exports = module.exports = function(options, fn){
7878
fn = options;
7979
options = {};
8080
}
81-
81+
82+
// check on version
83+
self.version = 1;
84+
8285
/////////////////////////////////////////////////////////////////
8386
// arguments check
8487
if (options === undefined || options.usrinfo === undefined) {
@@ -578,7 +581,7 @@ nmCln.prototype._LSM_setupSdpSession = function() {
578581
// 2.1.2
579582
// enable vURL if TURN ready
580583
if (self.turnagentReady) {
581-
var vurlproto = 'https://';
584+
var vurlproto = self.secmode ? 'https://' : 'http://';
582585

583586
if (self.vmode === vURL.URL_MODE_PATH) {
584587
// vpath-based turn vURL

0 commit comments

Comments
 (0)