1 package net.suberic.pooka.messaging; 2 3 import java.net.*; 4 import java.nio.channels.*; 5 6 import java.io.*; 7 import java.util.*; 8 import java.util.logging.*; 9 10 import javax.mail.*; 11 import javax.mail.internet.MimeMessage ; 12 13 import net.suberic.pooka.*; 14 import net.suberic.pooka.gui.NewMessageProxy; 15 import net.suberic.pooka.gui.NewMessageFrame; 16 import net.suberic.pooka.gui.MessageUI; 17 18 21 public class PookaMessageListener extends Thread { 22 23 ServerSocket mSocket = null; 24 boolean mStopped = false; 25 java.util.List mActiveHandlers = new java.util.LinkedList (); 26 27 30 public PookaMessageListener() { 31 super("PookaMessageListener Thread"); 32 getLogger().log(Level.FINE, "creating new PookaMessageListener."); 33 start(); 34 } 35 36 39 public void run() { 40 try { 41 getLogger().log(Level.FINE, "creating socket."); 42 createSocket(); 43 getLogger().log(Level.FINE, "socket created."); 44 while (! mStopped) { 45 getLogger().log(Level.FINE, "accepting connection."); 46 Socket currentSocket = mSocket.accept(); 47 if (! mStopped) { 48 getLogger().log(Level.FINE, "got connection."); 49 PookaMessageHandler handler = new PookaMessageHandler(this, currentSocket); 50 mActiveHandlers.add(handler); 51 handler.start(); 52 } 53 } 54 } catch (Exception e) { 55 System.out.println("error in MessagingListener -- exiting."); 56 e.printStackTrace(); 57 } 58 } 59 60 61 64 public void createSocket() throws Exception { 65 getLogger().log(Level.FINE, "creating new PookaMessageListener socket."); 66 Exception throwMe = null; 67 boolean success = false; 68 for (int port = PookaMessagingConstants.S_PORT; (! success) && port < PookaMessagingConstants.S_PORT + 25; port++) { 69 try { 70 mSocket = new ServerSocket(port); 71 success = true; 72 String propertiesPort = Pooka.getProperty("Pooka.messaging.port", Integer.toString(port)); 73 Pooka.setProperty("Pooka.messaging.port", Integer.toString(port)); 74 if (! Integer.toString(port).equals(propertiesPort)) { 75 Pooka.getResources().saveProperties(); 76 } 77 } catch (Exception e) { 78 throwMe = e; 79 } 80 } 81 82 if (! success) 83 throw throwMe; 84 } 85 86 89 public void stopMessageListener() { 90 mStopped = true; 91 92 try { 93 closeServerSocket(); 94 } catch (Exception e) { 95 } 97 98 Iterator iter = mActiveHandlers.iterator(); 99 while(iter.hasNext()) { 100 ((PookaMessageHandler) iter.next()).stopHandler(); 101 } 102 } 103 104 107 void closeServerSocket() throws java.io.IOException { 108 if (mSocket != null) { 109 mSocket.close(); 110 } 111 } 112 113 116 public void removeHandler(PookaMessageHandler pHandler) { 117 mActiveHandlers.remove(pHandler); 118 } 119 120 123 public Logger getLogger() { 124 return Logger.getLogger("Pooka.debug.messaging"); 125 } 126 } 127 | Popular Tags |