Exemple #1
0
  /**
   * Inicjuje moduł, przypisuje do potrzebnych socketów i przygotowywuje do pracy.
   *
   * @param self Kontakt jaki się zalogował.
   * @param picked Interface, który został wybrany.
   * @return Kontakty, które załadował model.
   * @throws FatalModelError Jeżeli nastąpił nie naprawialny błąd w modelu.
   */
  public Collection<Contact> init(final Contact self, final String picked) throws FatalModelError {
    if (self == null) {
      throw new NullPointerException("Self contact nie może być null!");
    }
    this.self = self;
    try {
      if (picked != null && displayedInterfaces != null && displayedInterfaces.contains(picked)) {
        int index = displayedInterfaces.indexOf(picked);
        displayedInterfaces = null; // mark for GC
        System.out.println("Picked " + candidates.get(index));
        this.selfConnection = new Connection(self.getId(), candidates.get(index), 0, null);
      } else {
        this.selfConnection = new Connection(self.getId(), InetAddress.getLocalHost(), 0, null);
      }
      candidates = null; // mark for GC
      server =
          new Server(executor, self.getId(), selfConnection, connections, timer, outQueue, inQueue);
      server.bind();
      sender =
          new Sender(
              executor,
              self.getId(),
              selfConnection,
              contacts,
              connections,
              timer,
              outQueue,
              inQueue);
      multicast =
          new MulticastHandler(self.getId(), selfConnection, connections, timer, outQueue, inQueue);
      multicast.bind();

      loadContacts();

    } catch (UnknownHostException | MulticastBindException | ServerBindException e) {
      e.printStackTrace();
      throw new FatalModelError(e);
    }
    return Collections.unmodifiableSet(contacts);
  }