Project

General

Profile

Download (4.77 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / message / EffectMessageSender.java @ 43eecb4c

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted 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, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.message;
21

    
22
import org.distorted.library.effect.Effect;
23

    
24
import java.util.Vector;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
/**
28
 * Not part of public API, do not document (public only because has to be used in Meshes)
29
 *
30
 * @y.exclude
31
 */
32
public final class EffectMessageSender extends Thread
33
  {
34
  private static class Message
35
    {
36
    EffectListener mListener;
37
    long mEffectID;
38

    
39
    Message(EffectListener listener, long effectID)
40
      {
41
      mListener = listener;
42
      mEffectID = effectID;
43
      }
44
    }
45

    
46
  private static final Object mLock        = new Object();
47
  private static Vector<Message> mList     = null;
48
  private static EffectMessageSender mThis = null;
49
  private static volatile boolean mNotify  = false;
50
  private static volatile boolean mRunning = false;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
   
54
  private EffectMessageSender() 
55
    {
56

    
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public static void startSending()
62
    {
63
    synchronized(mLock)
64
      {
65
      mRunning = true;
66

    
67
      if( mThis==null )
68
        {
69
        mList = new Vector<>();
70
        mThis = new EffectMessageSender();
71
        mThis.start();
72
        }
73
      else mLock.notify();
74
      }
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
  
79
  public static void stopSending()
80
    {
81
    synchronized(mLock)
82
      {
83
      mRunning = false;
84
      if( mThis!=null ) mLock.notify();
85
      }
86
    }
87
  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
  
90
  public void run()
91
    {
92
    Message tmp;  
93
     
94
    while(mRunning)
95
      {
96
      while( mList.size()>0 )
97
        {
98
        tmp = mList.remove(0);
99
        tmp.mListener.effectFinished(tmp.mEffectID);
100
        }
101

    
102
      synchronized(mLock)
103
        {
104
        if (!mNotify)
105
          {
106
          try  { mLock.wait(); }
107
          catch(InterruptedException ignored) { }
108
          }
109
        mNotify = false;
110
        }
111
      }
112

    
113
    mThis = null;
114
    mList.clear();
115
    }
116
  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
        
119
  public static void newMessage(Effect effect)
120
    {
121
    int numListeners = effect.getNumListeners();
122

    
123
    if( numListeners>0 )
124
      {
125
      long id = effect.getID();
126

    
127
      for(int i=0; i<numListeners; i++)
128
        {
129
        EffectListener listener = effect.removeFirstListener();
130
        Message msg = new Message(listener,id);
131
        mList.add(msg);
132
        }
133

    
134
      synchronized(mLock)
135
        {
136
        mNotify = true;
137
        mLock.notify();
138
        }
139
      }
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public static String reportState()
145
    {
146
    return "mRunning="+mRunning+" mNotify="+mNotify+" mThis null="+(mThis==null)+" mList elements="+mList.size();
147
    }
148
  }
(2-2/2)