[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: problem deleting objects
OK, here is where I do it.. I have attached the file. The
method called on exit is deleteObjects()
If the ozone server is not shut down the object (CspV2Impl)
persists. If I shutdown ozone it is gone. Note: the error
I get is on creating the object after my game restart not
while deleting.
Tim
--
Tim Brown
URL: http://www.incenter.org
// Server code
import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import com.sun.java.swing.*;
import com.sun.java.swing.tree.*;
import DE.softwarebuero.ozone.*;
import DE.softwarebuero.DxLib.*;
public class Server extends Thread {
public final static int DEFAULT_PORT = 2030;
protected int port;
protected ServerSocket listen_socket;
protected ThreadGroup threadgroup;
protected List connection_list;
protected JLabel status;
protected Vector connections;
protected Vulture vulture;
public RemoteDatabase db;
public boolean dbStatus;
private gIndexImpl_Int gameIndex;
private commandsImpl_Int cmds;
public String gameName;
public boolean keepGoing;
private static int sessNum = 0;
public gIndexImpl_Int getGameIndex() { return gameIndex; }
public RemoteDatabase getDatabase() { return db; }
public boolean sendMail(int frnum, int fgnum,
int rnum, int gnum,
String text) {
Enumeration en = gameIndex.getRaces().elements();
int i = 0;
while(en.hasMoreElements()) {
raceImpl_Int rc = (raceImpl_Int)en.nextElement();
if(rc.getPlayerNum().intValue() == rnum) {
governorImpl_Int gv = rc.getGovernor(new Integer(gnum));
if(gv == null) {
addMsg("No such gov");
return false;
}
Integer n = gv.getMailbox().size();
mesgImpl_Int msg = null;
try {
msg = (mesgImpl_Int)db.createObject("mesgImpl", 0,
rc.getDBName() + "/m/" +
gv.getDBName() + "/" + n);
} catch(Exception e) {
addMsg("Error in " +
rc.getName() + ".sendMail()::" + e);
return false;
}
msg.setFrom("" + frnum +
"/" + fgnum);
addMsg("txt=" + text);
msg.setBody(text);
addMsg("msg=" + msg.getBody());
Date d = new Date();
msg.setDateSent(d);
msg.setDateReceived(d);
gv.addMail(msg);
return true;
}
}
addMsg("No such race");
return false;
}
// Exit with an error message, when an exception occurs.
public static void fail(Exception e, String msg) {
System.err.println(msg + ": " + e);
System.exit(1);
}
public void broadcast(governorImpl_Int gov, String msg) {
Enumeration en = connections.elements();
while(en.hasMoreElements()) {
Connection con = (Connection)en.nextElement();
con.rcvBroadcast(gov, msg);
}
}
public void notifyAll(String msg) {
Enumeration en = connections.elements();
while(en.hasMoreElements()) {
Connection con = (Connection)en.nextElement();
con.rcvNotification(msg);
}
}
public commandsImpl_Int getCommands() {
try {
commandsImpl_Int cmds = (commandsImpl_Int)db.objectForName(OZ_newgame.COMMANDS);
if(cmds == null)
cmds = (commandsImpl_Int)db.createObject("commandsImpl",
0,
OZ_newgame.COMMANDS);
return cmds;
} catch(Exception e) {
System.err.println("server.getCommands()::" + e);
return null;
}
}
private void deleteObjects() {
System.err.print("Deleteing objects:");
deleteCommands();
deleteCspV2();
System.err.println(", done");
}
private void deleteCspV2() {
try {
CspV2Impl_Int gi = (CspV2Impl_Int)db.objectForName("CSPV2");
if(gi != null)
db.deleteObject(gi);
System.err.print(" CspV2 ");
} catch(Exception e) {
System.err.println("Server.deleteCspV2()::" + e);
}
}
private void deleteCommands() {
try {
db.deleteObject(getCommands());
System.err.print(" commands ");
} catch(Exception e) {
System.err.println("server.deleteCommands()::" + e);
}
}
public void addMsg(String msg) {
try {
connection_list.add(msg);
connection_list.makeVisible(connection_list.getItemCount()-1);
} catch(Exception e) {
System.err.println("Error in Server.addMsg()::" + e);
}
}
public void logoff(raceImpl_Int race, governorImpl_Int gov, int ses) {
gameIndex.removeSession(makeSessionKey(race, gov, ses));
}
private String makeSessionKey(raceImpl_Int race, governorImpl_Int gov, int sNum) {
// session number allows the same race to login more than once
String key =
race.getDBName() + "/" +
gov.getDBName() + "/" +
sNum;
return key;
}
public int addSession(raceImpl_Int race, governorImpl_Int gov) {
try {
sessionImpl_Int sess = (sessionImpl_Int)db.createObject("sessionImpl",0,
makeSessionKey(race, gov, sessNum));
sess.setRace(race);
sess.setGovernor(gov);
sess.setDBName(makeSessionKey(race, gov, sessNum));
sess.setLoggedOnAt(new Date());
gameIndex.addSession(sess);
return sessNum++;
} catch(Exception e) {
System.err.println("Error adding session::" + e);
return -1;
}
}
public raceImpl_Int getRace(Integer raceid, String racepass, String govpass) {
String rk = makeDBNames.makeRaceDBName(gameName, raceid);
raceImpl_Int race = gameIndex.getRace(rk);
if(race == null) {
System.err.println("No race for:" + rk);
return null;
}
// now see if passwords are right
if(!race.loginGovernor(racepass, govpass).booleanValue())
return null;
return race;
}
private boolean initDB() {
if(!openDB())
return false;
// get gindex
try {
gameIndex = (gIndexImpl_Int)db.objectForName("gameIndex");
if(gameIndex == null)
return false;
gameIndex.resetSessions();
return true;
} catch(Exception e) {
System.err.println("Error::" + e);
return false;
}
}
private boolean openDB() {
// open database
db = new RemoteDatabase();
try {
// open the connection on localhost at port 3333
db.open ("localhost", 3333);
// reload our database classes if changed them
db.reloadClasses();
return true;
} catch(Exception e) {
dbStatus = false;
JOptionPane.showMessageDialog(null
,"Error opening database"
,"Database Error"
,JOptionPane.ERROR_MESSAGE);
try {
db.close();
} catch (Exception ex) {
return false;
}
return true;
}
}
private boolean closeDB() {
try {
System.err.println("Closing database");
db.close();
return true;
} catch(Exception e) {
System.err.println(e + "::Error closing database");
return false;
}
}
// Create a ServerSocket to listen for connections on; start the thread.
public Server(int port, String gname) {
// Create our server thread with a name.
super("Server");
gameName = gname;
// open database
if(!initDB()) {
System.err.println("Error opening database");
System.exit(2);
}
if (port == 0) port = DEFAULT_PORT;
this.port = port;
try { listen_socket = new ServerSocket(port); }
catch (IOException e) { fail(e, "Exception creating server socket"); }
try { listen_socket.setSoTimeout(1000); }
catch(Exception e) {;}
// Create a threadgroup for our connections
threadgroup = new ThreadGroup("Server Connections");
// Create a window to display our connections in
JFrame f = new JFrame("Server Status");
connection_list = new List();
status = new JLabel();
JButton btnQuit = new JButton("Quit");
btnQuit.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (java.awt.event.ActionEvent evt) {
btnQuitActionPerformed (evt);
}
});
f.getContentPane().add("North", btnQuit);
f.getContentPane().add("Center", connection_list);
f.getContentPane().add("South", status);
f.setSize(400, 200);
f.show();
// Initialize a vector to store our connections in
connections = new Vector();
// Create a Vulture thread to wait for other threads to die.
// It starts itself automatically.
vulture = new Vulture(this);
// Start the server listening for connections
this.start();
}
private void btnQuitActionPerformed (java.awt.event.ActionEvent evt) {
keepGoing = false;
}
// The body of the server thread. Loop forever, listening for and
// accepting connections from clients. For each connection,
// create a Connection object to handle communication through the
// new Socket. When we create a new connection, add it to the
// Vector of connections, and display it in the List. Note that we
// use synchronized to lock the Vector of connections. The Vulture
// class does the same, so the vulture won't be removing dead
// connections while we're adding fresh ones.
public void run() {
keepGoing = true;
while(keepGoing) {
try {
Socket client_socket = listen_socket.accept();
Connection c = new Connection(client_socket, threadgroup,
3, vulture);
// prevent simultaneous access.
synchronized (connections) {
connections.addElement(c);
connection_list.addItem(c.toString());
}
} catch(Exception e) {
if(!e.toString().startsWith("java.io.InterruptedIOException")) {
System.err.println(e);
break;
}
}
}
notifyAll("Going down!");
deleteObjects();
closeDB();
System.exit(0);
}
// Start the server up, listening on an optionally specified port
public static void main(String[] args) {
int port = 0;
String gname = null;
if (args.length == 1) {
try { gname = args[0]; }
catch(Exception e) {
System.err.println("Error::" + e);
System.exit(3);
}
}
System.err.println("Game:" + gname);
try { port = Integer.parseInt(args[1]); }
catch (Exception e) { port = 0; }
new Server(port, gname);
}
}
class CmdBase {
}