KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > p2p > peerconfiguration > Daemon


1 package org.objectweb.proactive.p2p.peerconfiguration;
2
3 import java.beans.*;
4 import java.io.*;
5 import java.rmi.RemoteException JavaDoc;
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 /**
16  * @author jbustos
17  *
18  * To change the template for this generated type comment go to
19  * Window>Preferences>Java>Code Generation>Code and Comments
20  */

21 public class Daemon implements Runnable JavaDoc{
22
23 private ScheduleBean[] schedule;
24 private String JavaDoc p2pRegistry;
25 private String JavaDoc protocol;
26 private boolean isAlive;
27 private Process JavaDoc process;
28 private P2PRegistryImpl register=null;
29 private ProActiveRuntime runtime=null;
30
31 public Daemon(String JavaDoc file) {
32             // Read the XML descriptor and create the schedule
33

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 JavaDoc) d.readObject();
44             protocol = (String JavaDoc) 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 JavaDoc[] 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         // First: check if hte load balancer was to work today:
74

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         // Then: Sleep until tomorrow
85
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 JavaDoc 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     // First: get the schedule for today
106
for (i=0; i< schedule.length; i++) {
107         if (schedule[i].WorkToday()) schedToday.add(schedule[i]);
108     }
109     
110     // Second: order the schedule by start time
111
Object JavaDoc[] schedOrdered = schedToday.toArray();
112     sortSchedule(schedOrdered);
113     
114     // Third: start & stop the application
115
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         // wait for the next start time?
119
if (schedNow.getStartTime() > now) {
120                 wakeUp = (schedNow.getStartTime() - now) * 60 * 1000 + 1;
121                 try {
122                     wait(wakeUp);
123                 } catch (InterruptedException JavaDoc e) {
124                     today = Calendar.getInstance();
125                     now = today.get(Calendar.HOUR_OF_DAY) *100 + today.get(Calendar.MINUTE);
126                 }
127         }
128
129         // the right schedule?
130
if (schedNow.getStartTime() + schedNow.getWorkTime() < now) continue;
131         
132         // start it!
133
if (!isAlive) startIt(schedNow.getMaxLoad());
134
135         // If the schedule < 24 hours, stop it
136

137         if (schedNow.getWorkTime() < 2400 && isAlive) {
138                 wakeUp = (schedNow.getStartTime() + schedNow.getWorkTime() - now) * 60 * 1000;
139                 try {
140                     wait(wakeUp);
141                 } catch (InterruptedException JavaDoc e1) {
142                     if (isAlive) killIt();
143                 }
144             }
145         }
146 }
147
148
149 /************
150  * Sort the schedule array, using the start time
151  */

152
153 private synchronized void sortSchedule(Object JavaDoc[] schedArray) {
154     int i,j;
155     
156     if (schedArray.length < 2) return;
157
158     Object JavaDoc 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 /**********
178  * Methods for start and stop the load balancer
179  */

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 JavaDoc 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 JavaDoc 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 /* (non-Javadoc)
209  * @see java.lang.Object#finalize()
210  */

211 protected void finalize() throws Throwable JavaDoc {
212     killIt();
213     super.finalize();
214 }
215
216 }
217
218
Popular Tags