Project

General

Profile

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

library / src / main / java / org / distorted / library / message / EffectMessageSender.java @ b7209ffe

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 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 Vector<Message> mList =null;
47
  private static EffectMessageSender mThis=null;
48
  private static volatile boolean mNotify  = false;
49
  private static volatile boolean mRunning = false;
50

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

    
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public static void startSending()
61
    {
62
    mRunning = true;
63

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

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

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

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

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

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

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

    
140
      synchronized(mThis)
141
        {
142
        mNotify = true;
143
        mThis.notify();
144
        }
145
      }
146
    }
147
  }
(2-2/2)