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         if (this._current >= this._handlers.length) {
 83             // this.reset();
 84             return jls.net.SelectionHandler.STATUS_DONE;
 85         }
 86         for (;;) {
 87             var status = this._handlers[this._current].onSelect(op, channel);
 88             //jls.logger.info('SelectionHandlerSequence.onSelect(' + op + ')' + ', current: ' + this._current + '/' + this._handlers.length + ', status: ' + status);
 89             if (status <= jls.net.SelectionHandler.STATUS_IN_PROGRESS) { // In progress or failed
 90                 return status;
 91             }
 92             // else, completed
 93             this._current++;
 94 			if (this._current < this._handlers.length) {
 95 				this._handlers[this._current].reset();
 96 			} else {
 97                 break;
 98             }
 99         }
100         return jls.net.SelectionHandler.STATUS_DONE;
101     },
102     onRead : function(channel) {
103         return this.onSelect(jls.net.SelectionHandler.OP_READ, channel);
104     },
105     onWrite : function(channel) {
106         return this.onSelect(jls.net.SelectionHandler.OP_WRITE, channel);
107     }
108 });
109