KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > messaging > PookaMessageSender


1 package net.suberic.pooka.messaging;
2
3 import java.net.*;
4 import java.nio.channels.*;
5 import java.nio.charset.Charset JavaDoc;
6 import java.io.*;
7 import java.util.logging.*;
8 import java.nio.channels.SocketChannel JavaDoc;
9
10 import net.suberic.pooka.Pooka;
11
12 /**
13  * This class sends messages to a Pooka network client.
14  */

15 public class PookaMessageSender {
16   
17   /** the channel that's connected to a PookaMessageListener. */
18   SocketChannel JavaDoc mChannel = null;
19
20   /** whether or not we have a connection open. */
21   boolean mConnected = false;
22
23   /**
24    * This opens a connection to the server port of the running Pooka
25    * instance.
26    */

27   public void openConnection() throws java.net.UnknownHostException JavaDoc,
28                       java.io.IOException JavaDoc,
29                       SecurityException JavaDoc {
30     int port;
31     try {
32       port = Integer.parseInt(Pooka.getProperty("Pooka.messaging.port", ""));
33     } catch (Exception JavaDoc e) {
34       port = PookaMessagingConstants.S_PORT;
35     }
36     getLogger().log(Level.FINE, "opening port " + port);
37     SocketAddress address = new InetSocketAddress("localhost",port);
38     SocketChannel JavaDoc channel = SocketChannel.open();
39     channel.configureBlocking(false);
40     if (! channel.connect(address)) {
41       // we're willing to wait for about a second.
42
for (int i = 0; (! channel.finishConnect()) && i < 4; i++) {
43     try {
44       getLogger().log(Level.FINE, "not connected; sleeping (" + i + ").");
45       Thread.currentThread().sleep(250);
46     } catch (Exception JavaDoc e) {
47     }
48       }
49     }
50     if (channel.isConnected()) {
51       getLogger().log(Level.FINE, "got connection.");
52       mChannel = channel;
53       mChannel.configureBlocking(true);
54
55       mConnected = true;
56     } else {
57       getLogger().log(Level.FINE, "failed to get connection.");
58       throw new SocketTimeoutException("Unable to connect to server localhost at port " + port);
59     }
60   }
61
62   /**
63    * Sends a new message message to the server.
64    */

65   public void openNewEmail(String JavaDoc pAddress, String JavaDoc pUserProfile) throws java.io.IOException JavaDoc {
66     StringBuffer JavaDoc sendBuffer = new StringBuffer JavaDoc();
67     sendBuffer.append(PookaMessagingConstants.S_NEW_MESSAGE);
68     if (pAddress != null && pAddress.length() > 0) {
69       sendBuffer.append(" ");
70       sendBuffer.append(pAddress);
71       if (pUserProfile != null && pUserProfile.length() > 0) {
72     sendBuffer.append(" ");
73     sendBuffer.append(pUserProfile);
74       }
75     }
76     
77     getLogger().log(Level.FINE, "sending message " + sendBuffer.toString());
78     sendMessage(sendBuffer.toString());
79   }
80
81   /**
82    * Checks the version running on the server with this client to make
83    * sure they're both on the same page.
84    */

85   public boolean checkVersion() throws java.io.IOException JavaDoc {
86     sendMessage(PookaMessagingConstants.S_CHECK_VERSION);
87
88     String JavaDoc response = retrieveResponse();
89     getLogger().log(Level.FINE, "got response " + response);
90     
91     return (response != null && response.equals(Pooka.getPookaManager().getLocalrc()));
92   }
93
94   /**
95    * Starts an instance of Pooka.
96    */

97   public void sendStartPookaMessage() throws java.io.IOException JavaDoc {
98     getLogger().log(Level.FINE, "sending message " + PookaMessagingConstants.S_START_POOKA);
99     sendMessage(PookaMessagingConstants.S_START_POOKA);
100   }
101
102   /**
103    * Closes the connection.
104    */

105   public void closeConnection() {
106     if (mConnected || mChannel != null) {
107       try {
108     getLogger().log(Level.FINE, "sending message " + PookaMessagingConstants.S_BYE);
109     sendMessage(PookaMessagingConstants.S_BYE);
110     mChannel.close();
111       } catch (java.io.IOException JavaDoc ioe) {
112     // ignore -- we're closing anyway.
113
} finally {
114     mChannel = null;
115     mConnected = false;
116       }
117     }
118   }
119   
120   /**
121    * Sends a message.
122    */

123   public void sendMessage(String JavaDoc pMessage) throws java.io.IOException JavaDoc {
124     BufferedWriter writer = new BufferedWriter(Channels.newWriter(mChannel, "UTF-8"));
125     getLogger().log(Level.FINE, "sending message '" + pMessage);
126     writer.write(pMessage);
127     writer.newLine();
128     writer.flush();
129   }
130       
131   /**
132    * Gets a response from the (already open) connection.
133    */

134   public String JavaDoc retrieveResponse() throws java.io.IOException JavaDoc {
135     BufferedReader reader = new BufferedReader(Channels.newReader(mChannel,"UTF-8"));
136     String JavaDoc returnValue = reader.readLine();
137     getLogger().log(Level.FINE, "got response '" + returnValue + "'");
138     return returnValue;
139   }
140
141   /**
142    * Returns if this is connected or not.
143    */

144   public boolean isConnected() {
145     return mConnected;
146   }
147
148   /**
149    * Gets the logger for this class.
150    */

151   public Logger getLogger() {
152     return Logger.getLogger("Pooka.debug.messaging");
153   }
154
155 }
156
Popular Tags