KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > claros > chat > threads > ChatListener


1 package org.claros.chat.threads;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.claros.chat.controllers.QueueController;
10 import org.jivesoftware.smack.PacketListener;
11 import org.jivesoftware.smack.XMPPConnection;
12 import org.jivesoftware.smack.filter.PacketFilter;
13 import org.jivesoftware.smack.packet.Message;
14 import org.jivesoftware.smack.packet.Packet;
15 import org.jivesoftware.smack.packet.Presence;
16
17 /**
18  * @author Umut Gokbayrak
19  */

20 public class ChatListener extends Thread JavaDoc {
21     private static Log log = LogFactory.getLog(ChatListener.class);
22     private static List JavaDoc subsRequest;
23     private String JavaDoc user;
24     private XMPPConnection conn;
25     private MyListener listener;
26     private MyFilter filter;
27
28     /**
29      * Do not use this constructor
30      *
31      */

32     private ChatListener() {
33         super();
34     }
35
36     /**
37      * Constructor method.
38      * @param user
39      * @param conn
40      */

41     public ChatListener(String JavaDoc user, XMPPConnection conn) {
42         if (subsRequest == null) {
43             subsRequest = Collections.synchronizedList(new ArrayList JavaDoc());
44         }
45         this.user = user;
46         this.conn = conn;
47         listener = new MyListener();
48         filter = new MyFilter();
49     }
50
51     /* (non-Javadoc)
52      * @see java.lang.Thread#run()
53      */

54     public void run() {
55         conn.addPacketListener(listener, filter);
56     }
57
58     /**
59      * method to call to stop this thread
60      *
61      */

62     public void terminate() {
63         try {
64             conn.removePacketListener(listener);
65         } catch (RuntimeException JavaDoc e) {
66             e.printStackTrace();
67         }
68     }
69     
70     public List JavaDoc getUnreadMessages() {
71         List JavaDoc msgs = null;
72         try {
73             msgs = QueueController.fetchUserMessages(user, QueueController.QUEUE_IN);
74         } catch (Exception JavaDoc e) {
75             log.error("unable to get unread messages", e);
76         }
77         return msgs;
78     }
79     
80     public synchronized List JavaDoc getNewSubscriptions() {
81         // TODO: mesajlardakinin aynısını queue yapısı bunda yapmalıyız.
82

83         ArrayList JavaDoc outList = new ArrayList JavaDoc();
84         ArrayList JavaDoc toDelete = new ArrayList JavaDoc();
85         
86         // look if new messages are waiting
87
Presence tmp = null;
88         String JavaDoc tmpTo = null;
89         int tmpPos = -1;
90         for (int i=0;i<subsRequest.size();i++) {
91             tmp = (Presence)subsRequest.get(i);
92             tmpTo = tmp.getTo();
93             tmpPos = tmpTo.indexOf("/");
94             if (tmpPos > -1) {
95                 tmpTo = tmpTo.substring(0, tmpPos);
96             }
97             if (tmpTo.equals(user)) {
98                 toDelete.add(new Integer JavaDoc(i));
99                 outList.add(tmp);
100             }
101         }
102
103         // remove the fetched messages
104
for (int i=toDelete.size()-1;i>=0;i--) {
105             try {
106                 subsRequest.remove(((Integer JavaDoc)toDelete.get(i)).intValue());
107             } catch (RuntimeException JavaDoc e) {
108                 e.printStackTrace();
109             }
110         }
111         return outList;
112     }
113
114     /**
115      * Listener class implementation
116      *
117      * @author Umut Gokbayrak
118      */

119     class MyListener implements PacketListener {
120
121         /* (non-Javadoc)
122          * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
123          */

124         public void processPacket(Packet arg0) {
125             if (arg0 instanceof Message) {
126                 Message msg = (Message)arg0;
127                 
128                 if (msg.getFrom().indexOf("/") > -1) {
129                     msg.setFrom(msg.getFrom().substring(0, msg.getFrom().indexOf("/")));
130                 }
131
132                 if (msg.getTo().indexOf("/") > -1) {
133                     msg.setTo(msg.getTo().substring(0, msg.getTo().indexOf("/")));
134                 }
135
136                 // pushing the message to db.
137
QueueController.push(msg.getFrom(), msg.getTo(), msg.getBody(), QueueController.QUEUE_IN);
138                 
139                 log.debug("new message written to queue, from:" + msg.getFrom() + " to: " + msg.getTo() + " body: " + msg.getBody());
140             } else if (arg0 instanceof Presence) {
141                 Presence prs = (Presence)arg0;
142                 if (prs.getType().equals(Presence.Type.SUBSCRIBE)) {
143                     subsRequest.add(arg0);
144                 }
145             }
146         }
147     }
148
149     /**
150      * Listener packet filter implementation.
151      *
152      * @author Umut Gokbayrak
153      */

154     class MyFilter implements PacketFilter {
155
156         /* (non-Javadoc)
157          * @see org.jivesoftware.smack.filter.PacketFilter#accept(org.jivesoftware.smack.packet.Packet)
158          */

159         public boolean accept(Packet arg0) {
160             // TODO: ignored users should be handled here.
161
return true;
162         }
163     }
164
165     /**
166      * @return
167      */

168     public String JavaDoc getUser() {
169         return user;
170     }
171
172     /**
173      * @param string
174      */

175     public void setUser(String JavaDoc string) {
176         user = string;
177     }
178
179 }
180
Popular Tags