Esempio n. 1
0
  /** Stops unicast and multicast receiver threads */
  void stopThreads() {
    Thread tmp;

    // 1. Stop the multicast receiver thread
    if (mcast_receiver != null) {
      if (mcast_receiver.isAlive()) {
        tmp = mcast_receiver;
        mcast_receiver = null;
        closeMulticastSocket(); // will cause the multicast thread to terminate
        tmp.interrupt();
        try {
          tmp.join(100);
        } catch (Exception e) {
        }
        tmp = null;
      }
      mcast_receiver = null;
    }

    // 2. Stop the unicast receiver thread
    if (ucast_receiver != null) {
      ucast_receiver.stop();
      ucast_receiver = null;
    }

    // 3. Stop the in_packet_handler thread
    if (incoming_packet_handler != null) {
      incoming_packet_handler.stop();
    }
  }
Esempio n. 2
0
  private void startListening() {
    // if listener thread is alive and listening now, no need to recreate. Just reuse it.
    if (listenerThread != null && listenerThread.isAlive()) {
      return;
    }

    Runnable listeningAction =
        () -> {
          boolean running = true;
          while (connected && running) {
            List<String> list = getMessages();

            localHistory.addAll(list);

            try {
              Thread.sleep(POLLING_PERIOD_MILLIS);
            } catch (InterruptedException e) {
              logger.error("The message listening thread was interrupted", e);
              running = false;
            }
          }
        };
    listenerThread = new Thread(listeningAction);
    listenerThread.start();
  }
Esempio n. 3
0
 private void startReceiver() {
   if (receiver == null || !receiver.isAlive()) {
     receiver = new Thread(Util.getGlobalThreadGroup(), this, "ReceiverThread");
     receiver.setDaemon(true);
     receiver.start();
     if (log.isTraceEnabled()) log.trace("receiver thread started");
   }
 }
Esempio n. 4
0
 public void start() {
   if (streamReader != null && streamReader.isAlive()) {
     System.out.println("Camera already started");
     return;
   }
   streamReader = new Thread(this, "HTTP Stream reader");
   keepAlive = true;
   streamReader.start();
 }
Esempio n. 5
0
 public synchronized void handle(Socket socket) throws IOException {
   con = new Connection(socket);
   count = 0;
   if (thread == null || !thread.isAlive()) {
     thread = new Thread(runners, this);
     thread.start();
   } else {
     notify();
   }
 }
Esempio n. 6
0
 public void stop() {
   Thread tmp;
   if (thread != null && thread.isAlive()) {
     running = false;
     tmp = thread;
     thread = null;
     closeSocket(); // this will cause the thread to break out of its loop
     tmp.interrupt();
     tmp = null;
   }
   thread = null;
 }
Esempio n. 7
0
 public void stop() {
   if (streamReader == null || !streamReader.isAlive()) {
     System.out.println("Camera already stopped");
     return;
   }
   keepAlive = false;
   try {
     streamReader.join();
   } catch (InterruptedException e) {
     System.err.println(e.getMessage());
   }
 }
Esempio n. 8
0
  public static void startChat(Socket socket) {

    InputStream input;
    DataInputStream dis = null;
    OutputStream output;
    DataOutputStream dos = null;

    try {
      input = socket.getInputStream();
      dis = new DataInputStream(input);
      output = socket.getOutputStream();
      dos = new DataOutputStream(output);

      dos.writeInt(3); // send 5 as request to server to request for chat
    } catch (IOException e) {
      System.out.println("Cannot send requests to server.");
    }

    System.out.println("Enter your message and hit enter to send.\n");

    connectedToChat = true;

    Thread readThread = new Thread(new Read(dis));
    Thread writeThread = new Thread(new Write(dos));
    readThread.start();
    writeThread.start();

    while ((writeThread.isAlive()) && (readThread.isAlive())) {}

    System.out.println("Disconnected from chat. Enter anything to return to menu.");

    if (!writeThread.isAlive()) {
      Scanner scanner = new Scanner(System.in);
      scanner.next();
    }

    while (writeThread.isAlive()) {}
  }
Esempio n. 9
0
  private void startMessageSending() {
    // if listener thread is alive and listening now, no need to recreate. Just reuse it.
    if (messageSendingThread != null && messageSendingThread.isAlive()) {
      return;
    }

    Runnable messageSendingAction =
        () -> {
          Scanner scanner = new Scanner(System.in);
          while (connected) {
            String message = scanner.nextLine();
            sendMessage(message);
          }
        };
    messageSendingThread = new Thread(messageSendingAction);
    messageSendingThread.start();
  }
Esempio n. 10
0
  /** Starts the unicast and multicast receiver threads */
  void startThreads() throws Exception {
    if (ucast_receiver == null) {
      // start the listener thread of the ucast_recv_sock
      ucast_receiver = new UcastReceiver();
      ucast_receiver.start();
      if (Trace.trace) {
        Trace.info("UDP.startThreads()", "created unicast receiver thread");
      }
    }

    if (ip_mcast) {
      if (mcast_receiver != null) {
        if (mcast_receiver.isAlive()) {
          if (Trace.trace) {
            Trace.info(
                "UDP.createThreads()",
                "did not create new multicastreceiver thread as existing "
                    + "multicast receiver thread is still running");
          }
        } else {
          mcast_receiver = null; // will be created just below...
        }
      }

      if (mcast_receiver == null) {
        mcast_receiver = new Thread(this, "UDP mcast receiver");
        mcast_receiver.setPriority(Thread.MAX_PRIORITY); // needed ????
        mcast_receiver.setDaemon(true);
        mcast_receiver.start();
      }
    }
    if (use_outgoing_packet_handler) {
      outgoing_packet_handler.start();
    }
    if (use_incoming_packet_handler) {
      incoming_packet_handler.start();
    }
  }
Esempio n. 11
0
 /**
  * Returns true if the server is ready to accept new clients.
  *
  * @return true if the server is listening.
  */
 public final boolean isListening() {
   return connectionListener != null && connectionListener.isAlive(); // modified in version 2.31
 }
Esempio n. 12
0
 public boolean isAlive() {
   return streamReader.isAlive();
 }