Project

General

Profile

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

library / src / main / java / org / distorted / library / message / EffectMessageSender.java @ 46b572b5

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 java.util.Vector;
23

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

    
39
    Message(EffectListener l, EffectMessage m, long effID, long objID)
40
      {
41
      mListener = l;
42
      mMessage  = m;
43
      mEffectID = effID;
44
      mObjectID = objID;
45
      }
46
    }
47
  
48
  private static Vector<Message> mList =null;
49
  private static EffectMessageSender mThis=null;
50
  private static volatile boolean mNotify  = false;
51
  private static volatile boolean mRunning = false;
52
  private static final Object mLock = new Object();
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
   
56
  private EffectMessageSender() 
57
    {
58

    
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  public static void startSending()
64
    {
65
    mRunning = true;
66

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

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

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

    
111
      synchronized(mLock)
112
        {
113
        if (!mNotify)
114
          {
115
          try  { mThis.wait(); }
116
          catch(InterruptedException ex) { }
117
          }
118
        mNotify = false;
119
        }
120
      }
121

    
122
    mThis = null;
123
    mList.clear();
124
    }
125
  
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
        
128
  public static void newMessage(EffectListener l, EffectMessage m, long effID, long objID)
129
    {
130
    Message msg = mThis.new Message(l,m,effID,objID);
131
    mList.add(msg);
132

    
133
    synchronized(mLock)
134
      {
135
      mNotify = true;
136
      mThis.notify();
137
      }
138
    }
139
  }
(3-3/3)