Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package OraclePinger;
- import java.io.IOException;
- // Thread for connecting to all targets in parallel via a single selector
- public class Connector extends Thread {
- Selector sel;
- // Printer printer;
- // List of pending targets. We use this list because if we try to
- // register a channel with the selector while the connector thread is
- // blocked in the selector then we will block.
- LinkedList<Target> pending = new LinkedList<Target>();
- public Connector(/*Printer pr*/) throws Exception {
- // printer = pr;
- sel = Selector.open();
- setName("Connector");
- }
- // Initiate a connection sequence to the given target and add the
- // target to the pending-target list
- public void add(Target t) {
- SocketChannel sc = null;
- try {
- // Open the channel, set it to non-blocking, initiate connect
- sc = SocketChannel.open();
- sc.configureBlocking(false);
- // Record the time we started
- t.channel = sc;
- t.connectStart = System.currentTimeMillis();
- boolean connected = sc.connect(t.address);
- if (connected) {
- t.connectFinish = t.connectStart;
- sc.close();
- // printer.add(t);
- } else {
- // Add the new channel to the pending list
- synchronized (pending) {
- pending.add(t);
- }
- // Nudge the selector so that it will process the pending list
- sel.wakeup();
- }
- } catch (IOException x) {
- if (sc != null) {
- try {
- sc.close();
- } catch (IOException xx) {
- // print sc close IOception
- }
- }
- t.failure = x;
- // printer.add(t);
- }
- }
- // Process any targets in the pending list
- void processPendingTargets() throws IOException {
- synchronized (pending) {
- while (pending.size() > 0) {
- Target t = (Target)pending.removeFirst();
- try {
- // Register the channel with the selector, indicating
- // interest in connection completion and attaching the
- // target object so that we can get the target back
- // after the key is added to the selector's
- // selected-key set
- t.channel.register(sel, SelectionKey.OP_CONNECT, t);
- } catch (IOException x) {
- // Something went wrong, so close the channel and
- // record the failure
- t.channel.close();
- t.failure = x;
- // printer.add(t);
- }
- }
- }
- }
- // Process keys that have become selected
- void processSelectedKeys() throws IOException {
- for ( SelectionKey sk : sel.selectedKeys() ) {//Iterator i = sel.selectedKeys().iterator(); i.hasNext();) {
- // Retrieve the next key and remove it from the set
- //sk = (SelectionKey)i.next();
- //i.remove();
- // Retrieve the target and the channel
- Target t = (Target)sk.attachment();
- SocketChannel sc = (SocketChannel)sk.channel();
- // Attempt to complete the connection sequence
- try {
- if (sc.finishConnect()) {
- sk.cancel();
- t.connectFinish = System.currentTimeMillis();
- sc.close();
- // printer.add(t);
- }
- } catch (IOException x) {
- sc.close();
- t.failure = x;
- // printer.add(t);
- }
- }
- }
- volatile boolean shutdown = false;
- // Invoked by the main thread when it's time to shut down
- public void shutdown() {
- shutdown = true;
- sel.wakeup();
- }
- // Connector thread loop
- public void run() {
- for (;;) {
- try {
- int n = sel.select();
- if (n > 0)
- processSelectedKeys();
- processPendingTargets();
- if (shutdown) {
- sel.close();
- return;
- }
- } catch (IOException x) {
- x.printStackTrace();
- }
- }
- }
- //synchronize it to avoid thread interference &/ memory inconsistence during list removal
- // public synchronized void clearList(){
- // pending.clear();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement