📄 mudplace.java
字号:
/* * Copyright (c) 2000 David Flanagan. All rights reserved. * This code is from the book Java Examples in a Nutshell, 2nd Edition. * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. * You may study, use, and modify it for any non-commercial purpose. * You may distribute it non-commercially as long as you retain this notice. * For a commercial use license, or to purchase the book (recommended), * visit http://www.davidflanagan.com/javaexamples2. */package com.davidflanagan.examples.rmi;import java.rmi.*;import java.rmi.server.*;import java.rmi.registry.*;import java.io.*;import java.util.*;import com.davidflanagan.examples.rmi.Mud.*;/** * This class implements the RemoteMudPlace interface and exports a * bunch of remote methods that are at the heart of the MUD. The * MudClient interacts primarily with these methods. See the comment * for RemoteMudPlace for an overview. * The MudPlace class is Serializable so that places can be saved to disk * along with the MudServer that contains them. Note, however that the * names and people fields are marked transient, so they are not serialized * along with the place (because it wouldn't make sense to try to save * RemoteMudPerson objects, even if they could be serialized). **/public class MudPlace extends UnicastRemoteObject implements RemoteMudPlace, Serializable{ String placename, description; // information about the place Vector exits = new Vector(); // names of exits from this place Vector destinations = new Vector(); // where the exits go to Vector things = new Vector(); // names of things in this place Vector descriptions = new Vector(); // descriptions of those things transient Vector names = new Vector(); // names of people in this place transient Vector people = new Vector(); // the RemoteMudPerson objects MudServer server; // the server for this place /** A no-arg constructor for de-serialization only. Do not call it */ public MudPlace() throws RemoteException { super(); } /** * This constructor creates a place, and calls a server method * to register the object so that it will be accessible by name **/ public MudPlace(MudServer server, String placename, String description) throws RemoteException, PlaceAlreadyExists { this.server = server; this.placename = placename; this.description = description; server.setPlaceName(this, placename); // Register the place } /** This remote method returns the name of this place */ public String getPlaceName() throws RemoteException { return placename; } /** This remote method returns the description of this place */ public String getDescription() throws RemoteException { return description; } /** This remote method returns a Vector of names of people in this place */ public Vector getNames() throws RemoteException { return names; } /** This remote method returns a Vector of names of things in this place */ public Vector getThings() throws RemoteException { return things; } /** This remote method returns a Vector of names of exits from this place*/ public Vector getExits() throws RemoteException { return exits; } /** * This remote method returns a RemoteMudPerson object corresponding to * the specified name, or throws an exception if no such person is here **/ public RemoteMudPerson getPerson(String name) throws RemoteException, NoSuchPerson { synchronized(names) { // What about when there are 2 of the same name? int i = names.indexOf(name); if (i == -1) throw new NoSuchPerson(); return (RemoteMudPerson) people.elementAt(i); } } /** * This remote method returns a description of the named thing, or * throws an exception if no such thing is in this place. **/ public String examineThing(String name) throws RemoteException, NoSuchThing { synchronized(things) { int i = things.indexOf(name); if (i == -1) throw new NoSuchThing(); return (String) descriptions.elementAt(i); } } /** * This remote method moves the specified RemoteMudPerson from this place * in the named direction (i.e. through the named exit) to whatever place * is there. It throws exceptions if the specified person isn't in this * place to begin with, or if they are already in the place through the * exit or if the exit doesn't exist, or if the exit links to another MUD * server and the server is not functioning. **/ public RemoteMudPlace go(RemoteMudPerson who, String direction) throws RemoteException, NotThere, AlreadyThere, NoSuchExit, LinkFailed { // Make sure the direction is valid, and get destination if it is Object destination; synchronized(exits) { int i = exits.indexOf(direction); if (i == -1) throw new NoSuchExit(); destination = destinations.elementAt(i); } // If destination is a string, it is a place on another server, so // connect to that server. Otherwise, it is a place already on this // server. Throw an exception if we can't connect to the server. RemoteMudPlace newplace; if (destination instanceof String) { try { String t = (String) destination; int pos = t.indexOf('@'); String url = t.substring(0, pos); String placename = t.substring(pos+1); RemoteMudServer s = (RemoteMudServer) Naming.lookup(url); newplace = s.getNamedPlace(placename); } catch (Exception e) { throw new LinkFailed(); } } // If the destination is not a string, then it is a Place else newplace = (RemoteMudPlace) destination; // Make sure the person is here and get their name. // Throw an exception if they are not here String name = verifyPresence(who); // Move the person out of here, and tell everyone who remains about it. this.exit(who, name + " has gone " + direction); // Put the person into the new place. // Send a message to everyone already in that new place String fromwhere; if (newplace instanceof MudPlace) // going to a local place fromwhere = placename; else fromwhere = server.getMudName() + "." + placename; newplace.enter(who, name, name + " has arrived from: " + fromwhere); // Return the new RemoteMudPlace object to the client so they // know where they are now at. return newplace; } /** * This remote method sends a message to everyone in the room. Used to * say things to everyone. Requires that the speaker be in this place. **/ public void speak(RemoteMudPerson speaker, String msg) throws RemoteException, NotThere { String name = verifyPresence(speaker); tellEveryone(name + ":" + msg); } /** * This remote method sends a message to everyone in the room. Used to * do things that people can see. Requires that the actor be in this place. **/ public void act(RemoteMudPerson actor, String msg) throws RemoteException, NotThere { String name = verifyPresence(actor); tellEveryone(name + " " + msg); } /** * This remote method creates a new thing in this room. * It requires that the creator be in this room. **/ public void createThing(RemoteMudPerson creator, String name, String description) throws RemoteException, NotThere, AlreadyThere { // Make sure the creator is here String creatorname = verifyPresence(creator); synchronized(things) { // Make sure there isn't already something with this name. if (things.indexOf(name) != -1) throw new AlreadyThere(); // Add the thing name and descriptions to the appropriate lists things.addElement(name); descriptions.addElement(description); } // Tell everyone about the new thing and its creator tellEveryone(creatorname + " has created a " + name); } /** * Remove a thing from this room. Throws exceptions if the person * who removes it isn't themselves in the room, or if there is no * such thing here. **/ public void destroyThing(RemoteMudPerson destroyer, String thing) throws RemoteException, NotThere, NoSuchThing { // Verify that the destroyer is here String name = verifyPresence(destroyer); synchronized(things) { // Verify that there is a thing by that name in this room int i = things.indexOf(thing); if (i == -1) throw new NoSuchThing(); // And remove its name and description from the lists things.removeElementAt(i); descriptions.removeElementAt(i);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -