List of usage examples for java.nio.channels SelectionKey isReadable
public final boolean isReadable()
From source file:org.gldapdaemon.core.ldap.LDAPListener.java
public final void run() { log.info("LDAP server started successfully."); // Create variables SelectionKey key, newKey; SocketChannel channel;//from w w w . ja v a 2 s .c o m Socket socket = null; Iterator keys; int n; // Server loop for (;;) { try { // Select sockets try { socket = null; n = selector.select(); } catch (NullPointerException closedError) { // Ignore Selector bug - client socket closed if (log.isDebugEnabled()) { log.debug("Socket closed.", closedError); } sleep(5000); continue; } catch (ClosedSelectorException interrupt) { break; } catch (Exception selectError) { // Unknown exception - stop server log.warn("Unable to select sockets!", selectError); break; } if (n != 0) { // Get an iterator over the set of selected keys keys = selector.selectedKeys().iterator(); if (keys == null) { sleep(5000); continue; } // Look at each key in the selected set while (keys.hasNext()) { key = (SelectionKey) keys.next(); keys.remove(); // Nothing to do if (key == null) { sleep(5000); continue; } // Check key status if (key.isValid()) { // Accept new incoming connection if (key.isAcceptable()) { channel = serverChannel.accept(); if (channel != null) { // Register new socket connection socket = channel.socket(); channel.configureBlocking(false); newKey = channel.register(selector, SelectionKey.OP_READ); processAccept(newKey); } } else { if (key.isReadable()) { // Read from socket connection socket = ((SocketChannel) key.channel()).socket(); processRead(key); } else { // Write to socket connection if (key.isWritable()) { socket = ((SocketChannel) key.channel()).socket(); processWrite(key); } } } } } } } catch (InterruptedException interrupt) { closeSocket(socket); break; } catch (IOException socketClosed) { closeSocket(socket); continue; } catch (Exception processingException) { closeSocket(socket); log.warn(processingException.getMessage(), processingException); } } log.info("LDAP server stopped."); }
From source file:org.apache.nifi.processor.util.listen.dispatcher.SocketChannelDispatcher.java
@Override public void run() { while (!stopped) { try {//from ww w . j av a2s . c om int selected = selector.select(); // if stopped the selector could already be closed which would result in a ClosedSelectorException if (selected > 0 && !stopped) { Iterator<SelectionKey> selectorKeys = selector.selectedKeys().iterator(); // if stopped we don't want to modify the keys because close() may still be in progress while (selectorKeys.hasNext() && !stopped) { SelectionKey key = selectorKeys.next(); selectorKeys.remove(); if (!key.isValid()) { continue; } if (key.isAcceptable()) { // Handle new connections coming in final ServerSocketChannel channel = (ServerSocketChannel) key.channel(); final SocketChannel socketChannel = channel.accept(); // Check for available connections if (currentConnections.incrementAndGet() > maxConnections) { currentConnections.decrementAndGet(); logger.warn("Rejecting connection from {} because max connections has been met", new Object[] { socketChannel.getRemoteAddress().toString() }); IOUtils.closeQuietly(socketChannel); continue; } logger.debug("Accepted incoming connection from {}", new Object[] { socketChannel.getRemoteAddress().toString() }); // Set socket to non-blocking, and register with selector socketChannel.configureBlocking(false); SelectionKey readKey = socketChannel.register(selector, SelectionKey.OP_READ); // Prepare the byte buffer for the reads, clear it out ByteBuffer buffer = bufferPool.poll(); buffer.clear(); buffer.mark(); // If we have an SSLContext then create an SSLEngine for the channel SSLSocketChannel sslSocketChannel = null; if (sslContext != null) { final SSLEngine sslEngine = sslContext.createSSLEngine(); sslEngine.setUseClientMode(false); switch (clientAuth) { case REQUIRED: sslEngine.setNeedClientAuth(true); break; case WANT: sslEngine.setWantClientAuth(true); break; case NONE: sslEngine.setNeedClientAuth(false); sslEngine.setWantClientAuth(false); break; } sslSocketChannel = new SSLSocketChannel(sslEngine, socketChannel); } // Attach the buffer and SSLSocketChannel to the key SocketChannelAttachment attachment = new SocketChannelAttachment(buffer, sslSocketChannel); readKey.attach(attachment); } else if (key.isReadable()) { // Clear out the operations the select is interested in until done reading key.interestOps(0); // Create a handler based on the protocol and whether an SSLEngine was provided or not final Runnable handler; if (sslContext != null) { handler = handlerFactory.createSSLHandler(key, this, charset, eventFactory, events, logger); } else { handler = handlerFactory.createHandler(key, this, charset, eventFactory, events, logger); } // run the handler executor.execute(handler); } } } // Add back all idle sockets to the select SelectionKey key; while ((key = keyQueue.poll()) != null) { key.interestOps(SelectionKey.OP_READ); } } catch (IOException e) { logger.error("Error accepting connection from SocketChannel", e); } } }
From source file:HttpDownloadManager.java
public void run() { log.info("HttpDownloadManager thread starting."); // The download thread runs until release() is called while (!released) { // The thread blocks here waiting for something to happen try {//from w ww .j a v a 2 s . c om selector.select(); } catch (IOException e) { // This should never happen. log.log(Level.SEVERE, "Error in select()", e); return; } // If release() was called, the thread should exit. if (released) break; // If any new Download objects are pending, deal with them first if (!pendingDownloads.isEmpty()) { // Although pendingDownloads is a synchronized list, we still // need to use a synchronized block to iterate through its // elements to prevent a concurrent call to download(). synchronized (pendingDownloads) { Iterator iter = pendingDownloads.iterator(); while (iter.hasNext()) { // Get the pending download object from the list DownloadImpl download = (DownloadImpl) iter.next(); iter.remove(); // And remove it. // Now begin an asynchronous connection to the // specified host and port. We don't block while // waiting to connect. SelectionKey key = null; SocketChannel channel = null; try { // Open an unconnected channel channel = SocketChannel.open(); // Put it in non-blocking mode channel.configureBlocking(false); // Register it with the selector, specifying that // we want to know when it is ready to connect // and when it is ready to read. key = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_CONNECT, download); // Create the web server address SocketAddress address = new InetSocketAddress(download.host, download.port); // Ask the channel to start connecting // Note that we don't send the HTTP request yet. // We'll do that when the connection completes. channel.connect(address); } catch (Exception e) { handleError(download, channel, key, e); } } } } // Now get the set of keys that are ready for connecting or reading Set keys = selector.selectedKeys(); if (keys == null) continue; // bug workaround; should not be needed // Loop through the keys in the set for (Iterator i = keys.iterator(); i.hasNext();) { SelectionKey key = (SelectionKey) i.next(); i.remove(); // Remove the key from the set before handling // Get the Download object we attached to the key DownloadImpl download = (DownloadImpl) key.attachment(); // Get the channel associated with the key. SocketChannel channel = (SocketChannel) key.channel(); try { if (key.isConnectable()) { // If the channel is ready to connect, complete the // connection and then send the HTTP GET request to it. if (channel.finishConnect()) { download.status = Status.CONNECTED; // This is the HTTP request we wend String request = "GET " + download.path + " HTTP/1.1\r\n" + "Host: " + download.host + "\r\n" + "Connection: close\r\n" + "\r\n"; // Wrap in a CharBuffer and encode to a ByteBuffer ByteBuffer requestBytes = LATIN1.encode(CharBuffer.wrap(request)); // Send the request to the server. If the bytes // aren't all written in one call, we busy loop! while (requestBytes.hasRemaining()) channel.write(requestBytes); log.info("Sent HTTP request: " + download.host + ":" + download.port + ": " + request); } } if (key.isReadable()) { // If the key indicates that there is data to be read, // then read it and store it in the Download object. int numbytes = channel.read(buffer); // If we read some bytes, store them, otherwise // the download is complete and we need to note this if (numbytes != -1) { buffer.flip(); // Prepare to drain the buffer download.addData(buffer); // Store the data buffer.clear(); // Prepare for another read log.info("Read " + numbytes + " bytes from " + download.host + ":" + download.port); } else { // If there are no more bytes to read key.cancel(); // We're done with the key channel.close(); // And with the channel. download.status = Status.DONE; if (download.listener != null) // notify listener download.listener.done(download); log.info("Download complete from " + download.host + ":" + download.port); } } } catch (Exception e) { handleError(download, channel, key, e); } } } log.info("HttpDownloadManager thread exiting."); }
From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.OutgoingConnectionThread.java
@Override public void run() { while (!isInterrupted()) { synchronized (this.pendingConnectionRequests) { if (!this.pendingConnectionRequests.isEmpty()) { final OutgoingConnection outgoingConnection = this.pendingConnectionRequests.poll(); try { final SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); final SelectionKey key = socketChannel.register(this.selector, SelectionKey.OP_CONNECT); socketChannel.connect(outgoingConnection.getConnectionAddress()); key.attach(outgoingConnection); } catch (final IOException ioe) { // IOException is reported by separate thread to avoid deadlocks final Runnable reporterThread = new Runnable() { @Override public void run() { outgoingConnection.reportConnectionProblem(ioe); }// ww w.j av a 2s. co m }; new Thread(reporterThread).start(); } } } synchronized (this.pendingWriteEventSubscribeRequests) { if (!this.pendingWriteEventSubscribeRequests.isEmpty()) { final SelectionKey oldSelectionKey = this.pendingWriteEventSubscribeRequests.poll(); final OutgoingConnection outgoingConnection = (OutgoingConnection) oldSelectionKey.attachment(); final SocketChannel socketChannel = (SocketChannel) oldSelectionKey.channel(); try { final SelectionKey newSelectionKey = socketChannel.register(this.selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); newSelectionKey.attach(outgoingConnection); outgoingConnection.setSelectionKey(newSelectionKey); } catch (final IOException ioe) { // IOException is reported by separate thread to avoid deadlocks final Runnable reporterThread = new Runnable() { @Override public void run() { outgoingConnection.reportTransmissionProblem(ioe); } }; new Thread(reporterThread).start(); } } } synchronized (this.connectionsToClose) { final Iterator<Map.Entry<OutgoingConnection, Long>> closeIt = this.connectionsToClose.entrySet() .iterator(); final long now = System.currentTimeMillis(); while (closeIt.hasNext()) { final Map.Entry<OutgoingConnection, Long> entry = closeIt.next(); if ((entry.getValue().longValue() + MIN_IDLE_TIME_BEFORE_CLOSE) < now) { final OutgoingConnection outgoingConnection = entry.getKey(); closeIt.remove(); // Create new thread to close connection to avoid deadlocks final Runnable closeThread = new Runnable() { @Override public void run() { try { outgoingConnection.closeConnection(); } catch (IOException ioe) { outgoingConnection.reportTransmissionProblem(ioe); } } }; new Thread(closeThread).start(); } } } try { this.selector.select(10); } catch (IOException e) { LOG.error(e); } final Iterator<SelectionKey> iter = this.selector.selectedKeys().iterator(); while (iter.hasNext()) { final SelectionKey key = iter.next(); iter.remove(); if (key.isValid()) { if (key.isConnectable()) { doConnect(key); } else { if (key.isReadable()) { doRead(key); // A read will always result in an exception, so the write key will not be valid anymore continue; } if (key.isWritable()) { doWrite(key); } } } else { LOG.error("Received invalid key: " + key); } } } // Finally, try to close the selector try { this.selector.close(); } catch (IOException ioe) { LOG.debug(StringUtils.stringifyException(ioe)); } }
From source file:com.packetsender.android.PacketListenerService.java
@Override protected void onHandleIntent(Intent intent) { dataStore = new DataStorage(getSharedPreferences(DataStorage.PREFS_SETTINGS_NAME, 0), getSharedPreferences(DataStorage.PREFS_SAVEDPACKETS_NAME, 0), getSharedPreferences(DataStorage.PREFS_SERVICELOG_NAME, 0), getSharedPreferences(DataStorage.PREFS_MAINTRAFFICLOG_NAME, 0)); listenportTCP = dataStore.getTCPPort(); listenportUDP = dataStore.getUDPPort(); Log.i("service", DataStorage.FILE_LINE("TCP: " + listenportTCP + " / UDP: " + listenportUDP)); Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0); startNotification();/*from w ww . ja va 2s. co m*/ CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder(); ByteBuffer response = null; try { response = encoder.encode(CharBuffer.wrap("response")); } catch (CharacterCodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { SocketAddress localportTCP = new InetSocketAddress(listenportTCP); SocketAddress localportUDP = new InetSocketAddress(listenportUDP); tcpserver = ServerSocketChannel.open(); tcpserver.socket().bind(localportTCP); udpserver = DatagramChannel.open(); udpserver.socket().bind(localportUDP); tcpserver.configureBlocking(false); udpserver.configureBlocking(false); Selector selector = Selector.open(); tcpserver.register(selector, SelectionKey.OP_ACCEPT); udpserver.register(selector, SelectionKey.OP_READ); ByteBuffer receiveBuffer = ByteBuffer.allocate(1024); receiveBuffer.clear(); shutdownListener = new Runnable() { public void run() { if (false) { try { tcpserver.close(); } catch (IOException e) { } try { udpserver.close(); } catch (IOException e) { } stopSelf(); } else { mHandler.postDelayed(shutdownListener, 2000); } } }; sendListener = new Runnable() { public void run() { //Packet fetchedPacket = mDbHelper.needSendPacket(); Packet[] fetchedPackets = dataStore.fetchAllServicePackets(); if (fetchedPackets.length > 0) { dataStore.clearServicePackets(); Log.d("service", DataStorage.FILE_LINE("sendListener found " + fetchedPackets.length + " packets")); for (int i = 0; i < fetchedPackets.length; i++) { Packet fetchedPacket = fetchedPackets[i]; Log.d("service", DataStorage.FILE_LINE("send packet " + fetchedPacket.toString())); } new SendPacketsTask().execute(fetchedPackets); } mHandler.postDelayed(sendListener, 2000); } }; //start shutdown listener mHandler.postDelayed(shutdownListener, 2000); //start send listener mHandler.postDelayed(sendListener, 5000); while (true) { try { // Handle per-connection problems below // Wait for a client to connect Log.d("service", DataStorage.FILE_LINE("waiting for connection")); selector.select(); Log.d("service", DataStorage.FILE_LINE("client connection")); Set keys = selector.selectedKeys(); for (Iterator i = keys.iterator(); i.hasNext();) { SelectionKey key = (SelectionKey) i.next(); i.remove(); Channel c = (Channel) key.channel(); if (key.isAcceptable() && c == tcpserver) { SocketChannel client = tcpserver.accept(); if (client != null) { Socket tcpSocket = client.socket(); packetCounter++; DataInputStream in = new DataInputStream(tcpSocket.getInputStream()); byte[] buffer = new byte[1024]; int received = in.read(buffer); byte[] bufferConvert = new byte[received]; System.arraycopy(buffer, 0, bufferConvert, 0, bufferConvert.length); Packet storepacket = new Packet(); storepacket.tcpOrUdp = "TCP"; storepacket.fromIP = tcpSocket.getInetAddress().getHostAddress(); storepacket.toIP = "You"; storepacket.fromPort = tcpSocket.getPort(); storepacket.port = tcpSocket.getLocalPort(); storepacket.data = bufferConvert; UpdateNotification("TCP:" + storepacket.toAscii(), "From " + storepacket.fromIP); Log.i("service", DataStorage.FILE_LINE("Got TCP")); //dataStore.SavePacket(storepacket); /* Intent tcpIntent = new Intent(); tcpIntent.setAction(ResponseReceiver.ACTION_RESP); tcpIntent.addCategory(Intent.CATEGORY_DEFAULT); tcpIntent.putExtra(PARAM_OUT_MSG, storepacket.name); sendBroadcast(tcpIntent); */ storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); if (false) //mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSE).equalsIgnoreCase("Yes")) { storepacket = new Packet(); storepacket.name = dataStore.currentTimeStamp(); ; storepacket.tcpOrUdp = "TCP"; storepacket.fromIP = "You"; storepacket.toIP = tcpSocket.getInetAddress().getHostAddress(); storepacket.fromPort = tcpSocket.getLocalPort(); storepacket.port = tcpSocket.getPort(); // storepacket.data = Packet.toBytes(mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSETEXT)); storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); client.write(response); // send response } client.close(); // close connection } } else if (key.isReadable() && c == udpserver) { DatagramSocket udpSocket; DatagramPacket udpPacket; byte[] buffer = new byte[2048]; // Create a packet to receive data into the buffer udpPacket = new DatagramPacket(buffer, buffer.length); udpSocket = udpserver.socket(); receiveBuffer.clear(); InetSocketAddress clientAddress = (InetSocketAddress) udpserver.receive(receiveBuffer); if (clientAddress != null) { String fromAddress = clientAddress.getAddress().getHostAddress(); packetCounter++; int received = receiveBuffer.position(); byte[] bufferConvert = new byte[received]; System.arraycopy(receiveBuffer.array(), 0, bufferConvert, 0, bufferConvert.length); Packet storepacket = new Packet(); storepacket.tcpOrUdp = "UDP"; storepacket.fromIP = clientAddress.getAddress().getHostAddress(); storepacket.toIP = "You"; storepacket.fromPort = clientAddress.getPort(); storepacket.port = udpSocket.getLocalPort(); storepacket.data = bufferConvert; UpdateNotification("UDP:" + storepacket.toAscii(), "From " + storepacket.fromIP); //dataStore.SavePacket(storepacket); storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); if (false)//mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSE).trim().equalsIgnoreCase("Yes")) { storepacket = new Packet(); storepacket.name = dataStore.currentTimeStamp(); ; storepacket.tcpOrUdp = "UDP"; storepacket.fromIP = "You"; storepacket.toIP = clientAddress.getAddress().getHostAddress(); storepacket.fromPort = udpSocket.getLocalPort(); storepacket.port = clientAddress.getPort(); // storepacket.data = Packet.toBytes(mDbHelper.getSettings(PSDbAdapter.KEY_SETTINGS_SENDRESPONSETEXT)); //dataStore.SavePacket(storepacket); udpserver.send(response, clientAddress); storepacket.nowMe(); dataStore.saveTrafficPacket(storepacket); Log.d("service", DataStorage.FILE_LINE("sendBroadcast")); } } } } } catch (java.io.IOException e) { Log.i("service", DataStorage.FILE_LINE("IOException ")); } catch (Exception e) { Log.w("service", DataStorage.FILE_LINE("Fatal Error: " + Log.getStackTraceString(e))); } } } catch (BindException e) { //mDbHelper.putServiceError("Error binding to port"); dataStore.putToast("Port already in use."); Log.w("service", DataStorage.FILE_LINE("Bind Exception: " + Log.getStackTraceString(e))); } catch (Exception e) { //mDbHelper.putServiceError("Fatal Error starting service"); Log.w("service", DataStorage.FILE_LINE("Startup error: " + Log.getStackTraceString(e))); } stopNotification(); }
From source file:org.commoncrawl.io.NIOSocketSelector.java
/** * poll method - poll the registered socket for events and potentially block * for IO for the specified timeout value * /* ww w . ja v a 2s .c o m*/ * @param timeoutValue * - amount of time in MS to wait (block) for IO * * */ public int poll(long timeoutValue, TimeUsageDetail timeUsageDetailOut) throws IOException { long timeStart = System.currentTimeMillis(); if (_lastPollTime != -1 && (timeStart - _lastPollTime) >= 30000) { LOG.error("POLL Delta Too Long:" + (timeStart - _lastPollTime)); } _lastPollTime = timeStart; if (timeUsageDetailOut != null) { timeUsageDetailOut.blockedTime = 0; timeUsageDetailOut.unblockedTime = 0; } if (_selector == null || !_selector.isOpen()) { IOException e = new IOException("Selector NULL or Selector is Not Open!"); LOG.error(e); throw e; } processPendingRegistrations(); long timeEnd = System.currentTimeMillis(); if (timeUsageDetailOut != null) { timeUsageDetailOut.unblockedTime += (timeEnd - timeStart); } timeStart = System.currentTimeMillis(); int count = _selector.select(timeoutValue); timeEnd = System.currentTimeMillis(); if (timeUsageDetailOut != null) { timeUsageDetailOut.blockedTime += (timeEnd - timeStart); } long unblockedTimeStart = System.currentTimeMillis(); // if (count != 0 ) { Set<SelectionKey> selectionSet = _selector.selectedKeys(); for (Iterator<SelectionKey> i = selectionSet.iterator(); i.hasNext();) { SelectionKey selectionKey = i.next(); i.remove(); if (selectionKey.isValid()) { NIOSocket theSocket = (NIOSocket) selectionKey.attachment(); if (theSocket != null && theSocket.getListener() != null) { // reset interest ops selectionKey.interestOps(0); // process events in key ... if (selectionKey.isConnectable()) { boolean connected = false; Exception disconnectException = null; try { if (((NIOClientSocket) theSocket).finishConnect()) { connected = true; // log it ... // LOG.info("Connected to:"+((NIOClientSocket)theSocket).getSocketAddress()); // reset the selection key's ops.. otherwise, select keeps // returning on an already connected socket (since we have // registered for CONNECT) System.out.println( "Connected to:" + ((NIOClientSocket) theSocket).getSocketAddress()); timeStart = System.currentTimeMillis(); ((NIOClientSocketListener) theSocket.getListener()) .Connected((NIOClientSocket) theSocket); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInConnectedEvt += System.currentTimeMillis() - timeStart; } } else { // LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress() // + " - finishConnect returned false"); theSocket.close(); } } catch (IOException e) { // LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress() // + " with Exception:"+e); theSocket.close(); disconnectException = e; } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Connected Event:" + StringUtils.stringifyException(e)); ((NIOClientSocketListener) theSocket.getListener()).Excepted(theSocket, e); theSocket.close(); disconnectException = e; // KILL THE SERVER throw e; } // if we were unable to properly establish the connection, trigger // the Disconnected notification ... if (!connected) { // LOG.error("Failed to Complete Connection in Finish Connect- Calling Disconnected"); ((NIOClientSocketListener) theSocket.getListener()).Disconnected(theSocket, disconnectException); // continue to the next socket ... continue; } } // now always set the socket to readable state ... if ((theSocket instanceof NIOClientSocket) && selectionKey.isValid()) { selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_READ); } if (selectionKey.isValid() && selectionKey.isReadable()) { int bytesRead = -1; try { timeStart = System.currentTimeMillis(); // track the number of actual bytes read in the callback ... bytesRead = ((NIOClientSocketListener) theSocket.getListener()) .Readable((NIOClientSocket) theSocket); // System.out.println("Readable Took:" + // (System.currentTimeMillis() - timeStart)); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInReadableEvt += System.currentTimeMillis() - timeStart; } if (bytesRead == -1) { // log it ... // LOG.error("Abnormal Disconnect Detected on Socket:"+ // ((NIOClientSocket)theSocket).getSocketAddress()); // trigger a disconnect event ... ((NIOClientSocketListener) theSocket.getListener()).Disconnected(theSocket, null); // close the socket ... theSocket.close(); } } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Readable Event:" + StringUtils.stringifyException(e)); ((NIOClientSocketListener) theSocket.getListener()).Excepted(theSocket, e); theSocket.close(); // KILL THE SERVER throw e; } // if bytesRead == -1 then this means that the underlying connection // has gone bad ... } if (selectionKey.isValid() && selectionKey.isWritable()) { try { timeStart = System.currentTimeMillis(); ((NIOClientSocketListener) theSocket.getListener()) .Writeable((NIOClientSocket) theSocket); // System.out.println("Writable Took:" + // (System.currentTimeMillis() - timeStart)); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInWritableEvt += System.currentTimeMillis() - timeStart; } } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Readable Event:" + StringUtils.stringifyException(e)); ((NIOClientSocketListener) theSocket.getListener()).Excepted(theSocket, e); theSocket.close(); // KILL THE SERVER throw e; } } if (selectionKey.isValid() && selectionKey.isAcceptable()) { ((NIOServerSocket) theSocket).acceptable(); // re-register for accept on this socket selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_ACCEPT); } } } else { LOG.error("Invalid Socket Detected. Calling Disconnect"); NIOSocket theSocket = (NIOSocket) selectionKey.attachment(); if (theSocket != null && theSocket.getListener() != null) { theSocket.getListener().Disconnected(theSocket, null); } } } long unblockedTimeEnd = System.currentTimeMillis(); if (timeUsageDetailOut != null) { timeUsageDetailOut.unblockedTime += (unblockedTimeEnd - unblockedTimeStart); } // } return count; }
From source file:middleware.NewServerSocket.java
public void run() { int count = 0; while (!sharedData.isEndOfProgram()) { try {/* ww w .j a v a2s .c om*/ if (monitoring) { selector.select(10); } else { selector.select(); } } catch (IOException e) { e.printStackTrace(); } keyIterator = selector.selectedKeys().iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); keyIterator.remove(); if (key.isAcceptable()) { if (key.channel() == serverSocketChannel) { SocketChannel socketChannel = null; try { socketChannel = serverSocketChannel.accept(); } catch (IOException e) { e.printStackTrace(); } if (socketChannel != null) { MiddleClient middleClient = new MiddleClient(sharedData.getServerIpAddr(), sharedData.getServerPortNum()); middleClient.startClient(); MiddleServer middleServer = new MiddleServer(socketChannel); TransactionData transactionData = new TransactionData(sharedData, middleServer); middleServer.startServer(transactionData); int len = 0; buffer.clear(); len = middleClient.getInput(buffer); middleServer.sendOutput(buffer, len); buffer.clear(); len = middleServer.getInput(buffer); transactionData.setUserId(getUserId(data)); middleClient.sendOutput(buffer, len); middleServer.setNonBlocking(); middleClient.setNonBlocking(); if (sharedData.isOutputToFile()) { transactionData.openFileOutputStream(); } sharedData.allTransactionData.add(transactionData); workers[count % numWorkers].socketMap.put(middleServer.socketChannel, middleServer); workers[count % numWorkers].socketMap.put(middleClient.socketChannel, middleClient); middleServer.register(workers[count % numWorkers].selector, middleClient); middleClient.register(workers[count % numWorkers].selector, middleServer); ++count; } } else if (key.channel() == adminServerSocketChannel) { SocketChannel sock = null; try { sock = adminServerSocketChannel.accept(); } catch (IOException e) { e.printStackTrace(); } if (sock != null) { try { sock.configureBlocking(true); } catch (IOException e) { e.printStackTrace(); } MiddleSocketChannel middleSocketChannel = new MiddleSocketChannel(sock); middleSocketChannel.setNonBlocking(); middleSocketChannel.register(selector, middleSocketChannel); } } } else if (key.isReadable()) { MiddleSocketChannel middleSocketChannel = (MiddleSocketChannel) key.attachment(); buffer.clear(); int len = middleSocketChannel.getInput(buffer); if (len == -1) { middleSocketChannel.cancelKey(); continue; } buffer.position(0); int packetID = 0; long packetLength = -1; boolean isValidPacket = true; try { packetID = buffer.getInt(); packetLength = buffer.getLong(); } catch (BufferUnderflowException e) { buffer.clear(); buffer.putInt(102); String response = "Invalid packet header"; buffer.putLong(response.length()); buffer.put(response.getBytes()); isValidPacket = false; middleSocketChannel.sendOutput(buffer, buffer.position()); } if (isValidPacket) { if (packetID == 100) { if (userList.contains(middleSocketChannel)) { buffer.clear(); buffer.putInt(102); String response = "You have already logged in"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } else if (packetLength <= 0) { buffer.clear(); buffer.putInt(102); String response = "Invalid packet length"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { String userID = null; byte[] password = new byte[Encrypt.MAX_LENGTH]; byte[] packet = new byte[(int) packetLength]; buffer.get(packet); userID = parseLogInPacket(packet, password); if (userInfo.get(userID) != null && Arrays.equals(((byte[]) userInfo.get(userID)), Encrypt.encrypt(password))) { buffer.clear(); buffer.putInt(101); buffer.putLong(0); middleSocketChannel.sendOutput(buffer, buffer.position()); userList.add(middleSocketChannel); } else { buffer.clear(); buffer.putInt(102); String response = "Invalid User ID or password"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } } } else if (packetID == 103) { stopIncrementalLogging(); } else if (packetID == 200) { if (userList.contains(middleSocketChannel)) { if (sharedData.isOutputToFile() || endingMonitoring || sendingFiles) { String response = "Current monitoring not finished"; buffer.clear(); buffer.putInt(202); buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { startMonitoring(); buffer.clear(); buffer.putInt(201); buffer.putLong(0); middleSocketChannel.sendOutput(buffer, buffer.position()); curUser = middleSocketChannel; } } else { buffer.clear(); buffer.putInt(102); String response = "You have not been registered"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } } else if (packetID == 300) { if (userList.contains(middleSocketChannel)) { if (!sharedData.isOutputToFile()) { String response = "No monitoring running"; buffer.clear(); buffer.putInt(302); buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); // DY : I think this use case is not right at the moment where // monitoring is only stoppable by a user who started it. // } else if (middleSocketChannel != curUser) { // String response = "Monitoring running by other user"; // buffer.clear(); // buffer.putInt(302); // buffer.putLong(response.length()); // buffer.put(response.getBytes()); // middleSocketChannel.sendOutput(buffer, buffer.position()); } else if (endingMonitoring) { String response = "Writing log files, please wait"; buffer.clear(); buffer.putInt(302); buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { sendLog = true; curUser = middleSocketChannel; stopMonitoring(); } } else { buffer.clear(); buffer.putInt(102); String response = "You have not been registered"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } } else if (packetID == 303) { if (userList.contains(middleSocketChannel)) { if (!sharedData.isOutputToFile()) { String response = "No monitoring running"; buffer.clear(); buffer.putInt(302); buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); // } else if (middleSocketChannel != curUser) { // String response = "Monitoring running by other user"; // buffer.clear(); // buffer.putInt(302); // buffer.putLong(response.length()); // buffer.put(response.getBytes()); // middleSocketChannel.sendOutput(buffer, buffer.position()); } else if (endingMonitoring) { String response = "Writing log files, please wait"; buffer.clear(); buffer.putInt(302); buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { sendLog = false; stopMonitoring(); } } else { buffer.clear(); buffer.putInt(102); String response = "You have not been registered"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } } else if (packetID == 400) { if (userList.contains(middleSocketChannel)) { // when the GUI reconnects and the middleware is still monitoring... if (monitoring) { // start new logging threads and resume monitoring stopIncrementalLogging(); startIncrementalLogging(true); buffer.clear(); buffer.putInt(402); buffer.putLong(0); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { buffer.clear(); buffer.putInt(401); buffer.putLong(0); middleSocketChannel.sendOutput(buffer, buffer.position()); } } else { buffer.clear(); buffer.putInt(102); String response = "You have not been registered"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } } else if (packetID == 500) { if (!sharedData.isLiveMonitoring()) { buffer.clear(); buffer.putInt(501); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { buffer.clear(); buffer.putInt(502); LiveAggregateGlobal globalAggregate = sharedData.liveMonitor.globalAggregate; int numTransactionType = globalAggregate.getNumTransactionType(); buffer.putInt(numTransactionType); buffer.putDouble(globalAggregate.totalTransactionCount); for (int i = 0; i < numTransactionType; ++i) { buffer.putDouble( globalAggregate.transactionStatistics.get(i).currentTransactionCounts); // current TPS buffer.putDouble( globalAggregate.transactionStatistics.get(i).currentAverageLatency); // current average latency. buffer.putDouble( globalAggregate.transactionStatistics.get(i).totalTransactionCounts); // total transaction count } middleSocketChannel.sendOutput(buffer, buffer.position()); } } else if (packetID == 600) { int type = buffer.getInt(); int index = buffer.getInt(); String[] samples = sharedData.liveMonitor.getTransactionSamples(type); if (samples == null) { buffer.clear(); buffer.putInt(601); middleSocketChannel.sendOutput(buffer, buffer.position()); } else if (samples.length < index + 1) { buffer.clear(); buffer.putInt(601); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { String sample = samples[index]; buffer.clear(); buffer.putInt(602); buffer.putLong(sample.length()); buffer.put(sample.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } } else if (packetID == 700) { int type = buffer.getInt(); sharedData.liveMonitor.removeTransactionType(type); buffer.clear(); buffer.putInt(701); buffer.putInt(sharedData.liveMonitor.globalAggregate.getNumTransactionType()); middleSocketChannel.sendOutput(buffer, buffer.position()); } else { buffer.clear(); buffer.putInt(102); String response = "Invalid packet ID"; buffer.putLong(response.length()); buffer.put(response.getBytes()); middleSocketChannel.sendOutput(buffer, buffer.position()); } } } } if (!sharedData.allTransactions.isEmpty() || !sharedData.allQueries.isEmpty()) { int c = sharedData.allQueries.size(); while (c > 0) { printQueries(); --c; } // c = sharedData.allStatementsInfo.size(); // while (c > 0) { // printStatementsInfo(); // --c; // } c = sharedData.allTransactions.size(); while (c > 0) { printTransactions(); --c; } } else if (endingMonitoring) { try { sharedData.tAllLogFileOutputStream.flush(); sharedData.tAllLogFileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { sharedData.sAllLogFileOutputStream.flush(); sharedData.sAllLogFileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { sharedData.qAllLogFileOutputStream.flush(); sharedData.qAllLogFileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } if (curUser != null && sendLog) { // System.out.println("ready to compress log files"); if (stopRemoteDstat != null) { Interrupter interrupter = new Interrupter(Thread.currentThread()); interrupter.start(); try { stopRemoteDstat.waitFor(); interrupter.setOuterThreadWaiting(false); } catch (InterruptedException e) { e.printStackTrace(); } stopRemoteDstat = null; } buffer.clear(); buffer.putInt(301); buffer.putLong(0); curUser.sendOutput(buffer, buffer.position()); // if (zipAllFiles()) { // // System.out // .println("finish compressing files, ready to send zip file"); // // File zipFile = new File(zipFileName); // // FileInputStream fis = null; // try { // fis = new FileInputStream(zipFile); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } // // FileChannel fc = fis.getChannel(); // // buffer.clear(); // buffer.putInt(301); // buffer.putLong(zipFile.length()); // curUser.sendOutput(buffer, buffer.position()); // long position = 0; // long remaining = zipFile.length(); // long len = 0; // while (remaining > 0) { // try { // len = fc.transferTo(position, 1024, curUser.socketChannel); // } catch (IOException e) { // e.printStackTrace(); // } // position += len; // remaining -= len; // len = 0; // } // // System.out.println("finish sending zip file"); // // } else { // String response = "fail to compress log files"; // buffer.clear(); // buffer.putInt(302); // buffer.putLong(response.length()); // buffer.put(response.getBytes()); // curUser.sendOutput(buffer, buffer.position()); // } curUser = null; } endingMonitoring = false; monitoring = false; if (System.getProperty("user.name").contentEquals("root")) { String[] cmd = { "/bin/bash", "shell/chmod" }; try { Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); } } } } }
From source file:org.commoncrawl.io.internal.NIOSocketSelector.java
/** poll method - poll the registered socket for events and potentially block for IO for the * specified timeout value /*from www . j a v a2 s . c om*/ * * @param timeoutValue - amount of time in MS to wait (block) for IO * * */ @SuppressWarnings("unchecked") public int poll(long timeoutValue, TimeUsageDetail timeUsageDetailOut) throws IOException { long timeStart = System.currentTimeMillis(); if (_lastPollTime != -1 && (timeStart - _lastPollTime) >= 30000) { LOG.error("POLL Delta Too Long:" + (timeStart - _lastPollTime)); } _lastPollTime = timeStart; if (timeUsageDetailOut != null) { timeUsageDetailOut.blockedTime = 0; timeUsageDetailOut.unblockedTime = 0; } if (_selector == null || !_selector.isOpen()) { IOException e = new IOException("Selector NULL or Selector is Not Open!"); LOG.error(e); throw e; } processPendingRegistrations(); long timeEnd = System.currentTimeMillis(); if (timeUsageDetailOut != null) { timeUsageDetailOut.unblockedTime += (timeEnd - timeStart); } /* if (timeoutWatchSet.size() != 0) { // We have a timeout pending, so calculate the time until then and select appropriately long nextTimeout = timeoutWatchSet.first().getTimeoutTimestamp(); long selectTime = nextTimeout - System.currentTimeMillis(); if (selectTime < timeoutValue) { timeoutValue = Math.max(selectTime,0); } } */ timeStart = System.currentTimeMillis(); int count = 0; if (timeoutValue <= 0) { count = _selector.selectNow(); } else { if (timeoutValue == Long.MAX_VALUE) timeoutValue = 0; count = _selector.select(timeoutValue); } timeEnd = System.currentTimeMillis(); if (timeUsageDetailOut != null) { timeUsageDetailOut.blockedTime += (timeEnd - timeStart); } long unblockedTimeStart = System.currentTimeMillis(); // if (count != 0 ) { Set<SelectionKey> selectionSet = _selector.selectedKeys(); for (Iterator<SelectionKey> i = selectionSet.iterator(); i.hasNext();) { SelectionKey selectionKey = i.next(); i.remove(); if (selectionKey.isValid()) { Object attachment = selectionKey.attachment(); /* if (attachment instanceof TAsyncMethodCall) { transitionThriftMethod((TAsyncMethodCall)attachment,selectionKey); } */ if (attachment instanceof NIOSocket) { NIOSocket theSocket = (NIOSocket) selectionKey.attachment(); if (theSocket != null && theSocket.getListener() != null) { // reset interest ops selectionKey.interestOps(0); // process events in key ... if (selectionKey.isConnectable()) { boolean connected = false; Exception disconnectException = null; try { if (((NIOClientSocket) theSocket).finishConnect()) { connected = true; // log it ... // LOG.info("Connected to:"+((NIOClientSocket)theSocket).getSocketAddress()); // reset the selection key's ops.. otherwise, select keeps returning on an already connected socket (since we have registered for CONNECT) System.out.println( "Connected to:" + ((NIOClientSocket) theSocket).getSocketAddress()); timeStart = System.currentTimeMillis(); ((NIOClientSocketListener) theSocket.getListener()) .Connected((NIOClientSocket) theSocket); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInConnectedEvt += System.currentTimeMillis() - timeStart; } } else { //LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress() + " - finishConnect returned false"); theSocket.close(); } } catch (IOException e) { //LOG.error("Failed to Connect to:"+((NIOClientSocket)theSocket).getSocketAddress() + " with Exception:"+e); theSocket.close(); disconnectException = e; } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Connected Event:" + StringUtils.stringifyException(e)); ((NIOClientSocketListener) theSocket.getListener()) .Excepted((NIOClientSocket) theSocket, e); theSocket.close(); disconnectException = e; //KILL THE SERVER //throw e; } // if we were unable to properly establish the connection, trigger the Disconnected notification ... if (!connected) { //LOG.error("Failed to Complete Connection in Finish Connect- Calling Disconnected"); ((NIOClientSocketListener) theSocket.getListener()) .Disconnected((NIOClientSocket) theSocket, disconnectException); // continue to the next socket ... continue; } } // now always set the socket to readable state ... if ((theSocket instanceof NIOClientSocket) && selectionKey.isValid()) { selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_READ); } if (selectionKey.isValid() && selectionKey.isReadable()) { int bytesRead = -1; try { timeStart = System.currentTimeMillis(); // track the number of actual bytes read in the callback ... bytesRead = ((NIOClientSocketListener) theSocket.getListener()) .Readable((NIOClientSocket) theSocket); //System.out.println("Readable Took:" + (System.currentTimeMillis() - timeStart)); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInReadableEvt += System.currentTimeMillis() - timeStart; } if (bytesRead == -1) { // log it ... // LOG.error("Abnormal Disconnect Detected on Socket:"+ ((NIOClientSocket)theSocket).getSocketAddress()); // trigger a disconnect event ... ((NIOClientSocketListener) theSocket.getListener()) .Disconnected((NIOClientSocket) theSocket, null); // close the socket ... theSocket.close(); } } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Readable Event:" + StringUtils.stringifyException(e)); ((NIOClientSocketListener) theSocket.getListener()) .Excepted((NIOClientSocket) theSocket, e); theSocket.close(); //KILL THE SERVER // throw e; } // if bytesRead == -1 then this means that the underlying connection has gone bad ... } if (selectionKey.isValid() && selectionKey.isWritable()) { try { timeStart = System.currentTimeMillis(); ((NIOClientSocketListener) theSocket.getListener()) .Writeable((NIOClientSocket) theSocket); // System.out.println("Writable Took:" + (System.currentTimeMillis() - timeStart)); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInWritableEvt += System.currentTimeMillis() - timeStart; } } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Readable Event:" + StringUtils.stringifyException(e)); ((NIOClientSocketListener) theSocket.getListener()) .Excepted((NIOClientSocket) theSocket, e); theSocket.close(); //KILL THE SERVER ? //throw e; } } if (selectionKey.isValid() && selectionKey.isAcceptable()) { ((NIOServerSocket) theSocket).acceptable(); // re-register for accept on this socket selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_ACCEPT); } } } // exernally managed socket (thrift client socket) else if (attachment instanceof NIOClientSocketListener) { NIOClientSocketListener listener = (NIOClientSocketListener) attachment; // reset interest ops selectionKey.interestOps(0); // now always set the socket to readable state ... selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_READ); if (selectionKey.isValid() && selectionKey.isReadable()) { int bytesRead = -1; try { timeStart = System.currentTimeMillis(); // track the number of actual bytes read in the callback ... bytesRead = listener.Readable(null); //System.out.println("Readable Took:" + (System.currentTimeMillis() - timeStart)); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInReadableEvt += System.currentTimeMillis() - timeStart; } if (bytesRead == -1) { // log it ... // LOG.error("Abnormal Disconnect Detected on Socket:"+ ((NIOClientSocket)theSocket).getSocketAddress()); // trigger a disconnect event ... listener.Disconnected(null, null); } } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Readable Event:" + StringUtils.stringifyException(e)); listener.Excepted(null, e); } // if bytesRead == -1 then this means that the underlying connection has gone bad ... } if (selectionKey.isValid() && selectionKey.isWritable()) { try { timeStart = System.currentTimeMillis(); listener.Writeable(null); // System.out.println("Writable Took:" + (System.currentTimeMillis() - timeStart)); if (timeUsageDetailOut != null) { timeUsageDetailOut.timeInWritableEvt += System.currentTimeMillis() - timeStart; } } catch (RuntimeException e) { LOG.error("Caught Runtime Exception in Readable Event:" + StringUtils.stringifyException(e)); listener.Excepted(null, e); } } } } else { LOG.error("Invalid Socket Detected. Calling Disconnect"); NIOSocket theSocket = (NIOSocket) selectionKey.attachment(); if (theSocket != null && theSocket.getListener() != null) { theSocket.getListener().Disconnected(theSocket, null); } } } //timeoutThriftMethods(); //startPendingThriftMethods(); long unblockedTimeEnd = System.currentTimeMillis(); if (timeUsageDetailOut != null) { timeUsageDetailOut.unblockedTime += (unblockedTimeEnd - unblockedTimeStart); } // } return count; }
From source file:com.app.services.ExecutorServiceThread.java
public void run() { // create a selector that will by used for multiplexing. The selector // registers the socketserverchannel as // well as all socketchannels that are created String CLIENTCHANNELNAME = "clientChannel"; String SERVERCHANNELNAME = "serverChannel"; String channelType = "channelType"; ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>(); ClassLoader classLoader = null; ByteArrayOutputStream bstr;/*from w w w . j a v a 2s . co m*/ ByteBuffer buffer = null; ByteBuffer lengthBuffer = ByteBuffer.allocate(4); int bytesRead; InputStream bais; ObjectInputStream ois; Object object; ExecutorServiceInfo executorServiceInfo = null; Random random = new Random(System.currentTimeMillis()); // register the serversocketchannel with the selector. The OP_ACCEPT // option marks // a selection key as ready when the channel accepts a new connection. // When the // socket server accepts a connection this key is added to the list of // selected keys of the selector. // when asked for the selected keys, this key is returned and hence we // know that a new connection has been accepted. try { Selector selector = Selector.open(); SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // SelectionKey socketServerSelectionKey1 = // channel.register(selector1, // SelectionKey.OP_ACCEPT); // set property in the key that identifies the channel Map<String, String> properties = new ConcurrentHashMap<String, String>(); properties.put(channelType, SERVERCHANNELNAME); socketServerSelectionKey.attach(properties); // wait for the selected keys SelectionKey key = null; Set<SelectionKey> selectedKeys; // logger.info("Instance Number"+instanceNumber); Iterator<SelectionKey> iterator = null; SocketChannel clientChannel = null; while (true) { try { // the select method is a blocking method which returns when // atleast // one of the registered // channel is selected. In this example, when the socket // accepts // a // new connection, this method // will return. Once a socketclient is added to the list of // registered channels, then this method // would also return when one of the clients has data to be // read // or // written. It is also possible to perform a nonblocking // select // using the selectNow() function. // We can also specify the maximum time for which a select // function // can be blocked using the select(long timeout) function. if (selector.select() >= 0) { selectedKeys = selector.selectedKeys(); iterator = selectedKeys.iterator(); } while (iterator.hasNext()) { try { key = iterator.next(); // the selection key could either by the // socketserver // informing // that a new connection has been made, or // a socket client that is ready for read/write // we use the properties object attached to the // channel // to // find // out the type of channel. if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) { // a new connection has been obtained. This // channel // is // therefore a socket server. ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // accept the new connection on the server // socket. // Since // the // server socket channel is marked as non // blocking // this channel will return null if no client is // connected. SocketChannel clientSocketChannel = serverSocketChannel.accept(); if (clientSocketChannel != null) { // set the client connection to be non // blocking clientSocketChannel.configureBlocking(false); SelectionKey clientKey = clientSocketChannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE); Map<String, String> clientproperties = new ConcurrentHashMap<String, String>(); clientproperties.put(channelType, CLIENTCHANNELNAME); clientKey.attach(clientproperties); clientKey.interestOps(SelectionKey.OP_READ); // clientSocketChannel.close(); // write something to the new created client /* * CharBuffer buffer = * CharBuffer.wrap("Hello client"); while * (buffer.hasRemaining()) { * clientSocketChannel.write * (Charset.defaultCharset() * .encode(buffer)); } * clientSocketChannel.close(); * buffer.clear(); */ } } else { // data is available for read // buffer for reading clientChannel = (SocketChannel) key.channel(); if (key.isReadable()) { // the channel is non blocking so keep it // open // till // the // count is >=0 clientChannel = (SocketChannel) key.channel(); if (resultMap.get(key) == null) { //log.info(key); bstr = new ByteArrayOutputStream(); object = null; clientChannel.read(lengthBuffer); int length = lengthBuffer.getInt(0); lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } buffer.clear(); //log.info("Message1"+new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ois = new ObjectInputStream(bais); // Offending // line. // Produces // the // StreamCorruptedException. //log.info("In read obect"); object = ois.readObject(); //log.info("Class Cast"); //log.info("Class Cast1"); ois.close(); byte[] params = bstr.toByteArray(); bstr.close(); //log.info("readObject"); //log.info("After readObject"); if (object instanceof CloseSocket) { resultMap.remove(key); clientChannel.close(); key.cancel(); } // clientChannel.close(); String serviceurl = (String) object; String[] serviceRegistry = serviceurl.split("/"); //log.info("classLoaderMap" // + urlClassLoaderMap); //log.info(deployDirectory // + "/" + serviceRegistry[0]); int servicenameIndex; //log.info(earServicesDirectory // + "/" + serviceRegistry[0] // + "/" + serviceRegistry[1]); if (serviceRegistry[0].endsWith(".ear")) { classLoader = (VFSClassLoader) urlClassLoaderMap.get(deployDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]); servicenameIndex = 2; } else if (serviceRegistry[0].endsWith(".jar")) { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } else { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } String serviceName = serviceRegistry[servicenameIndex]; // log.info("servicename:"+serviceName);; synchronized (executorServiceMap) { executorServiceInfo = (ExecutorServiceInfo) executorServiceMap .get(serviceName.trim()); } ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader(); classLoaderExecutorServiceInfo.setClassLoader(classLoader); classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo); resultMap.put(key, classLoaderExecutorServiceInfo); // key.interestOps(SelectionKey.OP_READ); // log.info("Key interested Ops"); // continue; } //Thread.sleep(100); /* * if (classLoader == null) throw new * Exception( * "Could able to obtain deployed class loader" * ); */ /* * log.info( * "current context classloader" + * classLoader); */ //log.info("In rad object"); bstr = new ByteArrayOutputStream(); lengthBuffer.clear(); int numberofDataRead = clientChannel.read(lengthBuffer); //log.info("numberofDataRead" // + numberofDataRead); int length = lengthBuffer.getInt(0); if (length <= 0) { iterator.remove(); continue; } lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); buffer.clear(); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } if (bytesRead <= 0 || bytesRead < length) { //log.info("bytesRead<length"); iterator.remove(); continue; } //log.info(new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap .get(key); ois = new ClassLoaderObjectInputStream( (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending // line. // Produces // the // StreamCorruptedException. object = ois.readObject(); ois.close(); bstr.close(); executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo(); //System.out // .println("inputStream Read Object"); //log.info("Object=" // + object.getClass()); // Thread.currentThread().setContextClassLoader(currentContextLoader); if (object instanceof ExecutorParams) { ExecutorParams exeParams = (ExecutorParams) object; Object returnValue = null; //log.info("test socket1"); String ataKey; ATAConfig ataConfig; ConcurrentHashMap ataServicesMap; Enumeration<NodeResourceInfo> noderesourceInfos = addressmap.elements(); NodeResourceInfo noderesourceinfo = null; String ip = ""; int port = 1000; long memavailable = 0; long memcurr = 0; if (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); ; } } while (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { memcurr = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); if (memavailable <= memcurr) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = memcurr; } } } ATAExecutorServiceInfo servicesAvailable; Socket sock1 = new Socket(ip, port); OutputStream outputStr = sock1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr); NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setClassNameWithPackage( executorServiceInfo.getExecutorServicesClass().getName()); nodeInfo.setMethodName(executorServiceInfo.getMethod().getName()); nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS()); NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam(); nodeInfoMethodParam.setMethodParams(exeParams.getParams()); nodeInfoMethodParam .setMethodParamTypes(executorServiceInfo.getMethodParams()); //log.info("Serializable socket="+sock); //nodeInfo.setSock(sock); //nodeInfo.setOstream(sock.getOutputStream()); objOutputStream.writeObject(nodeInfo); objOutputStream = new ObjectOutputStream(outputStr); objOutputStream.writeObject(nodeInfoMethodParam); ObjectInputStream objInputStream1 = new ObjectInputStream( sock1.getInputStream()); returnValue = objInputStream1.readObject(); objOutputStream.close(); objInputStream1.close(); sock1.close(); /*returnValue = executorServiceInfo .getMethod() .invoke(executorServiceInfo .getExecutorServicesClass() .newInstance(), exeParams.getParams());*/ // Thread.currentThread().setContextClassLoader(oldCL); // log.info("Written Value=" // + returnValue.toString()); resultMap.put(key, returnValue); } key.interestOps(SelectionKey.OP_WRITE); //log.info("Key interested Ops1"); } else if (key.isWritable()) { // the channel is non blocking so keep it // open // till the // count is >=0 //log.info("In write"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make // a // BAOS // stream ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the // stream Object result = resultMap.get(key); oos.writeObject(result); // write an object // to // the stream oos.flush(); oos.close(); byte[] objData = baos.toByteArray(); // get // the // byte // array baos.close(); buffer = ByteBuffer.wrap(objData); // wrap // around // the // data buffer.rewind(); // buffer.flip(); //prep for writing //log.info(new String(objData)); //while (buffer.hasRemaining()) clientChannel.write(buffer); // write resultMap.remove(key); buffer.clear(); key.cancel(); clientChannel.close(); //log.info("In write1"); numberOfServicesRequests++; //log.info("Key interested Ops2"); } } iterator.remove(); } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); key.cancel(); clientChannel.close(); resultMap.remove(key); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); } }
From source file:com.web.services.ExecutorServiceThread.java
public void run() { // create a selector that will by used for multiplexing. The selector // registers the socketserverchannel as // well as all socketchannels that are created String CLIENTCHANNELNAME = "clientChannel"; String SERVERCHANNELNAME = "serverChannel"; String channelType = "channelType"; ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>(); ClassLoader classLoader = null; ByteArrayOutputStream bstr;/*from ww w . ja v a 2 s . c o m*/ ByteBuffer buffer = null; ByteBuffer lengthBuffer = ByteBuffer.allocate(4); int bytesRead; InputStream bais; ObjectInputStream ois; Object object; ExecutorServiceInfo executorServiceInfo = null; Random random = new Random(System.currentTimeMillis()); // register the serversocketchannel with the selector. The OP_ACCEPT // option marks // a selection key as ready when the channel accepts a new connection. // When the // socket server accepts a connection this key is added to the list of // selected keys of the selector. // when asked for the selected keys, this key is returned and hence we // know that a new connection has been accepted. try { Selector selector = Selector.open(); SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // SelectionKey socketServerSelectionKey1 = // channel.register(selector1, // SelectionKey.OP_ACCEPT); // set property in the key that identifies the channel Map<String, String> properties = new ConcurrentHashMap<String, String>(); properties.put(channelType, SERVERCHANNELNAME); socketServerSelectionKey.attach(properties); // wait for the selected keys SelectionKey key = null; Set<SelectionKey> selectedKeys; // logger.info("Instance Number"+instanceNumber); Iterator<SelectionKey> iterator = null; SocketChannel clientChannel = null; while (true) { try { // the select method is a blocking method which returns when // atleast // one of the registered // channel is selected. In this example, when the socket // accepts // a // new connection, this method // will return. Once a socketclient is added to the list of // registered channels, then this method // would also return when one of the clients has data to be // read // or // written. It is also possible to perform a nonblocking // select // using the selectNow() function. // We can also specify the maximum time for which a select // function // can be blocked using the select(long timeout) function. if (selector.select() >= 0) { selectedKeys = selector.selectedKeys(); iterator = selectedKeys.iterator(); } while (iterator.hasNext()) { try { key = iterator.next(); // the selection key could either by the // socketserver // informing // that a new connection has been made, or // a socket client that is ready for read/write // we use the properties object attached to the // channel // to // find // out the type of channel. if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) { // a new connection has been obtained. This // channel // is // therefore a socket server. ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // accept the new connection on the server // socket. // Since // the // server socket channel is marked as non // blocking // this channel will return null if no client is // connected. SocketChannel clientSocketChannel = serverSocketChannel.accept(); if (clientSocketChannel != null) { // set the client connection to be non // blocking clientSocketChannel.configureBlocking(false); SelectionKey clientKey = clientSocketChannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE); Map<String, String> clientproperties = new ConcurrentHashMap<String, String>(); clientproperties.put(channelType, CLIENTCHANNELNAME); clientKey.attach(clientproperties); clientKey.interestOps(SelectionKey.OP_READ); // clientSocketChannel.close(); // write something to the new created client /* * CharBuffer buffer = * CharBuffer.wrap("Hello client"); while * (buffer.hasRemaining()) { * clientSocketChannel.write * (Charset.defaultCharset() * .encode(buffer)); } * clientSocketChannel.close(); * buffer.clear(); */ } } else { // data is available for read // buffer for reading clientChannel = (SocketChannel) key.channel(); if (key.isReadable()) { // the channel is non blocking so keep it // open // till // the // count is >=0 clientChannel = (SocketChannel) key.channel(); if (resultMap.get(key) == null) { //System.out.println(key); bstr = new ByteArrayOutputStream(); object = null; clientChannel.read(lengthBuffer); int length = lengthBuffer.getInt(0); lengthBuffer.clear(); //System.out.println(length); buffer = ByteBuffer.allocate(length); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } buffer.clear(); //System.out.println("Message1"+new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ois = new ObjectInputStream(bais); // Offending // line. // Produces // the // StreamCorruptedException. //System.out.println("In read obect"); object = ois.readObject(); //System.out.println("Class Cast"); //System.out.println("Class Cast1"); ois.close(); byte[] params = bstr.toByteArray(); bstr.close(); //System.out.println("readObject"); //System.out.println("After readObject"); if (object instanceof CloseSocket) { resultMap.remove(key); clientChannel.close(); key.cancel(); } // clientChannel.close(); String serviceurl = (String) object; String[] serviceRegistry = serviceurl.split("/"); //System.out.println("classLoaderMap" // + urlClassLoaderMap); //System.out.println(deployDirectory // + "/" + serviceRegistry[0]); int servicenameIndex; //System.out.println(earServicesDirectory // + "/" + serviceRegistry[0] // + "/" + serviceRegistry[1]); if (serviceRegistry[0].endsWith(".ear")) { classLoader = (VFSClassLoader) urlClassLoaderMap .get(earServicesDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]); servicenameIndex = 2; } else if (serviceRegistry[0].endsWith(".jar")) { classLoader = (WebClassLoader) urlClassLoaderMap .get(jarservicesDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } else { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } String serviceName = serviceRegistry[servicenameIndex]; // System.out.println("servicename:"+serviceName);; synchronized (executorServiceMap) { executorServiceInfo = (ExecutorServiceInfo) executorServiceMap .get(serviceName.trim()); } ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader(); classLoaderExecutorServiceInfo.setClassLoader(classLoader); classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo); resultMap.put(key, classLoaderExecutorServiceInfo); // key.interestOps(SelectionKey.OP_READ); // System.out.println("Key interested Ops"); // continue; } //Thread.sleep(100); /* * if (classLoader == null) throw new * Exception( * "Could able to obtain deployed class loader" * ); */ /* * System.out.println( * "current context classloader" + * classLoader); */ //System.out.println("In rad object"); bstr = new ByteArrayOutputStream(); lengthBuffer.clear(); int numberofDataRead = clientChannel.read(lengthBuffer); //System.out.println("numberofDataRead" // + numberofDataRead); int length = lengthBuffer.getInt(0); if (length <= 0) { iterator.remove(); continue; } lengthBuffer.clear(); //System.out.println(length); buffer = ByteBuffer.allocate(length); buffer.clear(); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } if (bytesRead <= 0 || bytesRead < length) { //System.out.println("bytesRead<length"); iterator.remove(); continue; } //System.out.println(new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap .get(key); ois = new ClassLoaderObjectInputStream( (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending // line. // Produces // the // StreamCorruptedException. object = ois.readObject(); ois.close(); bstr.close(); executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo(); //System.out // .println("inputStream Read Object"); //System.out.println("Object=" // + object.getClass()); // Thread.currentThread().setContextClassLoader(currentContextLoader); if (object instanceof ExecutorParams) { ExecutorParams exeParams = (ExecutorParams) object; Object returnValue = null; //System.out.println("test socket1"); String ataKey; ATAConfig ataConfig; ConcurrentHashMap ataServicesMap; ATAExecutorServiceInfo servicesAvailable; Socket sock1 = new Socket("0.0.0.0", Integer.parseInt(nodesport[random.nextInt(nodesport.length)])); OutputStream outputStr = sock1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr); NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setClassNameWithPackage( executorServiceInfo.getExecutorServicesClass().getName()); nodeInfo.setMethodName(executorServiceInfo.getMethod().getName()); nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS()); NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam(); nodeInfoMethodParam.setMethodParams(exeParams.getParams()); nodeInfoMethodParam .setMethodParamTypes(executorServiceInfo.getMethodParams()); //System.out.println("Serializable socket="+sock); //nodeInfo.setSock(sock); //nodeInfo.setOstream(sock.getOutputStream()); objOutputStream.writeObject(nodeInfo); objOutputStream = new ObjectOutputStream(outputStr); objOutputStream.writeObject(nodeInfoMethodParam); ObjectInputStream objInputStream1 = new ObjectInputStream( sock1.getInputStream()); returnValue = objInputStream1.readObject(); objOutputStream.close(); objInputStream1.close(); sock1.close(); /*returnValue = executorServiceInfo .getMethod() .invoke(executorServiceInfo .getExecutorServicesClass() .newInstance(), exeParams.getParams());*/ // Thread.currentThread().setContextClassLoader(oldCL); // System.out.println("Written Value=" // + returnValue.toString()); resultMap.put(key, returnValue); } key.interestOps(SelectionKey.OP_WRITE); //System.out.println("Key interested Ops1"); } else if (key.isWritable()) { // the channel is non blocking so keep it // open // till the // count is >=0 //System.out.println("In write"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make // a // BAOS // stream ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the // stream Object result = resultMap.get(key); oos.writeObject(result); // write an object // to // the stream oos.flush(); oos.close(); byte[] objData = baos.toByteArray(); // get // the // byte // array baos.close(); buffer = ByteBuffer.wrap(objData); // wrap // around // the // data buffer.rewind(); // buffer.flip(); //prep for writing //System.out.println(new String(objData)); //while (buffer.hasRemaining()) clientChannel.write(buffer); // write resultMap.remove(key); buffer.clear(); key.cancel(); clientChannel.close(); //System.out.println("In write1"); numberOfServicesRequests++; //System.out.println("Key interested Ops2"); } } iterator.remove(); } catch (Exception ex) { ex.printStackTrace(); key.cancel(); clientChannel.close(); resultMap.remove(key); //ex.printStackTrace(); } } } catch (Exception ex) { //ex.printStackTrace(); } } } catch (Exception ex) { //ex.printStackTrace(); } }