public void run() {
      try {
        // Wait for a connection.
        SocketConnection sc = (SocketConnection) scn.acceptAndOpen();

        InputStream is = sc.openInputStream();
        OutputStream os = sc.openOutputStream();

        int ch = 0;
        int count = 0;
        while (ch != -1) {
          ch = is.read();
          if (ch == -1) {
            break;
          }
          os.write(ch);
          os.flush();
          count++;
        }
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        finished = true;
      }
    }
  /** Modified by Gabriele Bianchi 04/01/2006 */
  public void run() {
    StringBuffer connectorStringBuffer;
    if (Datas.isSSL) {
      connectorStringBuffer = new StringBuffer("ssl://");
    } else {
      connectorStringBuffer = new StringBuffer("socket://");
    }
    connectorStringBuffer.append(_hostname);
    connectorStringBuffer.append(":");
    connectorStringBuffer.append(_port);
    connectorStringBuffer.append("");

    String connectorString = connectorStringBuffer.toString().trim();
    System.out.println(connectorString);
    try {
      if (Datas.isSSL) {
        connection = (SecureConnection) Connector.open(connectorString);
      } else {
        connection = (StreamConnection) /*Connector.open*/ getConnectionForRequest(connectorString);
      }
      if (connection != null) {
        SocketConnection sc = (SocketConnection) connection;
        sc.setSocketOption(SocketConnection.KEEPALIVE, 2);
      }

      _cm.notifyConnect(connection, this.openInputStream(), this.openOutputStream());
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Connessione non riuscita:" + e.getMessage());
      _cm.notifyNoConnectionOn("I can't connect, server is unreachable");
      DebugStorage.getInstance().Log(0, "Can't connect, server is unreachable", e);
    }

    return;
  }
  /**
   * This will parse out the command. Lookup the appropriate Handler and pass the information to the
   * handler for processing.
   *
   * <p>Dev Note: the command parsing is processed here so that it is preformed by the assigned
   * thread rather than the listeners thread.
   */
  public void run() {
    try {
      logger.trace("start listening for cluster messages");
      SocketConnection sc = new SocketConnection(clientSocket);
      while (true) {
        if (owner.process(sc)) {
          // got the offline message or timeout
          break;
        }
      }
      logger.trace("disconnecting: {}", hostPort);
      sc.disconnect();

    } catch (Exception e) {
      logger.error("Error listening for messages - " + owner.getHostPort(), e);
    }
  }
  private void runLoopbackTest(String url) throws IOException {
    System.out.println("Connecting to " + url);
    SocketConnection sc = (SocketConnection) Connector.open(url);

    try {
      sc.setSocketOption(SocketConnection.LINGER, 5);

      InputStream is = sc.openInputStream();
      OutputStream os = sc.openOutputStream();

      String testData = "OK\r\n";

      os.write(testData.getBytes());
      os.flush();

      StringBuffer buf = new StringBuffer();

      int ch = 0;
      int count = 0;
      while (ch != -1) {
        ch = is.read();
        buf.append((char) ch);
        count++;
        if (count >= testData.length()) {
          break;
        }
      }

      assertEquals("Data received", buf.toString(), testData);

      is.close();
      os.close();
    } finally {
      sc.close();
    }
  }
Exemple #5
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            // test notification
            Notifications not = new Notifications();
            synchronized (not) {
              not.notification(getBaseContext());
            }
          }
        });
    Config.context = this;

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle =
        new ActionBarDrawerToggle(
            this,
            drawer,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    phone();

    Contacts c = new Contacts();
    c.contacts();

    SocketConnection sc = new SocketConnection();
    sc.setAction("get_messages");
    sc.execute();
    SocketConnection sc2 = new SocketConnection();
    sc2.setAction("get_new_messages");
    sc2.execute();
  }
 public void addListener(@NotNull SocketConnectionListener listener) {
   myConnection.addListener(listener, null);
 }
 public void registerHandler(AbstractResponseHandler<HXCPPResponse> debugProcess) {
   myConnection.registerHandler(HXCPPResponse.class, debugProcess);
 }
 public void open() throws IOException {
   myConnection.open();
 }
 public void close() {
   myConnection.close();
 }
 public synchronized void sendCommand(
     @Nullable AbstractResponseToRequestHandler<HXCPPResponse> responseHandler, String... args) {
   final int id = responseHandler == null ? -1 : lastCommandId++;
   myConnection.sendRequest(new HXCPPCommand(id, args), responseHandler);
 }