Exemplo n.º 1
0
 /** Model startuje i uruchamia wszystkie potrzebne moduły. */
 public void load() {
   if (self == null) {
     outQueue.add(new ProgramClosingEvent());
     throw new NullPointerException("Self contact nie może być null!");
   }
   server.start();
   multicast.start();
   sender.start();
 }
Exemplo n.º 2
0
 /** Zakończenie modelu i zwolnienie wszystkich zasobów. */
 public void close() {
   // inQueue.add(new ProgramClosingEvent());
   multicast.close();
   sender.interrupt();
   server.close();
   for (Connection con : connections.values()) {
     if (con.getCurrentConnection() != null) {
       con.getCurrentConnection().close();
     }
   }
   executor.shutdown();
   timer.shutdown();
 }
Exemplo n.º 3
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);
  }