StringMessageSender.java revision 0482f188546780be2e9fbb4607765fcaaecb898b
1ed626ea7464c25db7ea754177f36b344195da158Cédric Beustpackage org.testng.remote.strprotocol;
2ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
3ed626ea7464c25db7ea754177f36b344195da158Cédric Beustimport org.testng.remote.RemoteTestNG;
4ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beustimport java.io.BufferedReader;
6754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beustimport java.io.BufferedWriter;
715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beustimport java.io.IOException;
815f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beustimport java.io.InputStreamReader;
9754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beustimport java.io.OutputStreamWriter;
10ed626ea7464c25db7ea754177f36b344195da158Cédric Beustimport java.io.PrintWriter;
11754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beustimport java.io.UnsupportedEncodingException;
1215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beustimport java.net.SocketException;
13ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
14754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beustpublic class StringMessageSender extends BaseMessageSender {
15754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust
1615f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  private PrintWriter writer;
1715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
18754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust  public StringMessageSender(String host, int port) {
19754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust    super(host, port);
20754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust  }
21ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
22ed626ea7464c25db7ea754177f36b344195da158Cédric Beust  @Override
23754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust  public void sendMessage(IMessage message) {
24ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust    if (m_outStream == null) {
25ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust      throw new IllegalStateException("Trying to send a message on a shutdown sender");
26ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust    }
27ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust    if (writer == null) {
28ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust      try {
29ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust        writer = new PrintWriter(new BufferedWriter(
30ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust            new OutputStreamWriter(m_outStream, "UTF-8")), //$NON-NLS-1$
31ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust            false /* autoflush */);
32ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust      } catch (UnsupportedEncodingException e1) {
33ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust        writer = new PrintWriter(new BufferedWriter(
34ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust            new OutputStreamWriter(m_outStream)),
35ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust            false /* autoflush */);
36ab96c48086fd0dc4e6c6e69648be8ee283c0bd0eCédric Beust      }
37754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust    }
38754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust
39ed626ea7464c25db7ea754177f36b344195da158Cédric Beust    String msg = ((IStringMessage) message).getMessageAsString();
40ed626ea7464c25db7ea754177f36b344195da158Cédric Beust    if (RemoteTestNG.isVerbose()) {
41e13375131c0502f2553a694109d79cbf00732dfaCédric Beust      p("Sending message:" + message);
42e13375131c0502f2553a694109d79cbf00732dfaCédric Beust      p("  String version:" + msg);
43ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
44ed626ea7464c25db7ea754177f36b344195da158Cédric Beust      StringBuffer buf = new StringBuffer();
45ed626ea7464c25db7ea754177f36b344195da158Cédric Beust      for(int i = 0; i < msg.length(); i++) {
46ed626ea7464c25db7ea754177f36b344195da158Cédric Beust        if('\u0001' == msg.charAt(i)) {
47e13375131c0502f2553a694109d79cbf00732dfaCédric Beust          p("  word:[" + buf.toString() + "]");
48ed626ea7464c25db7ea754177f36b344195da158Cédric Beust          buf.delete(0, buf.length());
49ed626ea7464c25db7ea754177f36b344195da158Cédric Beust        }
50ed626ea7464c25db7ea754177f36b344195da158Cédric Beust        else {
51ed626ea7464c25db7ea754177f36b344195da158Cédric Beust          buf.append(msg.charAt(i));
52ed626ea7464c25db7ea754177f36b344195da158Cédric Beust        }
53ed626ea7464c25db7ea754177f36b344195da158Cédric Beust      }
54e13375131c0502f2553a694109d79cbf00732dfaCédric Beust      p("  word:[" + buf.toString() + "]");
55ed626ea7464c25db7ea754177f36b344195da158Cédric Beust    }
56ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
573eb34813b24377dd042c9e85b8a7854af34e3b81Cédric Beust    synchronized(m_ackLock) {
58754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust      writer.println(msg);
59754256e4b1acba1555bcb1892a2627fd34f594c6Cédric Beust      writer.flush();
600482f188546780be2e9fbb4607765fcaaecb898bCédric Beust      waitForAck();
61ed626ea7464c25db7ea754177f36b344195da158Cédric Beust    }
62ed626ea7464c25db7ea754177f36b344195da158Cédric Beust  }
63ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
64ed626ea7464c25db7ea754177f36b344195da158Cédric Beust  private static void p(String msg) {
65ed626ea7464c25db7ea754177f36b344195da158Cédric Beust    if (RemoteTestNG.isVerbose()) {
66ed626ea7464c25db7ea754177f36b344195da158Cédric Beust      System.out.println("[StringMessageSender] " + msg); //$NON-NLS-1$
67ed626ea7464c25db7ea754177f36b344195da158Cédric Beust    }
68ed626ea7464c25db7ea754177f36b344195da158Cédric Beust  }
69ed626ea7464c25db7ea754177f36b344195da158Cédric Beust
7015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  @Override
7115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  public IMessage receiveMessage() {
7215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    IMessage result = null;
7315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
7415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    if (m_inReader == null) {
7515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      try {
7654c45175326b6cda1d4da7780f906871632de1d5Cédric Beust        m_inReader = new BufferedReader(new InputStreamReader(m_inStream, "UTF-8"));
7715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      } catch (UnsupportedEncodingException e) {
7854c45175326b6cda1d4da7780f906871632de1d5Cédric Beust        m_inReader = new BufferedReader(new InputStreamReader(m_inStream));
7915f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      }
8015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    }
8115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    try {
8215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      try {
8315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//        m_outputWriter = new PrintWriter(new OutputStreamWriter(fSocket.getOutputStream(), "UTF-8"),
8415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//                                  true);
8515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      }
8615f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      catch(UnsupportedEncodingException e1) {
8715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//        m_outputWriter = new PrintWriter(new OutputStreamWriter(fSocket.getOutputStream()), true);
8815f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      }
8915f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      result = receiveMessage(m_inReader.readLine());
9015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    }
9115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    catch(SocketException e) {
9215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      handleThrowable(e);
9315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    }
9415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    catch(IOException e) {
9515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      handleThrowable(e);
9615f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    }
9715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
9815f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    return result;
9915f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//    finally {
10015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      shutDown();
10115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      return null;
10215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//    }
10315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  }
10415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
10515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  protected void handleThrowable(Throwable cause) {
1063eb34813b24377dd042c9e85b8a7854af34e3b81Cédric Beust    if (RemoteTestNG.isVerbose()) {
1073eb34813b24377dd042c9e85b8a7854af34e3b81Cédric Beust      cause.printStackTrace();
1083eb34813b24377dd042c9e85b8a7854af34e3b81Cédric Beust    }
10915f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  }
11015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
11115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//  private String readMessage(BufferedReader in) throws IOException {
11215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//    return in.readLine();
11315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//  }
11415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
11515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  private IMessage receiveMessage(String message) {
116e13375131c0502f2553a694109d79cbf00732dfaCédric Beust    if (message == null) return null;
11715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    IMessage result = null;
11815f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
11915f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    int messageType = MessageHelper.getMessageType(message);
12015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
12115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//    try {
12215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      if(messageType < MessageHelper.SUITE) {
12315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        // Generic message
12415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        result = MessageHelper.unmarshallGenericMessage(message);
12515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      }
12615f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      else if(messageType < MessageHelper.TEST) {
12715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        // Suite message
12815f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        result = MessageHelper.createSuiteMessage(message);
12915f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      }
13015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      else if(messageType < MessageHelper.TEST_RESULT) {
13115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        // Test message
13215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        result = MessageHelper.createTestMessage(message);
13315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      }
13415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      else {
13515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        // TestResult message
13615f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust        result = MessageHelper.unmarshallTestResultMessage(message);
13715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust      }
13815f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//    }
13915f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//    finally {
14015f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      if(isRunning() && (null != m_outputWriter)) {
14115f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//        m_outputWriter.println(MessageHelper.ACK_MSG);
14215f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//        m_outputWriter.flush();
14315f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//      }
14415f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust//    }
14515f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust
14615f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    p("receiveMessage() received:" + result);
14715f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust    return result;
14815f707e1b3e9b4360b4b1e5fa5ad7a3b396b04fdCédric Beust  }
149ed626ea7464c25db7ea754177f36b344195da158Cédric Beust}
150