Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectMessageSender.java @ 0df17fad

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;
21

    
22
import org.distorted.library.message.EffectListener;
23
import org.distorted.library.message.EffectMessage;
24

    
25
import java.util.Vector;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
final class EffectMessageSender extends Thread
30
  {
31
  private class Message
32
    {
33
    EffectListener mListener;
34
    EffectMessage mMessage;
35
    long mEffectID;
36
    int mEffectName;
37
    long mBitmapID;
38
    String mStr;
39

    
40
    Message(EffectListener l, EffectMessage m, long id, int name, long bmpID, String str)
41
      {
42
      mListener   = l;
43
      mMessage    = m;
44
      mEffectID   = id;
45
      mEffectName = name;
46
      mBitmapID   = bmpID;
47
      mStr        = str;
48
      }
49
    }
50
  
51
  private static Vector<Message> mList =null;
52
  private static EffectMessageSender mThis=null;
53
  private static volatile boolean mPaused;
54
  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
   
57
  private EffectMessageSender() 
58
    {
59
    }
60

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

    
63
  static void startSending()
64
    {
65
    mPaused = false;
66
       
67
    if( mThis==null )
68
      {
69
      mList = new Vector<>();
70
      mThis = new EffectMessageSender();
71
      mThis.start();
72
      }
73
    else  
74
      {  
75
      synchronized(mThis)
76
        {
77
        mThis.notify();
78
        }
79
      }
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
  
84
  static void stopSending()
85
    {
86
    mPaused = true;  
87
    }
88
  
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
  
91
  public void run()
92
    {
93
    Message tmp;  
94
     
95
    while(true)
96
      {
97
      if( mList.size()>0 )
98
        {
99
        tmp = mList.get(0); 
100
        tmp.mListener.effectMessage(tmp.mMessage, tmp.mEffectID, tmp.mEffectName, tmp.mBitmapID, tmp.mStr);
101
        mList.remove(0);
102
        }
103
     
104
      if( mPaused ) 
105
        {
106
        synchronized(mThis)
107
          {
108
          try  { mThis.wait(); }
109
          catch(InterruptedException ex) { }
110
          }
111
        }
112
      }
113
    }
114
  
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
        
117
  static void newMessage(EffectListener l, EffectMessage m, long id, int name, long bmpID, String str)
118
    {
119
    if( mThis!=null )
120
      {
121
      Message msg = mThis.new Message(l,m,id,name,bmpID,str);
122
      mList.add(msg);
123
      }
124
    }
125
  }
126
///////////////////////////////////////////////////////////////////////////////////////////////////
(11-11/17)