File: UpdateMessageBroker.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (79 lines) | stat: -rw-r--r-- 1,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package tim.prune;

/**
 * Class responsible for distributing update information
 * to all registered listeners
 */
public abstract class UpdateMessageBroker
{
	private static final int MAXIMUM_NUMBER_SUBSCRIBERS = 7;
	/** Array of all subscribers */
	private static DataSubscriber[] _subscribers = new DataSubscriber[MAXIMUM_NUMBER_SUBSCRIBERS];
	/** Counter of the number of subscribers added so far */
	private static int _subscriberNum = 0;
	/** Enable/disabled flag */
	private static boolean _enabled = true;


	/**
	 * Add a data subscriber to the list
	 * @param inSub DataSubscriber to add
	 */
	public static void addSubscriber(DataSubscriber inSub)
	{
		_subscribers[_subscriberNum] = inSub;
		_subscriberNum++;
	}

	/**
	 * Enable or disable the messaging (to allow temporary disabling for multiple operations)
	 * @param inEnabled false to disable, true to enable again
	 */
	public static void enableMessaging(boolean inEnabled)
	{
		_enabled = inEnabled;
	}

	/**
	 * Send a message to all subscribers that
	 * the data has been updated
	 */
	public static void informSubscribers()
	{
		informSubscribers(DataSubscriber.ALL);
	}


	/**
	 * Send message to all subscribers
	 * @param inChange Change that occurred
	 */
	public static void informSubscribers(byte inChange)
	{
		// TODO: Launch separate thread so that whatever caused the inform can finish
		if (!_enabled) return;
		for (int i=0; i<_subscribers.length; i++)
		{
			if (_subscribers[i] != null)
			{
				_subscribers[i].dataUpdated(inChange);
			}
		}
	}

	/**
	 * Send message to all subscribers
	 * @param inMessage message to display informing of action completed
	 */
	public static void informSubscribers(String inMessage)
	{
		if (!_enabled) return;
		for (int i=0; i<_subscribers.length; i++)
		{
			if (_subscribers[i] != null)
			{
				_subscribers[i].actionCompleted(inMessage);
			}
		}
	}
}