1 package net.suberic.pooka.messaging; 2 3 import java.net.*; 4 import java.nio.channels.*; 5 import java.nio.charset.Charset ; 6 import java.io.*; 7 import java.util.logging.*; 8 import java.nio.channels.SocketChannel ; 9 10 import net.suberic.pooka.Pooka; 11 12 15 public class PookaMessageSender { 16 17 18 SocketChannel mChannel = null; 19 20 21 boolean mConnected = false; 22 23 27 public void openConnection() throws java.net.UnknownHostException , 28 java.io.IOException , 29 SecurityException { 30 int port; 31 try { 32 port = Integer.parseInt(Pooka.getProperty("Pooka.messaging.port", "")); 33 } catch (Exception e) { 34 port = PookaMessagingConstants.S_PORT; 35 } 36 getLogger().log(Level.FINE, "opening port " + port); 37 SocketAddress address = new InetSocketAddress("localhost",port); 38 SocketChannel channel = SocketChannel.open(); 39 channel.configureBlocking(false); 40 if (! channel.connect(address)) { 41 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 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 65 public void openNewEmail(String pAddress, String pUserProfile) throws java.io.IOException { 66 StringBuffer sendBuffer = new StringBuffer (); 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 85 public boolean checkVersion() throws java.io.IOException { 86 sendMessage(PookaMessagingConstants.S_CHECK_VERSION); 87 88 String response = retrieveResponse(); 89 getLogger().log(Level.FINE, "got response " + response); 90 91 return (response != null && response.equals(Pooka.getPookaManager().getLocalrc())); 92 } 93 94 97 public void sendStartPookaMessage() throws java.io.IOException { 98 getLogger().log(Level.FINE, "sending message " + PookaMessagingConstants.S_START_POOKA); 99 sendMessage(PookaMessagingConstants.S_START_POOKA); 100 } 101 102 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 ioe) { 112 } finally { 114 mChannel = null; 115 mConnected = false; 116 } 117 } 118 } 119 120 123 public void sendMessage(String pMessage) throws java.io.IOException { 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 134 public String retrieveResponse() throws java.io.IOException { 135 BufferedReader reader = new BufferedReader(Channels.newReader(mChannel,"UTF-8")); 136 String returnValue = reader.readLine(); 137 getLogger().log(Level.FINE, "got response '" + returnValue + "'"); 138 return returnValue; 139 } 140 141 144 public boolean isConnected() { 145 return mConnected; 146 } 147 148 151 public Logger getLogger() { 152 return Logger.getLogger("Pooka.debug.messaging"); 153 } 154 155 } 156 | Popular Tags |