001    /**
002     * Copyright (C) 2010, Skype Limited
003     *
004     * All intellectual property rights, including but not limited to copyrights,
005     * trademarks and patents, as well as know how and trade secrets contained in,
006     * relating to, or arising from the internet telephony software of
007     * Skype Limited (including its affiliates, "Skype"), including without
008     * limitation this source code, Skype API and related material of such
009     * software proprietary to Skype and/or its licensors ("IP Rights") are and
010     * shall remain the exclusive property of Skype and/or its licensors.
011     * The recipient hereby acknowledges and agrees that any unauthorized use of
012     * the IP Rights is a violation of intellectual property laws.
013     *
014     * Skype reserves all rights and may take legal action against infringers of
015     * IP Rights.
016     *
017     * The recipient agrees not to remove, obscure, make illegible or alter any
018     * notices or indications of the IP Rights and/or Skype's rights and
019     * ownership thereof.
020     */
021    
022    package com.skype.ipc;
023    
024    import java.util.concurrent.BlockingQueue;
025    import java.util.concurrent.LinkedBlockingQueue;
026    
027    import com.skype.ipc.Event;
028    import com.skype.ipc.PropertyChange;
029    
030    import com.skype.util.Log;
031    
032    public class EventDispatcher extends Thread {
033      private static final String TAG = "EventDispatcher";  
034      private final BlockingQueue<AbstractDecoder> event_queue;
035      private boolean die;
036      private Thread my_thread;
037      private EventHandler myHandler;
038      
039      public EventDispatcher(EventHandler inHandler) {
040        setName("EventDispatcher thread");
041        myHandler=inHandler;
042        die=false;
043            event_queue=new LinkedBlockingQueue<AbstractDecoder>(); // does not need to be fair
044      }
045    
046      public void Die() {
047              die=true;
048              my_thread.interrupt(); // trigger interrupt in case of waiting
049      }
050      
051      public void run() {
052            my_thread=Thread.currentThread();
053            Log.d(TAG,"Running! Threadid="+my_thread.getId());
054            AbstractDecoder event;
055        while(!die) {
056            event=null;
057                    try {
058                            event=event_queue.take();
059                    } catch (InterruptedException e) {
060                            if(die)
061                            {
062                                    Log.d(TAG,"EventDispatcher eventQueue.take() interrupted and die=true.");
063                                    break;
064                            }
065                            else
066                            {
067                                    Log.d(TAG,"EventDispatcher eventQueue.take() interrupted but die=false. Ignoring.");
068                            }
069                    }
070                    if(event==null) {}
071                    else if(event instanceof Event)
072                    {
073                            int eid=((Event)event).getEventId();
074                            Log.d(TAG,"Dispatching an event id="+eid);
075                            try
076                            {
077                                    myHandler.HandleEvent((Event)event);
078                            }
079                            catch(Exception e)
080                            {
081                                    Log.e(TAG,"Skype.HandleEvent threw unhandled exception. Event (id="+eid+") probably lost. Stack trace follows.");
082                                    e.printStackTrace();
083                            }
084                            Log.d(TAG,"Dispatched an event id="+eid);
085                    }
086                    else if(event instanceof PropertyChange)
087                    {
088                            int prid=((PropertyChange)event).propid;
089                            Log.d(TAG,"Dispatching a propertychange propid="+prid);
090                            try
091                            {
092                                    myHandler.HandlePropertyChange((PropertyChange)event);
093                            }
094                            catch(Exception e)
095                            {
096                                    Log.e(TAG,"Skype.HandlePropertyChange threw unhandled exception. Property change (id="+prid+") probably lost. Stack trace follows."); e.printStackTrace();
097                            }
098    
099                            Log.d(TAG,"Dispatched a propertychange propid="+prid);
100                    }
101                    else
102                    {
103                            Log.e(TAG,"Event queue entry is of unknown type! - discarding");
104                    }
105        }
106        Log.d(TAG,"Exiting!");
107      }
108    
109            // These are called from response listener
110            public void AddPropertyChange(PropertyChange c) throws InterruptedException
111            {
112                    Log.d(TAG,"Adding PropertyChange (propid="+c.propid+") to eventQueue. eQ size="+event_queue.size());
113                    event_queue.put(c);
114                    Log.d(TAG,"Added PropertyChange (propid="+c.propid+") to eventQueue. eQ size="+event_queue.size());
115            }
116            
117            public void AddEvent(Event e) throws InterruptedException
118            {
119                    Log.d(TAG,"Adding Event (event_id="+e.getEventId()+") to eventQueue. eQ size="+event_queue.size());
120                    event_queue.put(e);
121                    Log.d(TAG,"Added Event (event_id="+e.getEventId()+") to eventQueue. eQ size="+event_queue.size());
122            }
123    }
124