1 /*
2  * Created on Sep 18, 2012
3  * Created by Paul Gardner
4  *
5  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * This program 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  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 
21 package org.gudy.azureus2.core3.util;
22 
23 import java.util.Iterator;
24 
25 import org.gudy.azureus2.core3.config.COConfigurationManager;
26 
27 import com.aelitis.azureus.core.util.CopyOnWriteList;
28 
29 public class
30 AERunStateHandler
31 {
32 	public static final long		RS_DELAYED_UI			= 0x00000001;
33 	public static final long		RS_UDP_NET_ONLY			= 0x00000002;
34 	public static final long		RS_DHT_SLEEPING			= 0x00000004;
35 
36 	public static final long		RS_ALL_ACTIVE			= 0x00000000;
37 	public static final long		RS_ALL_LOW				= 0xffffffff;
38 
39 	public static final long[] RS_MODES = {
40 		RS_DELAYED_UI,
41 		RS_UDP_NET_ONLY,
42 		RS_DHT_SLEEPING
43 	};
44 
45 	public static final String[] RS_MODE_NAMES = {
46 		"dui: Delay UI Initialisation",
47 		"uno: UDP Network Only",
48 		"ds:  DHT Sleeping"
49 	};
50 
51 
52 	private static boolean	start_low = COConfigurationManager.getBooleanParameter( "Start In Low Resource Mode" );
53 
54 	private static long	current_mode = start_low?RS_ALL_LOW:RS_ALL_ACTIVE;
55 
56 	private static AsyncDispatcher	dispatcher = new AsyncDispatcher(2500);
57 
58 	private static CopyOnWriteList<RunStateChangeListener>	listeners = new CopyOnWriteList<RunStateChangeListener>();
59 
60 	public static boolean
isDelayedUI()61 	isDelayedUI()
62 	{
63 		return( ( current_mode & RS_DELAYED_UI ) != 0 );
64 	}
65 
66 	public static boolean
isUDPNetworkOnly()67 	isUDPNetworkOnly()
68 	{
69 		return( ( current_mode & RS_UDP_NET_ONLY ) != 0 );
70 	}
71 
72 	public static boolean
isDHTSleeping()73 	isDHTSleeping()
74 	{
75 		return( ( current_mode & RS_DHT_SLEEPING ) != 0 );
76 	}
77 
78 	public static long
getResourceMode()79 	getResourceMode()
80 	{
81 		return( current_mode );
82 	}
83 
84 	public static void
setResourceMode( final long new_mode )85 	setResourceMode(
86 		final long		new_mode )
87 	{
88 		synchronized( dispatcher ){
89 
90 			if ( new_mode == current_mode ){
91 
92 				return;
93 			}
94 
95 			current_mode = new_mode;
96 
97 			final Iterator<RunStateChangeListener> it = listeners.iterator();
98 
99 			dispatcher.dispatch(
100 				new AERunnable()
101 				{
102 					public void
103 					runSupport()
104 					{
105 						while( it.hasNext()){
106 
107 							try{
108 								it.next().runStateChanged( new_mode );
109 
110 							}catch( Throwable e ){
111 
112 								Debug.out( e );
113 							}
114 						}
115 
116 					}
117 				});
118 		}
119 	}
120 
121 	public static void
addListener( final RunStateChangeListener l, boolean fire_now )122 	addListener(
123 		final RunStateChangeListener	l,
124 		boolean							fire_now )
125 	{
126 		synchronized( dispatcher ){
127 
128 			listeners.add( l );
129 
130 			if ( fire_now ){
131 
132 				dispatcher.dispatch(
133 					new AERunnable()
134 					{
135 						public void
136 						runSupport()
137 						{
138 							try{
139 								l.runStateChanged( current_mode );
140 
141 							}catch( Throwable e ){
142 
143 								Debug.out( e );
144 							}
145 						}
146 					});
147 			}
148 		}
149 	}
150 
151 	public static void
removeListener( RunStateChangeListener l )152 	removeListener(
153 		RunStateChangeListener	l )
154 	{
155 		synchronized( dispatcher ){
156 
157 			listeners.remove( l );
158 		}
159 	}
160 
161 	public interface
162 	RunStateChangeListener
163 	{
164 		public void
runStateChanged( long run_state )165 		runStateChanged(
166 			long		run_state );
167 	}
168 }
169