⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketmaster.java

📁 java实现的遗传算法
💻 JAVA
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.distr.sockets;

import java.io.*;
import java.net.*;
import java.nio.channels.*;
import org.jgap.distr.*;

/**
 * Sockets implementation of org.jgap.distr.Master
 *
 * @author Klaus Meffert
 * @since 2.4
 */
public class SocketMaster
    extends Master {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.3 $";

  /**
   * Port defined as default for this socket server.
   */
  public final static int DEFAULT_SOCKET_PORT = 4444;

  private ServerSocket serverSocket;

  public SocketMaster(RequestDispatcher a_dispatcher,
                      WorkerListener a_workerListener)
      throws Exception {
    super(a_dispatcher, a_workerListener);
  }

  /**
   * Starts the master listener.
   * @throws IOException
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public void start()
      throws Exception {
    http://www-128.ibm.com/developerworks/java/library/j-javaio/

    serverSocket = null;
    boolean listening = true;
//    try {
//      serverSocket = new ServerSocket(DEFAULT_SOCKET_PORT);
//    }
//    catch (IOException ex) {
//      System.err.println("Could not listen on port: " + DEFAULT_SOCKET_PORT);
//      ex.printStackTrace();
//      System.exit( -1);
//    }
//    System.out.println("Now listening for worker requests...");
  //
    // Create a non-blocking server socket and check for connections
    try {
      // Create the selector
      Selector selector = Selector.open();
      // Create a non-blocking server socket channel on port 80
      ServerSocketChannel ssChannel = ServerSocketChannel.open();
      ssChannel.configureBlocking(false);
      int port = DEFAULT_SOCKET_PORT;
      ssChannel.socket().bind(new InetSocketAddress(port));
      // Register channel with selector (other channels, too)
      ssChannel.register(selector, SelectionKey.OP_ACCEPT);
      System.out.println("Now listening for worker requests...");

      if (true)
        /**@todo move following to thread class*/
        while (true) {
          // Wait for an event
          selector.select();
          // Get list of selection keys with pending events
          java.util.Iterator it = selector.selectedKeys().iterator();
          // Process each key
          while (it.hasNext()) {
            // Get the selection key
            SelectionKey selKey = (SelectionKey) it.next();
            // Remove it from the list to indicate that it is being processed
            it.remove();
            // Check if it's a connection request
            if (selKey.isAcceptable()) {
              // Get channel with connection request
              ServerSocketChannel ssChannel2 = (ServerSocketChannel) selKey.
                  channel();
              // See e178 Accepting a Connection on a ServerSocketChannel
              // for an example of accepting a connection request
              try {
                // Accept the connection request.
                // If serverSocketChannel is blocking, this method blocks.
                // The returned channel is in blocking mode.
                SocketChannel sChannel = ssChannel.accept();
                // If serverSocketChannel is non-blocking, sChannel may be null
                if (sChannel == null) {
                  // There were no pending connection requests; try again later.
                  // To be notified of connection requests,
                  // see e179 Using a Selector to Manage Non-Blocking Server Sockets.
                }
                else {
                  // Use the socket channel to communicate with the client
                  // See e176 Using a Selector to Manage Non-Blocking Sockets.
                }
              }
              catch (IOException e) {
              }
            }
          }
        }

      // Get port that received the connection request; this information
      // might be useful in determining how to handle the connection
      int localPort = ssChannel.socket().getLocalPort();

    }
    catch (IOException e) {
      e.printStackTrace();
    }
//    SocketMasterThread mt = new SocketMasterThread(serverSocket);
//    mt.start();
    while (listening) {
//      new SocketWorkerListenerThread(serverSocket.accept()).start();
      /**@todo accept is blocking until connection is established!*/
      listenToRequests();
    }
    serverSocket.close();
  }

  /**
   * Starting a master instance for testing purposes.
   * @param args ignored
   * @throws Exception
   * @author Klaus Meffert
   * @since 2.4
   */
  public static void main(String[] args)
      throws Exception {
    int port = DEFAULT_SOCKET_PORT + 1;
    int timeout = 3000;
    RequestDispatcher dispatcher = new SocketRequestDispatcher();
    SocketWorkerListener workerListener = new SocketWorkerListener(port,
        timeout);
    SocketMaster server = new SocketMaster(dispatcher, workerListener);
    server.start();
  }

  public synchronized void listenToRequests() {
    /**@todo check if new requests are to be sent from the master to its workers*/
    System.out.println("Listening...");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -