1 /* 2 * Created on 29-Apr-2004 3 * Created by Paul Gardner 4 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 */ 19 20 package org.gudy.azureus2.pluginsimpl.local.utils; 21 22 /** 23 * @author parg 24 * 25 */ 26 27 import org.gudy.azureus2.plugins.*; 28 import org.gudy.azureus2.plugins.utils.*; 29 30 import org.gudy.azureus2.core3.util.*; 31 32 public class 33 UTTimerImpl 34 implements UTTimer 35 { 36 private PluginInterface plugin_interface; 37 private Timer timer; 38 39 private boolean destroyed; 40 41 public UTTimerImpl( String name, boolean lightweight )42 UTTimerImpl( 43 String name, 44 boolean lightweight ) 45 { 46 // this constructor is for external (non-az) use, e.g. someone using the UPnP component 47 // in their own app 48 49 if ( !lightweight ){ 50 51 timer = new Timer( name ); 52 } 53 } 54 55 protected UTTimerImpl( PluginInterface pi, String name, boolean lightweight )56 UTTimerImpl( 57 PluginInterface pi, 58 String name, 59 boolean lightweight ) 60 { 61 plugin_interface = pi; 62 63 if ( !lightweight ){ 64 65 timer = new Timer( "Plugin " + pi.getPluginID() + ":" + name ); 66 } 67 } 68 69 protected UTTimerImpl( PluginInterface pi, String name, int priority )70 UTTimerImpl( 71 PluginInterface pi, 72 String name, 73 int priority ) 74 { 75 plugin_interface = pi; 76 77 timer = new Timer( "Plugin " + pi.getPluginID() + ":" + name, 1, priority ); 78 } 79 80 public UTTimerEvent addEvent( long when, final UTTimerEventPerformer ext_performer )81 addEvent( 82 long when, 83 final UTTimerEventPerformer ext_performer ) 84 { 85 if ( destroyed ){ 86 87 throw( new RuntimeException( "Timer has been destroyed" )); 88 } 89 90 final timerEvent res = new timerEvent(); 91 92 TimerEventPerformer performer = 93 new TimerEventPerformer() 94 { 95 public void 96 perform( 97 TimerEvent ev ) 98 { 99 UtilitiesImpl.callWithPluginThreadContext( 100 plugin_interface, 101 new Runnable() 102 { 103 public void 104 run() 105 { 106 res.perform( ext_performer ); 107 } 108 }); 109 } 110 }; 111 112 if ( timer == null ){ 113 114 res.setEvent( SimpleTimer.addEvent( "Plugin:" + ext_performer.getClass(), when, performer )); 115 116 }else{ 117 118 res.setEvent( timer.addEvent( "Plugin:" + ext_performer.getClass(), when, performer )); 119 120 } 121 122 return( res ); 123 } 124 125 public UTTimerEvent addPeriodicEvent( long periodic_millis, final UTTimerEventPerformer ext_performer )126 addPeriodicEvent( 127 long periodic_millis, 128 final UTTimerEventPerformer ext_performer ) 129 { 130 if ( destroyed ){ 131 132 throw( new RuntimeException( "Timer has been destroyed" )); 133 } 134 135 final timerEvent res = new timerEvent(); 136 137 TimerEventPerformer performer = 138 new TimerEventPerformer() 139 { 140 public void 141 perform( 142 TimerEvent ev ) 143 { 144 UtilitiesImpl.callWithPluginThreadContext( 145 plugin_interface, 146 new Runnable() 147 { 148 public void 149 run() 150 { 151 try{ 152 153 res.perform( ext_performer ); 154 155 }catch( Throwable e ){ 156 157 Debug.out("Plugin '" + plugin_interface.getPluginName() + " (" 158 + plugin_interface.getPluginID() + " " 159 + plugin_interface.getPluginVersion() 160 + ") caused an error while processing a timer event", e); 161 } 162 } 163 }); 164 } 165 }; 166 167 if ( timer == null ){ 168 169 res.setEvent( SimpleTimer.addPeriodicEvent( "Plugin:" + ext_performer.getClass(), periodic_millis, performer )); 170 171 }else{ 172 173 res.setEvent( timer.addPeriodicEvent( "Plugin:" + ext_performer.getClass(), periodic_millis, performer )); 174 175 } 176 177 return( res ); 178 } 179 180 public void destroy()181 destroy() 182 { 183 destroyed = true; 184 185 if ( timer != null ){ 186 187 timer.destroy(); 188 } 189 } 190 191 private static class 192 timerEvent 193 implements UTTimerEvent 194 { 195 protected TimerEvent ev; 196 protected TimerEventPeriodic pev; 197 198 protected void setEvent( TimerEventPeriodic _ev )199 setEvent( 200 TimerEventPeriodic _ev ) 201 { 202 pev = _ev; 203 } 204 205 protected void setEvent( TimerEvent _ev )206 setEvent( 207 TimerEvent _ev ) 208 { 209 ev = _ev; 210 } 211 212 protected void perform( UTTimerEventPerformer p )213 perform( 214 UTTimerEventPerformer p ) 215 { 216 p.perform( this ); 217 } 218 219 public void cancel()220 cancel() 221 { 222 if ( ev != null ){ 223 224 ev.cancel(); 225 226 }else{ 227 228 pev.cancel(); 229 } 230 } 231 } 232 } 233