MessageHub.java revision acb03a641d45d538bb9338a5580fc8701820a1c8
1package org.testng.remote.strprotocol;
2
3
4import org.testng.TestNGException;
5import org.testng.remote.RemoteTestNG;
6
7import java.io.IOException;
8
9/**
10 * Central class to connect to the host and send message.
11 *
12 * @author Cedric Beust <cedric@beust.com>
13 */
14public class MessageHub {
15
16  private boolean m_debug = false;
17
18  private IMessageSender m_messageSender;
19
20  public MessageHub(IMessageSender messageSender) {
21    m_messageSender = messageSender;
22  }
23
24  /**
25   * Starts the connection.
26   *
27   * @return <TT>true</TT> if the connection was successful, <TT>false</TT> otherwise
28   * @throws TestNGException if an exception occurred while establishing the connection
29   */
30  public void connect() throws IOException {
31    m_messageSender.connect();
32  }
33
34  /**
35   * Shutsdown the connection to the remote test listener.
36   */
37  public void shutDown() {
38    m_messageSender.shutDown();
39  }
40
41  public void sendMessage(IMessage message) {
42    try {
43      m_messageSender.sendMessage(message);
44    } catch (Exception e) {
45      e.printStackTrace();
46    }
47  }
48
49  public IMessage receiveMessage() {
50    IMessage result = null;
51    try {
52      result = m_messageSender.receiveMessage();
53      m_messageSender.sendAck();
54    } catch (Exception e) {
55      // TODO Auto-generated catch block
56      e.printStackTrace();
57    }
58    return result;
59  }
60
61  private static void p(String msg) {
62    if (RemoteTestNG.isVerbose()) {
63      System.out.println("[StringMessageSenderHelper] " + msg); //$NON-NLS-1$
64    }
65  }
66
67
68  public void setDebug(boolean debug) {
69    m_debug = debug;
70  }
71
72  public void initReceiver() {
73    m_messageSender.initReceiver();
74  }
75}
76