1 jls.loader.provide('jls.net.SelectionHandler'); 2 3 /** 4 * @class A selection handler is capable of multiple operations on channel (reading, writing). 5 * The channel must provide the associated operations (read, write). 6 * The result of a selection handle is a number indicating the handling status (>0: completed, 0: in progress, <0: failed). 7 */ 8 jls.net.SelectionHandler = jls.lang.Class.create(/** @lends jls.net.SelectionHandler.prototype */ 9 { 10 canRead : function() { 11 return 'onRead' in this; 12 }, 13 canWrite : function() { 14 return 'onWrite' in this; 15 }, 16 hasLength : function() { 17 return 'length' in this; 18 }, 19 canDo : function(op) { 20 var name = 'can' + op.capitalize(); 21 return (name in this) && this[name](); 22 }, 23 reset : function() { 24 }, 25 onSelect : function(op, channel) { 26 return this['on' + op.capitalize()](channel); 27 }, 28 getOpMode : function(op) { 29 return this._opMode; 30 }, 31 setOpMode : function(op) { 32 if (this._opMode == null) { 33 if (this.canDo(op)) { 34 this._opMode = op; 35 } else if (this._opMode != op) { 36 throw new jls.lang.Exception('Invalid op mode "' + op + '"'); 37 } 38 } else if (this._opMode != op) { 39 throw new jls.lang.Exception('Invalid op mode "' + op + '" != "' + this._opMode + '"'); 40 } 41 return this; 42 } 43 }); 44 45 Object.extend(jls.net.SelectionHandler, /** @lends jls.net.SelectionHandler */ 46 { 47 STATUS_DONE : 1, 48 STATUS_IN_PROGRESS : 0, 49 STATUS_FAILURE : -1, 50 OP_READ : 'read', 51 OP_WRITE : 'write' 52 }); 53 54