KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
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 /**
19  * This class listens on a socket for messages from other Pooka clients.
20  */

21 public class PookaMessageListener extends Thread JavaDoc {
22   
23   ServerSocket mSocket = null;
24   boolean mStopped = false;
25   java.util.List JavaDoc mActiveHandlers = new java.util.LinkedList JavaDoc();
26   
27   /**
28    * Creates a new PookaMessageListener.
29    */

30   public PookaMessageListener() {
31     super("PookaMessageListener Thread");
32     getLogger().log(Level.FINE, "creating new PookaMessageListener.");
33     start();
34   }
35
36   /**
37    * Opens the socket and listens to it.
38    */

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 JavaDoc e) {
55       System.out.println("error in MessagingListener -- exiting.");
56       e.printStackTrace();
57     }
58   }
59
60
61   /**
62    * Creats the socket to listen to.
63    */

64   public void createSocket() throws Exception JavaDoc {
65     getLogger().log(Level.FINE, "creating new PookaMessageListener socket.");
66     Exception JavaDoc 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 JavaDoc 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 JavaDoc e) {
78     throwMe = e;
79       }
80     }
81
82     if (! success)
83       throw throwMe;
84   }
85
86   /**
87    * Stops this MessageListener.
88    */

89   public void stopMessageListener() {
90     mStopped = true;
91
92     try {
93       closeServerSocket();
94     } catch (Exception JavaDoc e) {
95       // ignore--we're stopping.
96
}
97
98     Iterator iter = mActiveHandlers.iterator();
99     while(iter.hasNext()) {
100       ((PookaMessageHandler) iter.next()).stopHandler();
101     }
102   }
103
104   /**
105    * Closes the server socket.
106    */

107   void closeServerSocket() throws java.io.IOException JavaDoc {
108     if (mSocket != null) {
109       mSocket.close();
110     }
111   }
112
113   /**
114    * Removes the handler from the active list.
115    */

116   public void removeHandler(PookaMessageHandler pHandler) {
117     mActiveHandlers.remove(pHandler);
118   }
119
120   /**
121    * Gets the logger for this class.
122    */

123   public Logger getLogger() {
124     return Logger.getLogger("Pooka.debug.messaging");
125   }
126 }
127
Popular Tags