public static void main(String[] args) throws IOException, InterruptedException {

    System.out.println("Auction Server starting");
    try { // Get hostname by textual representation of IP address
      InetAddress addr = InetAddress.getLocalHost(); // = InetAddress.getByName("127.0.0.1");
      String hostname = addr.getHostName(); // Get the host name
      System.out.println("ip address = " + addr.toString() + "\nHost name = " + hostname);
    } catch (UnknownHostException e) {
    }

    // used to let host user control flow of program
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    // System.out.println("How many players are needed?");
    int numPlayersNeeded = 2; // Integer.valueOf(br.readLine());

    // make an auction with which clients interact
    Auction myAuction = new Auction(5, numPlayersNeeded); // auction with 5 slots

    // SELECT TYPE OF AUCTION
    Vector<SimultaneousAscendingAuctionServerThread> myThreads =
        new Vector<SimultaneousAscendingAuctionServerThread>();

    ServerSocket serverSocket = null;
    int portNum = 7; // the luckiest port there is!
    // Try reading in the IP and socket number from the text file...
    try {
      BufferedReader in = new BufferedReader(new FileReader("./src/IP_and_Port.txt"));
      // two lines in this file.  First is hostName/IP address, and second is socket number of host
      String hName = in.readLine(); // use the hostname found above and skip this one
      portNum = Integer.valueOf(in.readLine());
      in.close();
    } catch (IOException e) {
    }

    try {
      serverSocket = new ServerSocket(portNum);
    } catch (IOException e) {
      System.err.println("AuctionServer could not listen on port: " + portNum + ".");
      System.exit(-1);
    }

    System.out.println("Clients should join the auction now...");
    boolean started = false;
    boolean firstThread = true; // used to designate first thread as "reporter"
    while (!started) { // (listening && !myAuction.isOver() ){

      SimultaneousAscendingAuctionServerThread aThread =
          new SimultaneousAscendingAuctionServerThread(
              serverSocket.accept(), myAuction, numPlayersNeeded);

      // set first thread as the "reporter", so only one copy of info printed to cmd prompt
      aThread.start();
      if (firstThread) {
        aThread.setReporter(true);
        firstThread = false;
      }

      myThreads.add(aThread);

      if (myThreads.size() >= numPlayersNeeded) {
        started = true;
        System.out.println("Auction should be ready to go...");
      } else
        System.out.println(
            "need more players.  Thread-count = "
                + myThreads.size()
                + "  , idCount = "
                + myAuction.bidderIDs.size());
      // myAuction was passed as a shallow copy... so only one auction exists
    }

    System.out.println("\nAuction has started with " + myThreads.size() + " agents!\n");

    // The auction is running right now... can you feel it?

    // System.out.println("Press Enter to end host connections and close...");
    // br.readLine();

    // System.out.println("Auction is complete... closing connections");
    for (int i = 0; i < myThreads.size(); i++) myThreads.get(i).join();

    for (int i = 0; i < myThreads.size(); i++)
      if (myThreads.get(i).isAlive()) myThreads.get(i).closeConnection();

    serverSocket.close();
  } // end main()
Пример #2
0
 /** Transforme une addresse IP en un ID qui permet de l'identifier dans la hashmap. */
 private String toId(InetAddress addr) {
   String addname = addr.toString();
   return addname.split("/")[1];
 }
Пример #3
0
  private ArrayList<InetAddress> contatta_Tracker(Descrittore descr) {

    InetAddress server = null;
    ObjectInputStream data_in;
    ObjectOutputStream data_out;
    InputStream in;
    OutputStream out;
    ArrayList<InetAddress> lista = null;
    /* socket per la comunicazione TCP */
    SSLSocketFactory factory = null;
    SSLSocket socket = null;

    // ###### preparo l'indirizzo
    try {
      server = InetAddress.getByName("192.168.0.10");
    } catch (UnknownHostException e1) {
      e1.printStackTrace();
    }

    // ###### creo il socket

    if (descr == null) {
      System.out.println("## contatta_tracker di Download ha ricevuto parametro null ! ");
      return null;
    }

    System.out.println(
        "@ provo a contattare via TCP il Tracker a "
            + server.toString()
            + " porta "
            + descr.getTcp());

    try {
      factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
      socket = (SSLSocket) factory.createSocket(server, descr.getTcp());
    } catch (IOException e1) {
      System.out.println("# eccezione nella creazione socket verso il Tracker TCP");
      e1.printStackTrace();
    }

    System.out.println("@ socket creato ");

    if (factory == null || socket == null) {
      System.out.println("## la connessione ssl di Download ha ritornato null ! ");
      return null;
    }

    try {
      System.out.println("@ out_stream ");
      out = socket.getOutputStream();
      System.out.println("@ out ");
      data_out = new ObjectOutputStream(out);
      System.out.println("@ in_stream ");
      in = socket.getInputStream();
      System.out.println("@ in ");
      data_in = new ObjectInputStream(in);

      System.out.println("@ Downloader:  streams ok  ");

      data_out.writeObject(descr); // TODO Perchè ?
      data_out.flush();

      System.out.println("@ descrittore mandato  ");

      lista = (ArrayList<InetAddress>) data_in.readObject();
      if (lista == null)
        System.out.println(
            "@ ritornata lista vuota dal tracker riguardo :  "
                + descr.nome); // TODO sarebbe un caso plausibile
      else System.out.println("@ lista ricevuta :  " + lista.toString());

    } catch (IOException e) {
      System.out.println("# eccezione I/O nella creazione socket verso il Tracker TCP");
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      System.out.println("# eccezione C N F nella creazione socket verso il Tracker TCP");
      e.printStackTrace();
    }

    return lista;
  }