Ejemplo n.º 1
0
  public boolean connect() {
    int failedAttempts = 0;

    socketSenders = new NetworkSocketStreamSender[numThreads];

    try {
      NetworkSocket socket = new NetworkSocket(host, port);

      for (int i = 0; i < numThreads; i++) {
        try {

          createSender(i, socket);
          numRunningThreads++;
        } catch (ConnectionException e) {
          failedAttempts++;
        }
      }

    } catch (ConnectionException e) {
      e.printStackTrace();
      failedAttempts = numThreads;
    }

    if ((failedAttempts == numThreads) && httpTunnel) {
      System.out.println(NAME + "Trying http tunneling");
      failedAttempts = 0;
      numRunningThreads = 0;
      if (tryHttpTunneling()) {
        tunneling = true;
        System.out.println(NAME + "Will use http tunneling");
        httpSenders = new NetworkHttpStreamSender[numThreads];
        for (int i = 0; i < numThreads; i++) {
          try {
            createHttpSender(i);
            numRunningThreads++;
          } catch (ConnectionException e) {
            failedAttempts++;
          }
        }
        return failedAttempts != numThreads;
      }
    } else {
      if (numRunningThreads != numThreads) {
        try {
          stop();
        } catch (ConnectionException e) {
          e.printStackTrace();
        }
        return false;
      } else {
        return true;
      }
    }
    System.out.println(NAME + "Http tunneling failed.");
    return false;
  }
Ejemplo n.º 2
0
  private static String senderBase(Method type, String path, String[]... args) {

    HTTPBaseClient client;
    switch (type) {
      case get:
        client = new HTTPGetClient(path);
        break;
      case post:
        client = new HTTPPostClient(path);
        break;
      default:
        client = null;
        break;
    }

    for (String[] pair : args) {
      client.addParameter(pair[0], pair[1]);
    }

    if (client != null) {
      try {
        return client.execute();
      } catch (ConnectionException e) {
        e.printStackTrace();
        return e.toString();
      }
    } else {
      return null;
    }
  }
    protected JSONObject doInBackground(String... args) {

      JSONObject json = new JSONObject();

      try {
        HttpUploader uploader =
            new HttpUploader(
                getActivity().getResources(), BuildConfig.DOMAIN + Constantes.PUBLISH_COMMENT);
        uploader.añadirArgumento(
            "idevento",
            event.getId()); // le pasamos el codigo del evento del cual estamos mostrando el detalle
        uploader.añadirArgumento("message", etCommentTxt.getText().toString().trim());
        uploader.añadirArgumento("devid", Utilidades.getDevId(getActivity()));
        uploader.añadirArgumento("timezoneoffset", Utilidades.getTimeZoneOffset(getActivity()));

        json = uploader.enviar();
      } catch (ConnectionException e) {
        e.printStackTrace();
        try {
          json.put(Constantes.JSON_MESSAGE, e.getMessage());
          json.put(Constantes.JSON_SUCCESS, 0);
        } catch (JSONException e1) {
          e1.printStackTrace();
        }
      }
      return json;
    }
    protected JSONObject doInBackground(String... args) {

      JSONObject json = new JSONObject();

      /////////////////////////////////////////////////////
      try {
        HttpUploader uploader =
            new HttpUploader(
                getActivity().getResources(), BuildConfig.DOMAIN + Constantes.LOAD_MESSAGES_FILE);
        uploader.añadirArgumento(
            "idevento",
            event.getId()); // le pasamos el codigo del evento del cual estamos mostrando el detalle
        uploader.añadirArgumento("thumb_width", Float.toString(Constantes.THUMB_SIZE));
        uploader.añadirArgumento("thumb_height", Float.toString(Constantes.THUMB_SIZE));
        uploader.añadirArgumento("timezoneoffset", Utilidades.getTimeZoneOffset(getActivity()));

        if ((args != null) && (args.length > 0)) {
          uploader.añadirArgumento("numerocomments", args[0]);
        }

        json = uploader.enviar();
      } catch (ConnectionException e) {
        e.printStackTrace();
        try {
          json.put(Constantes.JSON_MESSAGE, e.getMessage());
          json.put(Constantes.JSON_SUCCESS, 0);
        } catch (JSONException e1) {
          e1.printStackTrace();
        }
      }
      return json;
    }
Ejemplo n.º 5
0
 public void networkException(int id, ExitCode reason) {
   try {
     numRunningThreads--;
     if (tunneling) {
       // httpSenders[id].disconnect();
       System.out.println(NAME + "Failed to use http tunneling. Stopping.");
       stop();
       notifyNetworkConnectionListener(reason);
     } else {
       socketSenders[id].disconnect();
     }
     if (numRunningThreads < 1) {
       System.out.println(NAME + "No more sender threads. Stopping.");
       stop();
       notifyNetworkConnectionListener(reason);
     } else {
       System.out.println(
           NAME + "Sender thread stopped. " + numRunningThreads + " sender threads remaining.");
     }
   } catch (ConnectionException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     if (numRunningThreads < 1) {
       System.out.println(NAME + "No more sender threads. Stopping.");
       notifyNetworkConnectionListener(reason);
     } else {
       System.out.println(
           NAME + "Sender thread stopped. " + numRunningThreads + " sender threads remaining.");
     }
   }
 }
Ejemplo n.º 6
0
 public void start() {
   System.out.println(NAME + "Starting network sender.");
   if (tunneling) {
     for (int i = 0; i < numRunningThreads; i++) {
       httpSenders[i].sendStartStreamMessage();
       executor.execute(httpSenders[i]);
     }
   } else {
     for (int i = 0; i < numRunningThreads; i++) {
       try {
         socketSenders[i].sendStartStreamMessage();
         executor.execute(socketSenders[i]);
       } catch (ConnectionException e) {
         e.printStackTrace();
       }
     }
   }
   stopped = false;
 }
    protected JSONObject doInBackground(String... args) {

      JSONObject json = new JSONObject();

      try {
        HttpUploader uploader =
            new HttpUploader(
                getActivity().getResources(), BuildConfig.DOMAIN + Constantes.GET_IMAGE_FILE);
        uploader.añadirArgumento(
            "image", args[0]); // args[0] is the name of the image we're trying to get
        json = uploader.enviar();
      } catch (ConnectionException e) {
        e.printStackTrace();
        try {
          json.put(Constantes.JSON_MESSAGE, e.getMessage());
          json.put(Constantes.JSON_SUCCESS, 0);
        } catch (JSONException e1) {
          e1.printStackTrace();
        }
      }
      return json;
    }
    protected JSONObject doInBackground(String... args) {

      JSONObject json = new JSONObject();

      try {
        HttpUploader uploader =
            new HttpUploader(
                getActivity().getResources(), BuildConfig.DOMAIN + Constantes.LEER_EVENTOS_FILE);
        uploader.añadirArgumento(
            "_id",
            args[0]); // le pasamos el codigo del evento del cual estamos mostrando el detalle
        uploader.añadirArgumento("timezoneoffset", Utilidades.getTimeZoneOffset(getActivity()));
        json = uploader.enviar();
      } catch (ConnectionException e) {
        e.printStackTrace();
        try {
          json.put(Constantes.JSON_MESSAGE, e.getMessage());
          json.put(Constantes.JSON_SUCCESS, 0);
        } catch (JSONException e1) {
          e1.printStackTrace();
        }
      }
      return json;
    }