?? client.java
字號(hào):
package com.wiley.compBooks.roman.session.fazuul;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.*;
import java.io.*;
/**
* Client for Fazuul Game.
*/
public class Client {
public static void main(String[] args) {
new Client().start();
}
// Current list of Component EJB Objects I own
private Vector components = new Vector();
// A Machine which dispenses Component EJB Objects
private Machine machine;
/**
* Starts the game. The main game loop is here.
*/
public void start() {
try {
/*
* Get System properties for JNDI initialization
*/
Properties props = System.getProperties();
/*
* Get a reference to the MachineHome Object - the
* factory for Machine EJB Objects
*/
Context ctx = new InitialContext(props);
MachineHome home = (MachineHome) ctx.lookup("MachineHome");
/*
* Use the factory to create the Machine EJB Object
*/
machine = home.create();
}
catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
/*
* Start reading input from standard input
*/
String line = null, command = null, args = null;
StringTokenizer tokens = null;
BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));
while (true) {
/*
* Print prompt, read next input line, and get command
*/
try {
System.out.println();
System.out.print("> ");
line = reader.readLine();
System.out.println();
tokens = new StringTokenizer(line, " ", false);
// Get command. e.g. "attach" or "inv"
command = tokens.nextToken();
// Get arguments to command.
args = null;
if (tokens.hasMoreElements()) {
args = line.substring(command.length()+1, line.length());
}
}
catch (Exception e) {
continue;
}
/*
* Do case analysis based upon command.
*/
try {
/*
* If the user wants to examine a component
*/
if (command.equals("examine")) {
examine(args);
}
/*
* If user wants to attach 2 components,
* then extract the 2 component names from
* the argument string, and call attach()
*/
else if (command.equals("attach")) {
String item1 = null, item2 = null;
try {
StringTokenizer argtokens = new StringTokenizer(args, " ");
item1 = argtokens.nextToken();
argtokens.nextToken();
item2 = argtokens.nextToken();
}
catch (Exception e) {
throw new Exception("Syntax: attach <item1> to <item2>");
}
attach(item1, item2);
}
/*
* If the user wants to discard an object
*/
else if (command.equals("drop")) {
drop(args);
}
/*
* If the user needs more components
*/
else if (command.equals("gimme")) {
gimme();
}
/*
* If the user wants to list the components
* he has.
*/
else if (command.equals("inv")) {
inv();
}
/*
* If the user wants to end the game
*/
else if (command.equals("quit")) {
quit();
}
/*
* If the user wants to suspend the game
*/
else if (command.equals("suspend")) {
if (args == null) {
System.out.println("Please specify a filename.");
}
else {
suspend(args);
}
}
/*
* If the user wants to resume a suspended
* game.
*/
else if (command.equals("resume")) {
if (args == null) {
System.out.println("Please specify a filename.");
}
else {
resume(args);
}
}
else {
System.out.println("Syntax: [attach <item1> to <item2> | examine <item> | inv | gimme | drop <item> | suspend <filename> | resume <filename> | quit]");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Suspends the game. Writes the current game state to disk
* via EJB object handles.
*/
private void suspend(String filename) {
ObjectOutputStream stream = null;
try {
stream = new ObjectOutputStream(
new FileOutputStream(filename));
for (int i=0; i < components.size(); i++) {
Component comp = (Component) components.elementAt(i);
Handle handle = comp.getHandle();
stream.writeObject(handle);
}
stream.flush();
System.out.println("Game saved.");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (stream != null) {
try { stream.close(); } catch (Exception e) {}
}
}
System.exit(0);
}
/**
* Resumes a suspended game via EJB object handles.
*/
private void resume(String filename) {
clearComps();
ObjectInputStream stream = null;
try {
stream = new ObjectInputStream(
new FileInputStream(filename));
while (true) {
Handle handle = (Handle) stream.readObject();
components.addElement(
(Component) handle.getEJBObject());
}
}
catch (EOFException e) {
System.out.println("Game loaded.");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (stream != null) {
try { stream.close(); } catch (Exception e) {}
}
}
}
/*
* Removes all components the user has.
*/
private void clearComps() {
System.out.println("Clearing game state...");
for (int i=0; i < components.size(); i++) {
try {
Component comp =
(Component) components.elementAt(i);
comp.remove();
}
catch (Exception e) {
e.printStackTrace();
}
}
components = new Vector();
}
/**
* Quits the game, cleaning up all EJB Objects.
*/
private void quit() {
/*
* 1: Remove all components
*/
clearComps();
/*
* 2: Remove machine
*/
try {
machine.remove();
}
catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
/**
* Gets more components from the machine
*/
private void gimme() throws Exception {
for (int i=0; i < 3; i++) {
Component comp = machine.makeComponent();
components.addElement(comp);
System.out.println("The machine pops out a " + comp.getName());
}
}
/**
* Drops a component.
*/
private void drop(String componentName) throws Exception {
for (int i=0; i < components.size(); i++) {
Component comp = (Component) components.elementAt(i);
if (comp.getName().equals(componentName)) {
// Call remove() on EJB Object
comp.remove();
components.removeElement(comp);
System.out.println("You dropped your " + componentName);
return;
}
}
}
/**
* Lists the inventory of components I currently have.
*/
private void inv() throws Exception {
for (int i=0; i < components.size(); i++) {
Component comp = (Component) components.elementAt(i);
System.out.println(comp.getName());
}
}
/**
* Prints out the description of component with name componentName.
*/
private void examine(String componentName) throws Exception {
/*
* Loop through all the components. Get the names of each
* component. If the name matches, then print out that
* component's description.
*/
for (int i=0; i < components.size(); i++) {
Component comp = (Component) components.elementAt(i);
if (comp.getName().equals(componentName)) {
System.out.println(comp.getDescription());
return;
}
}
}
/**
* Attempts to attach components with names A and B together.
*/
private void attach(String componentNameA, String componentNameB) throws Exception {
Component componentA = null, componentB = null;
/*
* Loop through all the components. Get the Names of each
* component. If the name matches, then that's our Component A.
*/
for (int i=0; i < components.size(); i++) {
Component comp = (Component) components.elementAt(i);
if (comp.getName().equals(componentNameA)) {
componentA = comp;
}
}
/*
* Print out some error codes if we didn't find any matches
*/
if (componentA == null) {
System.out.println("You don't have a " + componentNameA);
return;
}
/*
* Loop through all the components. Get the Names of each
* component. If the name matches, AND it's not the same
* as the Component A above, then that's our Component B.
*/
for (int i=0; i < components.size(); i++) {
Component comp = (Component) components.elementAt(i);
if (comp.getName().equals(componentNameB)
&& (!comp.equals(componentA))) {
componentB = comp;
}
}
/*
* Print out some error codes if we didn't find any matches
*/
if (componentB == null) {
System.out.println("You don't have a " + componentNameB);
return;
}
/*
* Try to attach the two components. If they attach,
* 1) Remove the old Component EJB Objects
* 2) Remove the old Components from our list of components
* 3) Add the new, combined component to our list
*/
try {
Component newComp = componentA.attachTo(componentB);
// Remove the two old Components' EJB Object
componentA.remove();
componentB.remove();
components.removeElement(componentA);
components.removeElement(componentB);
components.addElement(newComp);
System.out.println("Fitting the " + componentNameA + " into the " + componentNameB + ", out pops a " + newComp.getName() + "!");
}
/*
* If an application-level exception occurs (i.e. if
* the two components won't attach) then handle it.
* Let the main loop handle system-level exceptions.
*/
catch (ComponentException e) {
System.out.println(e.toString());
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -