Beispiel #1
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;
  }