KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > doctor > Office


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.doctor;
32
33 import org.apache.log4j.Logger;
34
35 public class Office {
36     
37     static Logger logger = Logger.getLogger(Office.class.getName());
38
39   //Number of patients being created at startup
40
public static final int NB_PAT = 15;
41
42   //Number of doctors being created at startup
43
public static final int NB_DOC = 5;
44
45   //Gaussian time parameters of patients (time being healthy)
46
public static final int MEAN_PAT = 10000;
47   public static final int SIGM_PAT = 3000;
48
49   //Gaussian time parameters of doctors (cure finding duration)
50
public static final int MEAN_DOC = 13000;
51   public static final int SIGM_DOC = 4000;
52
53   //Maximal number of doctors and patients (used to size queues)
54
public static final int MAX_PAT = 50;
55   public static final int MAX_DOC = 20;
56
57   //State constants definition
58
public static final int DOC_UNDEF = -1;
59   public static final int PAT_WELL = -2;
60   public static final int PAT_SICK = -1;
61   java.util.Vector JavaDoc patients;
62   java.util.Vector JavaDoc doctors;
63   Office me;
64   Receptionnist recept;
65   DisplayPanel display;
66   RandomTime rand;
67   OfficeWindow win;
68
69
70   public Office() {
71   }
72
73
74   public Office(Integer JavaDoc useLess) {
75     // Creating patient and doctor vectors
76
patients = new java.util.Vector JavaDoc();
77     doctors = new java.util.Vector JavaDoc();
78     
79     // Creating the display window
80

81     win = new OfficeWindow();
82     win.pack();
83     win.setTitle("The Salishan problems (3)");
84     win.show();
85
86     display = win.getDisplay();
87   }
88
89
90   public void init(Office _me, Receptionnist _recept) {
91     me = _me;
92     recept = _recept;
93     createPeople();
94   }
95
96
97   public synchronized void createPeople() {
98     int i;
99     try {
100
101       rand = (RandomTime)org.objectweb.proactive.ProActive.newActive(RandomTime.class.getName(), null);
102
103       for (i = 1; i <= NB_DOC; i++)
104         addDoctor(i, MEAN_DOC, SIGM_DOC);
105
106       for (i = 1; i <= NB_PAT; i++)
107         addPatient(i, MEAN_PAT, SIGM_PAT);
108     } catch (Exception JavaDoc e) {
109       e.printStackTrace();
110     }
111   }
112
113
114   public void addDoctor(int id, long meanTime, long sigmaTime) {
115
116     try {
117       Object JavaDoc params[] = {new Integer JavaDoc(id), new Long JavaDoc(meanTime), new Long JavaDoc(sigmaTime),me,rand};
118
119       Doctor newDoc = (Doctor)org.objectweb.proactive.ProActive.newActive(Doctor.class.getName(), params);
120       doctors.insertElementAt(newDoc, id - 1);
121       recept.addDoctor(id);
122       display.addDoctor(id);
123     } catch (Exception JavaDoc e) {
124       e.printStackTrace();
125     }
126   }
127
128
129   public void addPatient(int id, long meanTime, long sigmaTime) {
130     try {
131       Object JavaDoc params[] = {new Integer JavaDoc(id), new Long JavaDoc(meanTime), new Long JavaDoc(sigmaTime),me,rand};
132
133       Patient newPat = (Patient)org.objectweb.proactive.ProActive.newActive(Patient.class.getName(), params);
134       patients.insertElementAt(newPat, id - 1);
135       display.addPatient(id);
136       Thread.yield();
137       newPat.init();
138     } catch (Exception JavaDoc e) {
139       e.printStackTrace();
140     }
141   }
142
143
144   public synchronized void doctorCureFound(int doctor, int patient, Cure _cure) {
145     display.setPatState(patient, PAT_WELL);
146     display.setDocFinished(doctor, patient);
147
148     Patient pat = (Patient)patients.elementAt(patient - 1);
149     pat.receiveCure(_cure);
150     recept.addDoctor(doctor);
151   }
152
153
154   public synchronized void patientSick(int patient) {
155     display.setPatState(patient, PAT_SICK);
156     recept.addPatient(patient);
157   }
158
159
160   public synchronized void doctorWithPatient(int doctor, int patient) {
161     display.setPatState(patient, doctor);
162
163     Patient pat = (Patient)patients.elementAt(patient - 1);
164     Doctor doc = (Doctor)doctors.elementAt(doctor - 1);
165
166     pat.hasDoctor(doctor);
167     doc.curePatient(patient);
168   }
169
170
171   public static void main(String JavaDoc argv[]) {
172     logger.info("The Salishan problems : Problem 3 - The Doctor's Office");
173     try {
174       Office off = (Office)org.objectweb.proactive.ProActive.newActive(Office.class.getName(), new Object JavaDoc[]{new Integer JavaDoc(0)});
175       Receptionnist recept = (Receptionnist)org.objectweb.proactive.ProActive.newActive(Receptionnist.class.getName(), new Object JavaDoc[]{off});
176       off.init(off, recept);
177     } catch (Exception JavaDoc e) {
178       e.printStackTrace();
179     }
180   }
181 }
182
Popular Tags