Project

General

Profile

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

library / src / main / java / org / distorted / library / message / EffectMessageSender.java @ 188b3dc0

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
    mRunning = true;
64

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

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
  
82
  public static void stopSending()
83
    {
84
    mRunning = false;
85

    
86
    if( mThis!=null )
87
      {
88
      synchronized(mLock)
89
        {
90
        mLock.notify();
91
        }
92
      }
93
    }
94
  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
  
97
  public void run()
98
    {
99
    Message tmp;  
100
     
101
    while(mRunning)
102
      {
103
      while( mList.size()>0 )
104
        {
105
        tmp = mList.remove(0);
106
        tmp.mListener.effectFinished(tmp.mEffectID);
107
        }
108

    
109
      synchronized(mLock)
110
        {
111
        if (!mNotify)
112
          {
113
          try  { mLock.wait(); }
114
          catch(InterruptedException ignored) { }
115
          }
116
        mNotify = false;
117
        }
118
      }
119

    
120
    mThis = null;
121
    mList.clear();
122
    }
123
  
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
        
126
  public static void newMessage(Effect effect)
127
    {
128
    int numListeners = effect.getNumListeners();
129

    
130
    if( numListeners>0 )
131
      {
132
      long id = effect.getID();
133

    
134
      for(int i=0; i<numListeners; i++)
135
        {
136
        EffectListener listener = effect.removeFirstListener();
137
        Message msg = new Message(listener,id);
138
        mList.add(msg);
139
        }
140

    
141
      synchronized(mLock)
142
        {
143
        mNotify = true;
144
        mLock.notify();
145
        }
146
      }
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  public static String reportState()
152
    {
153
    return "mRunning="+mRunning+" mNotify="+mNotify+" mThis null="+(mThis==null)+" mList elements="+mList.size();
154
    }
155
  }
(2-2/2)