1 package org.objectweb.proactive.p2p.peerconfiguration; 2 3 import java.beans.*; 4 import java.io.*; 5 import java.rmi.RemoteException ; 6 import java.util.*; 7 8 import org.objectweb.proactive.ActiveObjectCreationException; 9 import org.objectweb.proactive.ProActive; 10 import org.objectweb.proactive.core.ProActiveException; 11 import org.objectweb.proactive.core.runtime.*; 12 13 import org.objectweb.proactive.p2p.registry.P2PRegistryImpl; 14 15 21 public class Daemon implements Runnable { 22 23 private ScheduleBean[] schedule; 24 private String p2pRegistry; 25 private String protocol; 26 private boolean isAlive; 27 private Process process; 28 private P2PRegistryImpl register=null; 29 private ProActiveRuntime runtime=null; 30 31 public Daemon(String file) { 32 34 isAlive = false; 35 process = null; 36 37 XMLDecoder d; 38 try { 39 d = 40 new XMLDecoder( 41 new BufferedInputStream(new FileInputStream(file))); 42 schedule = (ScheduleBean[]) d.readObject(); 43 p2pRegistry = (String ) d.readObject(); 44 protocol = (String ) d.readObject(); 45 d.close(); 46 } catch (FileNotFoundException e) { 47 System.err.println("[Daemon ERROR] Cannot open "+file); 48 System.exit(-1); 49 } 50 51 try { 52 register = (P2PRegistryImpl) ProActive.lookupActive(P2PRegistryImpl.class.getName(),p2pRegistry); 53 } catch (ActiveObjectCreationException e1) { 54 e1.printStackTrace(); 55 } catch (IOException e1) { 56 e1.printStackTrace(); 57 } 58 59 if (schedule != null) this.run(); 60 } 61 62 public static void main(String [] args) { 63 64 if (args.length < 1) { 65 System.err.println("[Daemon ERROR] use: daemon file_schedule.xml"); 66 System.exit(-1); 67 } 68 Daemon daemon = new Daemon(args[0]); 69 } 70 71 public synchronized void run() { 72 while (true) { 73 75 int i = 0; 76 boolean hasToWork = true; 77 for (i=0; i < schedule.length; i++) { 78 hasToWork = hasToWork & schedule[i].WorkToday(); 79 } 80 81 if (hasToWork) work(); 82 else if (isAlive) killIt(); 83 84 Calendar today= Calendar.getInstance(); 86 87 long hour = today.get(Calendar.HOUR_OF_DAY); 88 long minute = today.get(Calendar.MINUTE); 89 90 long timeToSleep = (2400 - (hour*100 + minute))* 60 * 1000; 91 92 try { 93 wait(timeToSleep); 94 } catch (InterruptedException e) { 95 } 96 } 97 } 98 99 private synchronized void work() { 100 int i = 0; 101 long wakeUp = 0; 102 List schedToday = new LinkedList(); 103 Calendar today = Calendar.getInstance(); 104 105 for (i=0; i< schedule.length; i++) { 107 if (schedule[i].WorkToday()) schedToday.add(schedule[i]); 108 } 109 110 Object [] schedOrdered = schedToday.toArray(); 112 sortSchedule(schedOrdered); 113 114 for (i=0; i< schedOrdered.length; i++) { 116 int now = today.get(Calendar.HOUR_OF_DAY) *100 + today.get(Calendar.MINUTE); 117 ScheduleBean schedNow = (ScheduleBean) schedOrdered[i]; 118 if (schedNow.getStartTime() > now) { 120 wakeUp = (schedNow.getStartTime() - now) * 60 * 1000 + 1; 121 try { 122 wait(wakeUp); 123 } catch (InterruptedException e) { 124 today = Calendar.getInstance(); 125 now = today.get(Calendar.HOUR_OF_DAY) *100 + today.get(Calendar.MINUTE); 126 } 127 } 128 129 if (schedNow.getStartTime() + schedNow.getWorkTime() < now) continue; 131 132 if (!isAlive) startIt(schedNow.getMaxLoad()); 134 135 137 if (schedNow.getWorkTime() < 2400 && isAlive) { 138 wakeUp = (schedNow.getStartTime() + schedNow.getWorkTime() - now) * 60 * 1000; 139 try { 140 wait(wakeUp); 141 } catch (InterruptedException e1) { 142 if (isAlive) killIt(); 143 } 144 } 145 } 146 } 147 148 149 152 153 private synchronized void sortSchedule(Object [] schedArray) { 154 int i,j; 155 156 if (schedArray.length < 2) return; 157 158 Object aux = null; 159 160 for (i=0; i< schedArray.length; i++) { 161 162 for (j=i+1; j< schedArray.length;j++) { 163 164 ScheduleBean sched_i = (ScheduleBean) schedArray[i]; 165 ScheduleBean sched_j = (ScheduleBean) schedArray[j]; 166 167 if (sched_j.getStartTime() < sched_i.getStartTime()) { 168 aux = schedArray[i]; 169 schedArray[i] = schedArray[j]; 170 schedArray[j] = aux; 171 } 172 } 173 174 } 175 } 176 177 180 181 private synchronized void startIt(double maxload) { 182 isAlive = true; 183 System.out.println("[Daemon] Runtime started"); 184 if (runtime == null) runtime = ProActiveRuntimeImpl.getProActiveRuntime(); 185 186 try { 187 register.register(runtime.getURL(),runtime); 188 } catch (RemoteException e) { 189 System.err.println("[Daemon ERROR] Cannot register in the P2PRegistry"); 190 } catch (ProActiveException e) { 191 System.err.println("[Daemon ERROR] Cannot get the ProActive Runtime"); 192 } 193 194 } 195 private synchronized void killIt() { 196 System.out.println("[Daemon] Runtime stoped"); 197 isAlive = false; 198 try { 199 register.unregister(runtime.getURL(),runtime); 200 } catch (RemoteException e) { 201 System.err.println("[Daemon ERROR] Cannot unregister in the P2PRegistry"); 202 } catch (ProActiveException e) { 203 System.err.println("[Daemon ERROR] Cannot get the ProActive Runtime"); 204 } 205 206 } 207 208 211 protected void finalize() throws Throwable { 212 killIt(); 213 super.finalize(); 214 } 215 216 } 217 218 | Popular Tags |