Skip to content

Commit

Permalink
cli: peer names
Browse files Browse the repository at this point in the history
* peer: finish porting to new pripub API
  • Loading branch information
indutny committed Aug 12, 2012
1 parent b9a2a68 commit d12545d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
41 changes: 31 additions & 10 deletions lib/vock/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Cli.prototype.prompt = function _prompt(query, callback) {
self.logger.resume();
process.stdin.resume();
process.stdin.setRawMode(true);

listeners.forEach(function(listener) {
process.stdin.addListener('keypress', listener);
});
Expand Down Expand Up @@ -147,16 +148,34 @@ Cli.prototype.onServerAddr = function onServerAddr(address, port) {
});

this.instance.on('authorize', function(fingerprint, callback) {
var property = {
name: 'authorized',
message: 'Accept ' + fingerprint + '?',
validatior: /y[es]*|n[o]?/,
warning: 'Must respond yes or no',
default: 'no'
};
var name = nconf.get('peers:' + fingerprint),
property = {
name: 'authorized',
message: 'Accept ' + (name || 'unknown') + '(' + fingerprint + ')?',
validatior: /y[es]*|n[o]?/,
warning: 'Must respond yes or no',
default: 'no'
};

self.prompt(property, function(result) {
callback(/y[es]*/.test(result.authorized));
var ok = /y[es]*/.test(result.authorized);

callback(ok);

// Skip one tick to restore all keyboard watchers
process.nextTick(function() {
// Ask for peer's name
if (ok && !name) {
var property = {
name: 'name',
message: 'Enter peer\'s name (' + fingerprint + '): '
};
self.prompt(property, function(result) {
nconf.set('peers:' + fingerprint, result.name);
nconf.save();
});
}
});
});
});

Expand Down Expand Up @@ -185,8 +204,10 @@ Cli.prototype.onServerAddr = function onServerAddr(address, port) {
});

this.instance.on('peer:text', function (fingerprint, text) {
self.notify(fingerprint + ': ' + text);
self.logger.write((fingerprint + ' says: ').green + text);
var name = nconf.get('peers:' + fingerprint) || fingerprint;

self.notify(name + ': ' + text);
self.logger.write((name + ' says: ').green + text);
});

this.instance.on('nat:traversal', function (how, port) {
Expand Down
4 changes: 2 additions & 2 deletions lib/vock/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ Instance.prototype.getPeer = function getPeer(info) {

// Handle peer errors
peer.on('error', function(e) {
self.emit(e);
peer.emit('close');
peer.emit('close', 'error');
self.emit('error', e);
});

peer.once('connect', function() {
Expand Down
37 changes: 27 additions & 10 deletions lib/vock/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ function Peer(options) {

var self = this;

this.options = options;

// Protocol configurations
this.version = [0,1];
this.seqs = {};
Expand Down Expand Up @@ -57,9 +59,14 @@ function Peer(options) {
// Load private key
this.state = 'init';
this.pripub.init(function(err) {
if (err) throw err;
if (err) return self.emit('error', err);

self.state = 'idle';
self._initQueue.forEach(function(callback) {
callback.call(self);
});
});
this._initQueue = [];
};
util.inherits(Peer, EventEmitter);

Expand Down Expand Up @@ -282,16 +289,20 @@ Peer.prototype.handleHello = function handleHello(packet) {

if (!ok) return;

self.fingerprint = fingerprint;
self.write('handshake', {
type: 'acpt',
dh: new Buffer(
pripub.create({ pub: packet.public }).encrypt(self.dh.getPublicKey()),
'binary'
)
});
pripub.create({
pub: packet.public,
password: self.options.password
}).init(function(err, p) {
if (err) return self.emit('error', err);

self.fingerprint = fingerprint;
self.write('handshake', {
type: 'acpt',
dh: new Buffer(p.encrypt(self.dh.getPublicKey()), 'binary')
});

if (!self.connecting) self.connect(self.roomId);
if (!self.connecting) self.connect(self.roomId);
});
});

var interval = setInterval(function() {
Expand Down Expand Up @@ -398,6 +409,12 @@ Peer.prototype.close = function close() {
// Connect to opponent
//
Peer.prototype.connect = function connect(id, callback) {
if (this.state === 'init') {
this._initQueue.push(function() {
this.connect(id, callback);
});
return;
}
if (this.connecting) return;

var self = this,
Expand Down

0 comments on commit d12545d

Please sign in to comment.