messageProcessor.start();
while( !_stop ) {
StreamConnection stream = null;
InputStream input = null;
DataBuffer db = null;
try {
/**
* Does this need to be synch'd? This should be a single threaded instance that runs autonomously on a port.
*
* Might want to call this in the the PushService class to catch IOPortAlreadyBoundException so we can properly
* throw this back up via JavaScript so the developer realizes that the port they tried to open is unavailable.
*/
synchronized( this ) {
_notify = (StreamConnectionNotifier) Connector.open( getURL() );
}
while( !_stop ) {
// block for data
stream = _notify.acceptAndOpen();
try {
// create input stream
input = stream.openInputStream();
// extract the data from the input stream
db = new DataBuffer();
byte[] data = new byte[ CHUNK_SIZE ];
int chunk = 0;
while( -1 != ( chunk = input.read( data ) ) ) {
db.write( data, 0, chunk );
}
// trim the array - the buffer in DataBuffer grows
// past the size and fills with empty chars
db.trim();
// synchronize this block to avoid race conditions with the queue
synchronized( _messageQueue ) {
// Create push object, add to queue for callback thread
PushData pd = new PushData( stream, input, db.getArray() );
_messageQueue.addElement( pd );
_messageQueue.notify();
}
} catch( IOException e1 ) {
// a problem occurred with the input stream