예제 #1
0
  public synchronized Object __rpccall__(String endpoint, Object funcobj, boolean usecache) {
    try {
      // normal case is passing a functioncall, batch case is passing a List<FunctionCall>
      FunctionCall func = (FunctionCall) funcobj;
      if (usecache && has(endpoint, func)) return get(endpoint, func);
      if (!can_connect)
        if (has(endpoint, func)) {
          return get(endpoint, func);
        } else {
          return null;
        }
      func.params = __marshall_args__(func.params);
      funcobj = func;
    } catch (ClassCastException ex) {
      try {
        BatchClient b = (BatchClient) funcobj;
        List<FunctionCall> newcalls = new Vector<FunctionCall>();
        for (int i = 0; i < b.batch.size(); i++) {
          b.batch.get(i).params = __marshall_args__(b.batch.get(i).params);
        }
        for (FunctionCall f : b.batch) {
          if (!b.called.contains(f)) {
            newcalls.add(f);
          }
        }
        funcobj = newcalls;
      } catch (ClassCastException ex2) {

      }
    }
    HttpPost post = new HttpPost(endpoint);
    StringEntity req = null;
    req = new StringEntity(gson.toJson(funcobj), "UTF-8");
    post.setEntity(req);
    post.setHeader(HTTP.CONTENT_TYPE, "application/json");
    HttpResponse resp = null;
    HttpClient client = Http.client();
    try {
      resp = client.execute(post, Http.context());
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return null;
    }
    Object ret = null;
    HttpEntity rep = resp.getEntity();
    InputStreamReader is = null;
    try {
      is = new InputStreamReader(rep.getContent());
      BufferedReader rd = new BufferedReader(is);
      String out = "";
      String line = null;
      while ((line = rd.readLine()) != null) {
        out += line;
      }
      resp.getEntity().consumeContent();
      ret = __parse_response__(out);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      throw new RuntimeException();
    } finally {
      if (is != null)
        try {
          is.close();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
    }
    FunctionCall func = (FunctionCall) funcobj;
    if (usecache) set(endpoint, func, ret);
    return ret;
  }