Beispiel #1
0
  public static void main(String[] args) throws IOException {
    Server server = new Server(PORT);
    Socket client = server.listen();

    ChatDialog chat = new ChatDialog(client);
    chat.pack();
    SwingUtilities.invokeLater(() -> chat.setVisible(true));
    chat.start();
    System.exit(0);
  }
Beispiel #2
0
  public void windowClosing(WindowEvent e) {
    if (server != null) {
      try {
        server.stop();
      } catch (Exception eClose) {
      }
      server = null;
    }

    dispose();
    System.exit(0);
  }
 /*
  * If the user click the X button to close the application
  * I need to close the connection with the server to free the port
  */
 public void windowClosing(WindowEvent e) {
   // if my Server exist
   if (server != null) {
     try {
       server.stop(); // ask the server to close the conection
     } catch (Exception eClose) {
     }
     server = null;
   }
   // dispose the frame
   dispose();
   System.exit(0);
 }
  public void nick_name(String msg) {
    try {
      String name = msg.substring(13);
      this.setName(name);
      Vector v = father.onlineList;
      boolean isRepeatedName = false;
      int size = v.size();
      for (int i = 0; i < size; i++) {
        ServerAgentThread tempSat = (ServerAgentThread) v.get(i);
        if (tempSat.getName().equals(name)) {
          isRepeatedName = true;
          break;
        }
      }
      if (isRepeatedName == true) {
        dout.writeUTF("<#NAME_REPEATED#>");
        din.close();
        dout.close();
        sc.close();
        flag = false;
      } else {
        v.add(this);
        father.refreshList();
        String nickListMsg = "";
        StringBuilder nickListMsgSb = new StringBuilder();
        size = v.size();
        for (int i = 0; i < size; i++) {
          ServerAgentThread tempSat = (ServerAgentThread) v.get(i);
          nickListMsgSb.append("!");
          nickListMsgSb.append(tempSat.getName());
        }
        nickListMsgSb.append("<#NICK_LIST#>");
        nickListMsg = nickListMsgSb.toString();
        Vector tempv = father.onlineList;
        size = tempv.size();
        for (int i = 0; i < size; i++) {
          ServerAgentThread tempSat = (ServerAgentThread) tempv.get(i);
          tempSat.dout.writeUTF(nickListMsg);
          if (tempSat != this) {
            tempSat.dout.writeUTF("<#MSG#>" + this.getName() + "is now online....");
          }
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public void actionPerformed(ActionEvent e) {

    // if running we have to stop

    if (server != null) {

      server.stop();

      server = null;

      tPortNumber.setEditable(true);

      stopStart.setText("Start");

      return;
    }

    // OK start the server

    int port;

    try {

      port = Integer.parseInt(tPortNumber.getText().trim());

    } catch (Exception er) {

      appendEvent("Invalid port number");

      return;
    }

    // ceate a new Server

    server = new Server(port, this);

    // and start it as a thread

    new ServerRunning().start();

    stopStart.setText("Stop");

    tPortNumber.setEditable(false);
  }
Beispiel #6
0
  public void actionPerformed(ActionEvent e) {
    if (server != null) {
      server.stop();
      server = null;
      tPortNumber.setEditable(true);
      stopStart.setText("Start");
      return;
    }

    int port;
    try {
      port = Integer.parseInt(tPortNumber.getText().trim());
    } catch (Exception er) {
      appendEvent("Invalid port number.");
      return;
    }

    server = new Server(port, this);
    new ServerRunning().start();
    stopStart.setText("Stop");
    tPortNumber.setEditable(false);
  }
  public void client_leave(String msg) {
    try {
      Vector tempv = father.onlineList;
      tempv.remove(this);
      int size = tempv.size();
      String nl = "<#NICK_LIST#>";
      for (int i = 0; i < size; i++) {
        ServerAgentThread tempSat = (ServerAgentThread) tempv.get(i);
        tempSat.dout.writeUTF("<#MSG#>" + this.getName() + "is offline....");
        nl = nl + "|" + tempSat.getName();
      }

      for (int i = 0; i < size; i++) {
        ServerAgentThread tempSat = (ServerAgentThread) tempv.get(i);
        tempSat.dout.writeUTF(nl);
      }
      this.flag = false;
      father.refreshList();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Beispiel #8
0
        public void actionPerformed(ActionEvent e) {
          String NameItem = (((JMenuItem) e.getSource()).getText());
          if (NameItem == "Load File") {
            // all files disabled
            fc.setAcceptAllFileFilterUsed(false);

            // only XML files
            FileNameExtensionFilter xmlfilter =
                new FileNameExtensionFilter("xml files (*.xml)", "xml");
            fc.setFileFilter(xmlfilter);

            // Set Directory!!
            fc.setCurrentDirectory(new java.io.File("data"));

            // Open XML
            fc.setDialogTitle("Open XML");
            int returnVal = fc.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
              File file = fc.getSelectedFile();
              String FileLocation = file.getPath();
              textArea.setText("");
              // textArea.setText(FileLocation + "\n" + "\n");

              // Parse XML
              xmlParser parser = new xmlParser();
              final ContainerSetXml containers;

              try {
                long time = System.nanoTime();
                containers = parser.parse(FileLocation);

                System.out.println(
                    "It took" + (System.nanoTime() - time) + "ns to parse the xml file");
                // new Thread for display next container after some time
                Thread t =
                    new Thread() {
                      public void run() {
                        for (ContainerXml c : containers.containers) {
                          DateFormat dateFormat = new SimpleDateFormat(" HH:mm:ss");
                          Calendar now = Calendar.getInstance();
                          String Time = "[" + dateFormat.format(now.getTime()) + "]";

                          textArea.append(Time + " " + c.id + " " + c.ownerName + "\n");
                          textArea.setCaretPosition(textArea.getDocument().getLength());
                          try {
                            sleep(150); // milliseconds
                          } catch (InterruptedException ex) {
                          }
                        }
                      }
                    };
                t.start(); // call back run()

              } catch (Exception ex) {
                System.out.println(ex);
              }
            }

          } else if (NameItem == "Start server") {
            // server.start() launches server.run() in a new thread
            // Meaning server.start won't freeze the gui anymore
            server.start(6666);
          } else if (NameItem == "Login to ftp") {
            FtpLoginView ftpLoginView = new FtpLoginView(server);
            ftpLoginView.display();
            String name = ftpLoginView.name;
            String password = ftpLoginView.name;

            // server.login() is called in ftpLoginView
          } else if (NameItem == "About") {
            JOptionPane.showMessageDialog(
                null,
                "Mede mogelijk gemaakt door Groep 5!",
                "About",
                JOptionPane.INFORMATION_MESSAGE);
          } else if (NameItem == "Help") {
            JOptionPane.showMessageDialog(
                null, "Moet nog ingevuld worden!", "Help", JOptionPane.INFORMATION_MESSAGE);
          } else if (NameItem == "Quit") {
            System.exit(0);
          } else if (NameItem == "Restart server") {
            server.restart(6666);
          } else if (NameItem == "Stop server") {
            server.stop();
          }
        }
  // ------------------------------------
  // main
  // ------------------------------------
  public static void main(String argv[]) throws Exception {
    // create a Server object
    Server theServer = new Server();

    // show GUI:
    theServer.pack();
    theServer.setVisible(true);

    // get RTSP socket port from the command line
    int RTSPport = Integer.parseInt(argv[0]);

    // Initiate TCP connection with the client for the RTSP session
    ServerSocket listenSocket = new ServerSocket(RTSPport);
    theServer.RTSPsocket = listenSocket.accept();
    listenSocket.close();

    // Get Client IP address
    theServer.ClientIPAddr = theServer.RTSPsocket.getInetAddress();

    // Initiate RTSPstate
    state = INIT;

    // Set input and output stream filters:
    RTSPBufferedReader =
        new BufferedReader(new InputStreamReader(theServer.RTSPsocket.getInputStream()));
    RTSPBufferedWriter =
        new BufferedWriter(new OutputStreamWriter(theServer.RTSPsocket.getOutputStream()));

    // Wait for the SETUP message from the client
    int request_type;
    boolean done = false;
    while (!done) {
      request_type = theServer.parse_RTSP_request(); // blocking

      if (request_type == SETUP) {
        done = true;

        // update RTSP state
        state = READY;
        System.out.println("New RTSP state: READY");

        // Send response
        theServer.send_RTSP_response();

        // init the VideoStream object:
        theServer.video = new VideoStream(VideoFileName);

        // init RTP socket
        theServer.RTPsocket = new DatagramSocket();
      }
    }

    // loop to handle RTSP requests
    while (true) {
      // parse the request
      request_type = theServer.parse_RTSP_request(); // blocking

      if ((request_type == PLAY) && (state == READY)) {
        // send back response
        theServer.send_RTSP_response();
        // start timer
        theServer.timer.start();
        // update state
        state = PLAYING;
        System.out.println("New RTSP state: PLAYING");
      } else if ((request_type == PAUSE) && (state == PLAYING)) {
        // send back response
        theServer.send_RTSP_response();
        // stop timer
        theServer.timer.stop();
        // update state
        state = READY;
        System.out.println("New RTSP state: READY");
      } else if (request_type == TEARDOWN) {
        // send back response
        theServer.send_RTSP_response();
        // stop timer
        theServer.timer.stop();
        // close sockets
        theServer.RTSPsocket.close();
        theServer.RTPsocket.close();

        System.exit(0);
      }
    }
  }