Esempio n. 1
0
  public void Connect() throws IOException {
    try {
      InetAddress addr = InetAddress.getByName(server);
      Socket socket = new Socket(addr, port);

      in = new InputStreamReader(socket.getInputStream());
      out = new OutputStreamWriter(socket.getOutputStream());

      char c[] = new char[LINE_LENGTH];

      boolean authed = false;

      while (true) {
        if (in.read(c) > 0) {
          String msg = new String(c).trim();

          if (!authed) {
            if (msg.endsWith("No Ident response")) {

              System.out.println("...sending identity...");
              out.write(String.format("NICK %s\r\n", nickname));
              out.write(String.format("USER %s %s %s :s\r\n", nickname, host, server, realname));
              out.flush();

              authed = true;

              for (IRCAuthListener a : authListeners) {
                a.afterAuthentication(this);
              }
            }
          }

          if (msg.startsWith("PING")) {
            System.out.println("Sending pong...");
            out.write("PONG\r\n");
            out.flush();
          }

          for (IRCRawDataListener d : rawDataListeners) {
            d.processRawData(this, msg);
          }

          c = new char[LINE_LENGTH];
        } else {
          System.out.println("Oops, if you see this we just got kicked or connection timed out...");
          break;
        }
      }
    } catch (IOException ioe) {
      throw (ioe);
    }
  }
Esempio n. 2
0
  public static void main(String[] args) {
    try {
      BufferedReader br1 =
          new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8"));

      BufferedReader br2 =
          new BufferedReader(new InputStreamReader(new FileInputStream(args[1]), "UTF-8"));

      OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("merge.txt"), "UTF-8");

      String cell1 = br1.readLine();
      String cell2 = br2.readLine();
      while (cell1 != null && cell2 != null) {

        String merge = cell1 + "\t" + cell2;
        System.out.println(merge);
        out.write(merge);
        out.write("\n");
        out.flush();

        cell1 = br1.readLine();
        cell2 = br2.readLine();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 3
0
 private void sendMSG(String msg) throws IOException {
   try {
     out.write(msg);
     out.flush();
   } catch (IOException ioe) {
     throw (ioe);
   }
 }
Esempio n. 4
0
 private Object request(
     String url, boolean post, Hashtable params, boolean basicAuth, boolean processOutput)
     throws Exception {
   HttpConnection conn = null;
   Writer writer = null;
   InputStream is = null;
   try {
     if (!post && (params != null)) {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       OutputStreamWriter osw = new OutputStreamWriter(baos, this.encoding);
       this.encodeParams(params, osw);
       osw.flush();
       osw.close();
       osw = null;
       url += "?" + new String(baos.toByteArray(), this.encoding);
       baos = null;
     }
     conn = (HttpConnection) Connector.open(url);
     conn.setRequestMethod(post ? HttpConnection.POST : HttpConnection.GET);
     if (basicAuth) {
       if (!this.areCredentialsSet()) throw new Exception("Credentials are not set");
       String token = base64Encode((this.user + ":" + this.pass).getBytes(this.encoding));
       conn.setRequestProperty("Authorization", "Basic " + token);
     }
     if (post && (params != null)) {
       OutputStream os = conn.openOutputStream();
       writer = new OutputStreamWriter(os, this.encoding);
       this.encodeParams(params, writer);
       writer.flush();
       writer.close();
       os = null;
       writer = null;
     }
     int code = conn.getResponseCode();
     if ((code != 200) && (code != 302))
       throw new Exception("Unexpected response code " + code + ": " + conn.getResponseMessage());
     is = conn.openInputStream();
     if (processOutput) {
       synchronized (this.json) {
         return this.json.parse(is);
       }
     } else {
       this.pump(is, System.out, 1024);
       return null;
     }
   } finally {
     if (writer != null) writer.close();
     if (is != null) is.close();
     if (conn != null) conn.close();
   }
 }
Esempio n. 5
0
  public void end() throws Exception {
    FunMessage.Builder fm = FunMessage.newBuilder().setGameId(gameId).setFunScore(rate());
    URL url = new URL("http://localhost:5000/gameFinish");
    URLConnection urlCon = url.openConnection();
    urlCon.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(urlCon.getOutputStream());
    wr.write("rating=");
    wr.flush();
    fm.build().writeTo(urlCon.getOutputStream());
    urlCon.getOutputStream().close();

    // No network I/O happens until you ask for feedback apparently
    urlCon.getInputStream();
  }
Esempio n. 6
0
  protected void writeParseTree(String filename, AST ast) {
    try {
      PrintStream stream = new PrintStream(new FileOutputStream(filename));
      stream.println("<?xml version=\"1.0\"?>");
      stream.println("<document>");
      OutputStreamWriter writer = new OutputStreamWriter(stream);
      if (ast != null) {
        ((CommonAST) ast).xmlSerialize(writer);
      }
      writer.flush();
      stream.println("</document>");
      writer.close();
    } catch (IOException e) {

    }
  }
 /** Utility method to write a migrations file */
 public static void writeMigrationsFile(ConfigManagerMigrations m, File f) {
   OutputStreamWriter r = null;
   try {
     r = new OutputStreamWriter(new FileOutputStream(f));
     XStream x = createXStream();
     String config = x.toXML(m);
     r.write(config);
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (r != null) {
       try {
         r.flush();
         r.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
Esempio n. 8
0
  /**
   * Executes a command as {@code user}. Saves the stdout/stderr in the corresponding fields and
   * returns the return code of the child process.
   *
   * @param cmd The command to execute
   * @param data The data to send to the stdin of the process
   * @param timelimt How many seconds the command can run
   * @param memlimit How many megabytes the command can use
   */
  private int exec(String cmdS, String data, int timelimit, int memlimit) throws Exception {
    ArrayList<String> cmd = new ArrayList<String>(cmdPrefix);
    cmd.add("-m");
    cmd.add("" + memlimit);
    cmd.add("-c");
    cmd.add("" + timelimit);
    cmd.add("-w");
    cmd.add("" + (3 * timelimit + 1));
    cmd.add("-x");
    cmd.add(cmdS);
    String tmp = "";
    for (String cs : cmd) tmp += " \"" + cs + "\"";
    log.fine("exec " + tmp);
    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.directory(workDir);
    Process p = pb.start();
    OutputStreamWriter writer = new OutputStreamWriter(p.getOutputStream());
    StreamReader rOut = new StreamReader(p.getInputStream());
    StreamReader rErr = new StreamReader(p.getErrorStream());
    rOut.start();
    rErr.start();

    // TODO I think this may block for big tests. Check and Fix.
    // send and receive data "in parallel"
    writer.write(data);
    writer.flush();
    writer.close();
    rOut.join();
    rErr.join();
    stdout = rOut.result;
    stderr = rErr.result;
    if (rOut.exception != null) throw rOut.exception;
    if (rErr.exception != null) throw rErr.exception;

    // log.finer("out: " + stdout);
    // log.finer("err: " + stderr);
    // log.finer("done exec");
    return p.waitFor();
  }
Esempio n. 9
0
  public static void main(String[] args) {
    /** Define a host server */
    String host = "localhost";
    /** Define a port */
    int port = 19999;
    int port2 = 19990;
    // int port3 = 19980;
    StringBuffer instr = new StringBuffer();
    String TimeStamp;
    Parser parser = new Parser();
    System.out.println("SocketClient initialized");

    try {

      // parsing
      // DataStream ds =
      // new PlainTextByLineDataStream(
      // new FileReader(new File("input.txt")));
      BufferedReader inputReader = new BufferedReader(new FileReader("input.txt"));
      String sent;
      while ((sent = inputReader.readLine()) != null) {
        // String sentence = (String)ds.nextToken() + (char) 13;
        String sentence = sent + (char) 13;
        // System.out.println(str);
        System.out.println("Parsing....");

        Tree tree = parser.parse(sentence);
        System.out.println(tree);

        System.out.println("Extracting features...");
        String srlIdentifier = "python srl-identifier.py " + '"' + tree + '"';
        // System.out.println(srlIdentifier);
        Runtime rr = Runtime.getRuntime();
        Process pp = rr.exec(srlIdentifier);
        BufferedReader brr = new BufferedReader(new InputStreamReader(pp.getInputStream()));
        pp.waitFor();

        BufferedReader reader = new BufferedReader(new FileReader("identifier.test"));
        BufferedReader classifier = new BufferedReader(new FileReader("classifier.test"));
        String line;
        PrintWriter identifierOutput = new PrintWriter("identifier-output.txt");
        PrintWriter classifierOutput = new PrintWriter("classifier-output.txt");
        BufferedReader preds = new BufferedReader(new FileReader("pred.test"));

        while ((line = reader.readLine()) != null) {

          String pred = preds.readLine();
          String features = line + (char) 13;
          String classifierFeature = classifier.readLine() + (char) 13;

          InetAddress address = InetAddress.getByName(host);
          // Establish a socket connetion
          Socket connection = new Socket(address, port);
          Socket connection2 = new Socket(address, port2);

          // Instantiate a BufferedOutputStream object
          BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());

          // Instantiate an OutputStreamWriter object with the optional character
          // encoding.
          //

          OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

          BufferedReader fromServer =
              new BufferedReader(new InputStreamReader(connection.getInputStream()));

          // Write across the socket connection and flush the buffer

          osw.write(features);
          osw.flush();
          String identifierResponse = fromServer.readLine();
          identifierOutput.println(identifierResponse);

          BufferedOutputStream bos2 = new BufferedOutputStream(connection2.getOutputStream());

          // Instantiate an OutputStreamWriter object with the optional character
          // encoding.
          //
          OutputStreamWriter osw2 = new OutputStreamWriter(bos2, "US-ASCII");

          BufferedReader fromServer2 =
              new BufferedReader(new InputStreamReader(connection2.getInputStream()));

          osw2.write(classifierFeature);
          osw2.flush();
          String ClassifierResponse = fromServer2.readLine();
          classifierOutput.println(pred + ' ' + ClassifierResponse);
        }
        identifierOutput.close();
        classifierOutput.close();

        Runtime rlabeler = Runtime.getRuntime();
        String srlClassifier = "python concept-formulator.py";
        Process p = rlabeler.exec(srlClassifier);
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        p.waitFor();
        // System.out.println("here i am");
        String line2;
        while ((line2 = br.readLine()) != null) {
          System.out.println(line2);
          // while (br.ready())
          // System.out.println(br.readLine());
        }
      }

    } catch (Exception e) {
      String cause = e.getMessage();
      if (cause.equals("python: not found")) System.out.println("No python interpreter found.");
    }
  }
Esempio n. 10
0
 @Override
 public void write(OutputStream out) throws IOException {
   OutputStreamWriter writer = new OutputStreamWriter(out, UTF8);
   this.write(writer);
   writer.flush();
 }