001: /*
002: * @(#)PlaneServer.java 1.3 03/01/16
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package IXCDemo.ixcXlets.serverXlet;
029:
030: import javax.microedition.xlet.Xlet;
031: import javax.microedition.xlet.XletContext;
032: import javax.microedition.xlet.ixc.IxcRegistry;
033: import java.rmi.AlreadyBoundException;
034: import java.rmi.RemoteException;
035: import java.rmi.AccessException;
036:
037: public class PlaneServer implements Xlet, Runnable {
038: private XletContext context;
039: private boolean running = false;
040: private boolean pleaseStop = false;
041: private boolean started = false;
042: private PlaneImpl planeImpl = new PlaneImpl();
043:
044: public PlaneServer() {
045: System.out.println("server: constructed");
046: }
047:
048: public void initXlet(XletContext context) {
049: this .context = context;
050: }
051:
052: public void startXlet() {
053: System.out.println("PlaneServer was started.");
054: boolean wasStarted;
055: synchronized (this ) {
056: pleaseStop = false;
057: if (running) {
058: return;
059: }
060: running = true;
061: wasStarted = started;
062: if (!started) {
063: started = true;
064: }
065: }
066: Thread t = new Thread(this );
067: t.setDaemon(true);
068: t.start();
069: if (!wasStarted) {
070: try {
071: IxcRegistry r = IxcRegistry.getRegistry(context);
072: r.bind("", planeImpl);
073: r.bind("other", new PlaneImpl());
074: } catch (AlreadyBoundException ex) {
075: System.out.println("Server was already registered.");
076: ex.printStackTrace();
077: context.notifyDestroyed();
078: return;
079: } catch (RemoteException re) {
080: System.out.println("Server cannot contact Registry");
081: re.printStackTrace();
082: context.notifyDestroyed();
083: return;
084: }
085: }
086: }
087:
088: public void pauseXlet() {
089: stopMe();
090: }
091:
092: public void destroyXlet(boolean unconditional) {
093: stopMe();
094: }
095:
096: private void stopMe() {
097: synchronized (this ) {
098: pleaseStop = true;
099: notifyAll();
100: }
101: }
102:
103: public void run() {
104: for (;;) {
105: synchronized (this ) {
106: if (pleaseStop) {
107: running = false;
108: return;
109: }
110: try {
111: wait(2000);
112: } catch (InterruptedException ex) {
113: Thread.currentThread().interrupt();
114: return; // We're being killed; bail out
115: }
116: }
117: if (!pleaseStop) {
118: planeImpl.tick();
119: }
120: }
121: }
122: }
|