Ejemplo n.º 1
0
 private static void run(Pipeline p, String input, String expected) throws Exception {
   Iterable<String> mat =
       p.read(From.textFile(input))
           .parallelDo(
               "conf",
               CONFIG_FN,
               Writables.strings(),
               ParallelDoOptions.builder().conf(KEY, expected).build())
           .materialize();
   for (String v : mat) {
     if (!expected.equals(v)) {
       Assert.fail("Unexpected value: " + v);
     }
   }
   p.done();
 }
Ejemplo n.º 2
0
  /**
   * This is input reading thread for the pipelined parser. You feed it input through the input
   * stream (see the constructor) and it calls back an event listener interface for message
   * processing or error. It cleans up the input - dealing with things like line continuation
   */
  public void run() {

    Pipeline inputStream = this.rawInputStream;
    // inputStream = new MyFilterInputStream(this.rawInputStream);
    // I cannot use buffered reader here because we may need to switch
    // encodings to read the message body.
    try {
      while (true) {
        this.sizeCounter = this.maxMessageSize;
        // this.messageSize = 0;
        StringBuffer inputBuffer = new StringBuffer();

        if (Debug.parserDebug) Debug.println("Starting parse!");

        String line1;
        String line2 = null;

        while (true) {
          try {
            line1 = readLine(inputStream);
            // ignore blank lines.
            if (line1.equals("\n")) {
              if (Debug.parserDebug) Debug.println("Discarding " + line1);
              continue;
            } else break;
          } catch (IOException ex) {
            Debug.printStackTrace(ex);
            this.rawInputStream.stopTimer();
            return;
          }
        }

        inputBuffer.append(line1);
        // Guard against bad guys.
        this.rawInputStream.startTimer();

        while (true) {
          try {
            line2 = readLine(inputStream);
            inputBuffer.append(line2);
            if (line2.trim().equals("")) break;
          } catch (IOException ex) {
            this.rawInputStream.stopTimer();
            Debug.printStackTrace(ex);
            return;
          }
        }

        // Stop the timer that will kill the read.
        this.rawInputStream.stopTimer();
        inputBuffer.append(line2);
        StringMsgParser smp = new StringMsgParser(sipMessageListener);
        smp.readBody = false;
        SIPMessage sipMessage = null;

        try {
          sipMessage = smp.parseSIPMessage(inputBuffer.toString());
          if (sipMessage == null) {
            this.rawInputStream.stopTimer();
            continue;
          }
        } catch (ParseException ex) {
          // Just ignore the parse exception.
          continue;
        }

        if (Debug.parserDebug) Debug.println("Completed parsing message");
        ContentLength cl = (ContentLength) sipMessage.getContentLength();
        int contentLength = 0;
        if (cl != null) {
          contentLength = cl.getContentLength();
        } else {
          contentLength = 0;
        }

        if (Debug.parserDebug) {
          Debug.println("contentLength " + contentLength);
          Debug.println("sizeCounter " + this.sizeCounter);
          Debug.println("maxMessageSize " + this.maxMessageSize);
        }

        if (contentLength == 0) {
          sipMessage.removeContent();
        } else if (maxMessageSize == 0 || contentLength < this.sizeCounter) {
          byte[] message_body = new byte[contentLength];
          int nread = 0;
          while (nread < contentLength) {
            // Start my starvation timer.
            // This ensures that the other end
            // writes at least some data in
            // or we will close the pipe from
            // him. This prevents DOS attack
            // that takes up all our connections.
            this.rawInputStream.startTimer();
            try {

              int readlength = inputStream.read(message_body, nread, contentLength - nread);
              if (readlength > 0) {
                nread += readlength;
              } else {
                break;
              }
            } catch (IOException ex) {
              ex.printStackTrace();
              break;
            } finally {
              // Stop my starvation timer.
              this.rawInputStream.stopTimer();
            }
          }
          sipMessage.setMessageContent(message_body);
        }
        // Content length too large - process the message and
        // return error from there.
        if (sipMessageListener != null) {
          try {
            sipMessageListener.processMessage(sipMessage);
          } catch (Exception ex) {
            // fatal error in processing - close the
            // connection.
            break;
          }
        }
      }
    } finally {
      try {
        inputStream.close();
      } catch (IOException e) {
        InternalErrorHandler.handleException(e);
      }
    }
  }