1 jls.loader.provide('jls.net.SelectionHandlerSequence'); 2 3 jls.loader.require('jls.net.SelectionHandler'); 4 5 jls.net.SelectionHandlerSequence = jls.lang.Class.create(jls.net.SelectionHandler, /** @lends jls.net.SelectionHandlerSequence.prototype */ 6 { 7 /** 8 * Creates a sequence of selection handlers. 9 * 10 * @constructs 11 * @augments jls.net.SelectionHandler 12 * @class A selection handler for reading and writing a sequence of selection handlers. 13 */ 14 initialize : function() { 15 this._handlers = []; 16 this._current = 0; 17 this._canRead = true; 18 this._canWrite = true; 19 for (var i = 0; i < arguments.length; i++) { 20 this.addSelectionHandler(arguments[i]); 21 } 22 }, 23 addSelectionHandler : function(handler) { 24 if (! (handler.canRead() || handler.canWrite())) { 25 throw new jls.lang.Exception('Invalid selection handler'); 26 } 27 if (handler.canRead() != handler.canWrite()) { 28 if (handler.canRead() && this._canRead) { 29 this._canWrite = false; 30 } else if (handler.canWrite() && this._canWrite) { 31 this._canRead = false; 32 } else { 33 throw new jls.lang.Exception('Invalid selection handler sequence'); 34 } 35 } 36 if (this._handlers.length == 0) { 37 handler.reset(); 38 } 39 this._handlers.push(handler); 40 return this; 41 }, 42 getSelectionHandler : function(index) { 43 return this._handlers[index]; 44 }, 45 clear : function() { 46 this._handlers = []; 47 this._canRead = true; 48 this._canWrite = true; 49 }, 50 hasLength : function() { 51 for (var i = 0; i < this._handlers.length; i++) { 52 if (! this._handlers[i].hasLength()) { 53 return false; 54 } 55 } 56 return true; 57 }, 58 length : function() { 59 if (! this.hasLength()) { 60 throw new jls.lang.Exception('UnsupportedOperation'); 61 } 62 var l = 0; 63 for (var i = 0; i < this._handlers.length; i++) { 64 l += this._handlers[i].length(); 65 } 66 return l; 67 }, 68 canRead : function() { 69 return this._canRead; 70 }, 71 canWrite : function() { 72 return this._canWrite; 73 }, 74 reset : function() { 75 this._current = 0; 76 if (this._handlers.length > 0) { 77 this._handlers[0].reset(); 78 } 79 return this; 80 }, 81 onSelect : function(op, channel) { 82 for (; this._current < this._handlers.length;) { 83 var status = this._handlers[this._current].onSelect(op, channel); 84 if (status <= jls.net.SelectionHandler.STATUS_IN_PROGRESS) { // In progress or failed 85 return status; 86 } 87 // else, completed 88 this._current++; 89 if (this._current < this._handlers.length) { 90 this._handlers[this._current].reset(); 91 } 92 } 93 return jls.net.SelectionHandler.STATUS_DONE; 94 }, 95 onRead : function(channel) { 96 return this.onSelect(jls.net.SelectionHandler.OP_READ, channel); 97 }, 98 onWrite : function(channel) { 99 return this.onSelect(jls.net.SelectionHandler.OP_WRITE, channel); 100 } 101 }); 102