KickJava   Java API By Example, From Geeks To Geeks.

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


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 Receptionnist implements org.objectweb.proactive.RunActive {
36     
37     static Logger logger = Logger.getLogger(Receptionnist.class.getName());
38
39   public final static int NO_ONE = -1;
40   int pat_id;
41   int doc_id;
42   Office off;
43
44
45   public Receptionnist() {
46   }
47
48
49   public Receptionnist(Office _off) {
50     off = _off;
51     doc_id = pat_id = NO_ONE;
52   }
53
54
55   public synchronized void addPatient(int pat) {
56     if (pat_id != NO_ONE) {
57       logger.error("ERROR: addPatient(" + pat + ") with pat_id=" + pat_id);
58       System.exit(0);
59     }
60     pat_id = pat;
61     if (doc_id != NO_ONE) {
62       off.doctorWithPatient(doc_id, pat_id);
63       doc_id = pat_id = NO_ONE;
64     }
65   }
66
67
68   public synchronized void addDoctor(int doc) {
69     if (doc_id != NO_ONE) {
70       logger.error("ERROR: addDoctor(" + doc + ") with doc_id=" + doc_id);
71       System.exit(0);
72     }
73     doc_id = doc;
74     if (pat_id != NO_ONE) {
75       off.doctorWithPatient(doc_id, pat_id);
76       doc_id = pat_id = NO_ONE;
77     }
78   }
79
80
81   public synchronized boolean doctorWaiting() {
82     return ((doc_id != NO_ONE) && (pat_id == NO_ONE));
83   }
84
85
86   public synchronized boolean patientWaiting() {
87     return ((pat_id != NO_ONE) && (doc_id == NO_ONE));
88   }
89
90
91   public void runActivity(org.objectweb.proactive.Body body) {
92     org.objectweb.proactive.Service service = new org.objectweb.proactive.Service(body);
93     while (body.isActive()) {
94       if (doctorWaiting())
95         service.blockingServeOldest("addPatient");
96       if (patientWaiting())
97         service.blockingServeOldest("addDoctor");
98       if ((!doctorWaiting()) && (!patientWaiting()))
99         service.blockingServeOldest();
100     }
101   }
102 }
103
Popular Tags