|
| 1 | +var util = require('util'); |
| 2 | +var EventEmitter = require('events').EventEmitter; |
| 3 | +var ndn = require('ndn-on-node'); |
| 4 | + |
| 5 | + |
| 6 | +// ---------------------------------------------------------------- |
| 7 | +// class Face |
| 8 | +var Face = function Face() { |
| 9 | + EventEmitter.call(this); |
| 10 | + this.closed = false; |
| 11 | + this.close_on_error = true; |
| 12 | +}; |
| 13 | +util.inherits(Face, EventEmitter); |
| 14 | + |
| 15 | +Face.prototype.register = function Face_register(facemgr) { |
| 16 | + facemgr.register(this);// sets this.id |
| 17 | +}; |
| 18 | + |
| 19 | +// public property id |
| 20 | + |
| 21 | +// public abstract method desc |
| 22 | +Face.prototype.desc = function Face_desc(msg) { |
| 23 | + throw new Error('not implemented'); |
| 24 | +}; |
| 25 | + |
| 26 | +// public method send |
| 27 | +Face.prototype.send = function Face_send(msg) { |
| 28 | + if (this.closed) return; |
| 29 | + if (msg instanceof ndn.Interest || msg instanceof ndn.ContentObject) { |
| 30 | + var pkt = msg.encodeToBinary(); |
| 31 | + this.sendpkt(pkt); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +// protected abstract method sendpkt |
| 36 | +Face.prototype.sendpkt = function Face_sendpkt(pkt) { |
| 37 | + throw new Error('not implemented'); |
| 38 | +}; |
| 39 | + |
| 40 | +// protected property close_on_error |
| 41 | + |
| 42 | +// protected method recvpkt |
| 43 | +// pkt must be a complete message |
| 44 | +Face.prototype.recvpkt = function Face_recvpkt(pkt) { |
| 45 | + var msg = false; |
| 46 | + try { |
| 47 | + var d = new ndn.BinaryXMLDecoder(pkt); |
| 48 | + if (d.peekStartElement(ndn.CCNProtocolDTags.Interest)) { |
| 49 | + var interest = new ndn.Interest(); |
| 50 | + interest.from_ccnb(d); |
| 51 | + msg = interest; |
| 52 | + } else if (d.peekStartElement(ndn.CCNProtocolDTags.ContentObject)) { |
| 53 | + var co = new ndn.ContentObject(); |
| 54 | + co.from_ccnb(d); |
| 55 | + msg = co; |
| 56 | + } |
| 57 | + } catch(ex) { |
| 58 | + msg = false; |
| 59 | + console.log('Face.recvpkt() decode error from '+this.id+(this.close_on_error?', closing':'')); |
| 60 | + if (this.close_on_error) this.close(); |
| 61 | + } |
| 62 | + if (msg !== false) this.recv(msg); |
| 63 | +}; |
| 64 | + |
| 65 | +// protected method recv |
| 66 | +Face.prototype.recv = function Face_recv(msg) { |
| 67 | + if (this.closed) return; |
| 68 | + msg.incoming_face = this.id; |
| 69 | + this.emit('recv', msg); |
| 70 | +}; |
| 71 | + |
| 72 | +// public event recv(Interest|ContentObject) |
| 73 | + |
| 74 | +// public method close |
| 75 | +Face.prototype.close = function Face_close() { |
| 76 | + this.closed = true; |
| 77 | + this.close_internal(); |
| 78 | + this.emit('close'); |
| 79 | +}; |
| 80 | + |
| 81 | +// protected abstract method close_internal |
| 82 | +Face.prototype.close_internal = function Face_close_internal() { |
| 83 | + throw new Error('not implemented'); |
| 84 | +}; |
| 85 | + |
| 86 | +// public event close() |
| 87 | +// Face is closed locally or by remote peer. |
| 88 | + |
| 89 | +// protected method end |
| 90 | +// invoked when face is closed by remote peer |
| 91 | +Face.prototype.end = function Face_end() { |
| 92 | + if (this.closed) return; |
| 93 | + this.closed = true; |
| 94 | + this.emit('end'); |
| 95 | + this.emit('close'); |
| 96 | +}; |
| 97 | + |
| 98 | +// public event end() |
| 99 | + |
| 100 | + |
| 101 | +// ---------------------------------------------------------------- |
| 102 | +exports.Face = Face; |
| 103 | + |
0 commit comments