View Javadoc
1   //////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003  Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
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  package net.sf.dexterim.msn;
20  
21  import net.sf.dexterim.msn.event.MessageEvent;
22  import net.sf.dexterim.msn.event.MessageListener;
23  import net.sf.dexterim.msn.io.BufferedMsnMessageReader;
24  import net.sf.dexterim.msn.message.MsnMessage;
25  
26  /***
27   *
28   * @author  Christoph Walcher
29   */
30  public class ReceiverThread extends Thread {
31    private BufferedMsnMessageReader messageReader;
32    private java.util.List listeners;
33    private org.apache.commons.logging.Log log;
34    private boolean stopped;
35  
36    /*** Creates a new instance of ReceiverThread */
37    public ReceiverThread(String id, BufferedMsnMessageReader messageReader) {
38      super(id);
39  
40      this.messageReader = messageReader;
41      stopped = false;
42  
43      listeners = new java.util.ArrayList();
44  
45      log = org.apache.commons.logging.LogFactory.getLog(getClass());
46    }
47  
48    public void run() {
49      while (!stopped && !messageReader.isClosed()) {
50        try {
51          MsnMessage message = this.messageReader.readMesage();
52  
53          if (message != null) {
54            fireMessageReceived(message);
55          }
56          else {
57            // @todo Init parameter!!!
58            sleep(100);
59          }
60        }
61        catch (Exception ex) {
62          if (log.isDebugEnabled()) {
63            log.debug("Exception thrown while reading from msn Network", ex);
64          }
65        }
66      }
67    }
68  
69    public void addMessageListener(MessageListener listener) {
70      listeners.add(listener);
71    }
72  
73    public void removeMessageListener(MessageListener listener) {
74      listeners.remove(listener);
75    }
76  
77    protected void fireMessageReceived(MsnMessage message) {
78      java.util.Iterator listenerIter = listeners.iterator();
79  
80      MessageEvent event = new MessageEvent(this, message);
81  
82      while (listenerIter.hasNext()) {
83        ((MessageListener)listenerIter.next()).messageReceived(event);
84      }
85    }
86  
87    public void setStopped(boolean stopped) {
88      this.stopped = stopped;
89    }
90  }