Ejemplo n.º 1
0
  private boolean updateClient(Client c) {
    if (c.isClosed()) {
      listener.onClientClose(this, c, null);

      return false;
    }

    try {
      c.read();
      c.update();

      listener.onClientUpdate(this, c);

      if (c.isReadyToSend()) {
        c.send();
      }

      return true;
    } catch (Exception e) {
      listener.onClientClose(this, c, e);

      c.close();

      return false;
    }
  }
Ejemplo n.º 2
0
  public static void main(String[] args) throws Exception {
    BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));

    Client c = new BaseClient();
    int connected = c.connect(Constants.HOSTNAME, Constants.PORT);

    if (connected != 0) {
      exitApp();
    }

    String msg;
    do {
      c.doLogin();
    } while (c.isConnected() && !c.isLoggedIn());

    while (c.isConnected()) {
      msg = inReader.readLine();
      if (!StringUtils.isBlank(msg)) {
        if (Utils.matchesClinetCommonCommandPattern(msg)) {
          // сообщение содержит комманду
          c.sendRawText(msg);
        } else {
          c.send(msg);
        }
      }
    }
    c.stop();
    exitApp();
  }
Ejemplo n.º 3
0
    public void run() {
      try {
        while (bConnected) {
          String str = dis.readUTF();
          Log.d("yinhang", "Client get String" + str);
          CommunicationAction action = CommunicationAction.jsonToObject(new JSONObject(str));
          // CommunicationAction action = (CommunicationAction)
          // ObjectStreamUtil.stringToObject(str);
          // handMessage(str,HomeActivity.TYPE_CONNECT_TAG);
          // if(action.getTag() == )
          handMessage(action);

          Log.d("yinhang", "Client get String2" + str);
          for (int i = 0; i < clients.size(); i++) {
            Client c = clients.get(i);
            c.send(str);
            // System.out.println(" a string send !");
          }
          /*
           * for(Iterator<Client> it = clients.iterator();
           * it.hasNext(); ) { Client c = it.next(); c.send(str); }
           */
          /*
           * Iterator<Client> it = clients.iterator();
           * while(it.hasNext()) { Client c = it.next(); c.send(str);
           * }
           */
        }
      } catch (EOFException e) {
        System.out.println("Client closed!");
        Log.d("yinhang", "Server Exception" + e);
      } catch (IOException e) {
        e.printStackTrace();
        Log.d("yinhang", "Server Exception2" + e);
      } catch (Exception e) {
        e.printStackTrace();
        Log.d("yinhang", "Server Exception3" + e);
      } finally {
        try {
          if (dis != null) dis.close();
          if (dos != null) dos.close();
          if (s != null) {
            s.close();
            // s = null;
          }

        } catch (IOException e1) {
          e1.printStackTrace();
        }
      }
    }
Ejemplo n.º 4
0
  ///
  /// Lit une requete depuis le Terminal, envoie cette requete au serveur,
  /// recupere sa reponse et l'affiche sur le Terminal.
  /// Noter que le programme bloque si le serveur ne repond pas.
  ///
  public static void main(String argv[]) {
    String host = DEFAULT_HOST;
    int port = DEFAULT_PORT;
    if (argv.length >= 1) host = argv[0];
    if (argv.length >= 2) port = Integer.parseInt(argv[1]);

    Client client = null;

    try {
      client = new Client(host, port);
    } catch (Exception e) {
      System.err.println("Client: Couldn't connect to " + host + ":" + port);
      System.exit(1);
    }

    System.out.println("Client connected to " + host + ":" + port);

    // pour lire depuis la console
    BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));

    while (true) {
      System.out.print(""); // ne marche pas sans.
      /* try {
          String request = cin.readLine();
          String response = client.send(request);
          System.out.println("Response: " + response);
        }
        catch (java.io.IOException e) {
          System.err.println("Client: IO error");
          return;
      }*/
      // try {
      String request = "";
      String response = "";
      if (client.windows.getCommand() != "") {
        request = client.windows.getCommand();
        client.windows.setCommand("");
        response = client.send(request);
        client.windows.setTextArea(response);
      }
      //   }
      // catch (java.io.IOException e) {
      //  System.err.println("Client: IO error");
      //  return;
      // }
    }
  }
Ejemplo n.º 5
0
 @Test
 public void testSend() throws Exception {
   final Request request = new Request().setUri("http://example.com").setMethod("GET");
   final Response response = client.send(request).get();
   assertThat(response.getStatus()).isEqualTo(Status.OK);
 }