View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  
21  package org.xnap.plugin.opennap.net.msg;
22  
23  import java.util.Hashtable;
24  import java.util.*;
25  
26  import org.xnap.chat.ChatManager;
27  import org.xnap.plugin.opennap.OpenNapPlugin;
28  import org.xnap.plugin.opennap.net.OpenNapServer;
29  import org.xnap.plugin.opennap.net.PrivateOpenNapChannel;
30  import org.xnap.plugin.opennap.net.msg.client.ClientMessage;
31  import org.xnap.plugin.opennap.net.msg.server.ServerMessage;
32  import org.xnap.plugin.opennap.user.OpenNapUser;
33  
34  public class MessageHandler {
35  
36      //--- Constant(s) ---
37  
38      //--- Data field(s) ---
39  
40      /***
41       * Maps message types to <code>MessageListener</code> objects.
42       */
43      private Hashtable listeners = new Hashtable();
44  
45      /***
46       * Maps <code>OpenNapUser</code> objects to
47       * <code>PrivateOpenNapChannel</code> objects.
48  	 */
49      private Hashtable privateChannels = new Hashtable();
50  
51      private MessageSender sender = new MessageSender();
52  
53      //--- Constructor(s) ---
54  
55      public MessageHandler()
56      {
57      }
58      
59      //--- Method(s) ---
60  
61      /***
62       * @see MessageSender.send(OpenNapServer, OvernetClientMessage)
63       */
64      public static void send(OpenNapServer server, ClientMessage msg)
65      {
66  		OpenNapPlugin.getMessageHandler().sender.send(server, msg);
67      }
68  
69      /***
70       * @see MessageSender.send(OvernetClientMessage)
71       */
72      public static void send(ClientMessage msg)
73      {
74  		OpenNapPlugin.getMessageHandler().sender.send(msg);
75      }
76  
77      public void die()
78      {
79  		sender.die();
80      }
81  
82      /***
83       * Handles message itself first and then sends it to all listeners. A
84       * listener can consume the message to prevent other listeners from
85       * getting it.
86       */
87      public void handle(ServerMessage msg)
88      {
89  		msg.received();
90  
91  		Object[] listeners = getListeners(msg.type).toArray();
92  		for (int i = 0; i < listeners.length && !msg.isConsumed(); i++) {
93  			((MessageListener)listeners[i]).messageReceived(msg);
94  		}
95      }	
96  
97      public synchronized PrivateOpenNapChannel getPrivateChannel
98  		(OpenNapUser user)
99      {
100 		PrivateOpenNapChannel pc = 
101 			(PrivateOpenNapChannel)privateChannels.get(user);
102 		if (pc == null) {
103 			pc = new PrivateOpenNapChannel(user);
104 			pc.join();
105 			privateChannels.put(user, pc);
106 			ChatManager.getInstance().add(pc);
107 		}
108 		return pc;
109     }
110 
111     public synchronized void removePrivateChannel(PrivateOpenNapChannel pc)
112     {
113 		privateChannels.remove(pc.getUser());
114     }
115 
116     public static void subscribe(int type, MessageListener listener)
117     {
118 		OpenNapPlugin.getMessageHandler().getListeners(type).add(listener);
119     }
120 
121     public static void unsubscribe(int type, MessageListener listener) 
122     {
123 		OpenNapPlugin.getMessageHandler().getListeners(type).remove(listener);
124     }
125 
126     /***
127      * Returns listeners list for corresponding message type. Creates a new
128      * list of listeners if it doesn't exist yet.
129 	 */
130     private synchronized List getListeners(int type) 
131     {
132 		List list = (List)listeners.get(new Integer(type));
133 
134 		if (list == null) {
135 			list = Collections.synchronizedList(new LinkedList());
136 			listeners.put(new Integer(type), list);
137 		}		
138 		return list;
139     }
140 
141 }