Serializable

C

Colin Hemmings

Hi there I posted a message up yesterday about serialisable. That is now
working but I now have a problem with a different class.

I have a class 'ServerImp' which contains and arraylist of 'remoteUser'
each user then has one 'RemoteWatchList'

I try using serializable to save the arraylist of remoteusers and it
seems to doe this fine and also seems to load fine.

The problem occurs when I try to access a users watchlist after it has
been reloaded but it comes up with connection 'refused'. it seems as
though the program knows the watchlist is there but has lost the pointer
to it.

I cannot for the life of me find the error. I have attached the classes
for the three classes i mentioned, if someone could please help?


I thankyou for your time

import java.rmi.*;
import java.util.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.io.*;


public class ServerImp extends UnicastRemoteObject implements Server
{
private ArrayList users = new ArrayList();
private ArrayList stocks = new ArrayList();

//DELEGATED OBSERVABLE
private DelegatedObservable obs;

/** Creates a new instance of RemoteServerImp */
public ServerImp() throws RemoteException
{
super();
System.out.println("Super class constructor loaded");

obs = new DelegatedObservable(); //DELEGATED OBSERVER

//READ IN SAVED USER AND STOCK FILES
readUserList();
readStockList();

}

/*************REMOTE METHODS****************/

public RemoteUser getUser(String uName) throws RemoteException
{
System.out.println("RETRIEVING USER: "+uName);
int id = findUser(uName);
RemoteUser temp = ((RemoteUser) users.get(id));
return temp;
}



public void addUser(String uName, String rName, String pwd, String em) throws RemoteException
{
int size = users.size();
RemoteUser u = new RemoteUserImp(uName, rName, pwd, em);

//make the new user's watchlist an observer of the server
obs.addObserver((Observer) u.getWatchList());

//then add user to the users array
users.add(u);
System.out.println("ADDING USER: "+size+": "+uName+" , "+rName+" , "+pwd+" , "+em);
//UPDATE USERS LIST
writeUserList();

}

public void removeUser(String uName) throws RemoteException
{
System.out.println("REMOVING USER: "+uName);
users.remove(findUser(uName));
//UPDATE USERS LIST
writeUserList();
}

public boolean userExist(String userName) throws RemoteException
{
boolean valid = false;
String temp = null;
for (int x=0; x<(users.size()); x++)
{
temp = ((RemoteUser)users.get(x)).getUserName();
if (userName.equals(temp))
{
valid = true;
}

}
return valid;
}

public boolean checkPsw (String uName, String psw) throws RemoteException
{
int userPos = findUser(uName);
RemoteUser tempUser;
String realPsw = ((RemoteUser)users.get(userPos)).getPsw();

if (realPsw.equals(psw))
{
return true;
}
else
{
return false;
}

}

public int numUsers() throws RemoteException
{
return users.size();
}
public RemoteUser getUserAt(int index) throws RemoteException
{
return (RemoteUser) users.get(index);
}



///TERMINATE USER
public void terminateUser (String userName) throws RemoteException
{
users.remove(findUser(userName));
}



///STOCK METHODS
public RemoteStock getStock(String symbol) throws RemoteException
{
System.out.println("RETRIEVING STOCK: "+symbol);
return (RemoteStock) stocks.get(findStock(symbol));
}

public void removeStock(String symbol) throws RemoteException
{
//OBSERVER STUFF
obs.setChanged();
obs.notifyObservers(new String(symbol)); //tell observers that stocks have been deleted, by sending the stock symbol

System.out.println("REMOVING STOCK: "+symbol);
stocks.remove(findStock(symbol));
//UPDATE USERS LIST
writeStockList();
}

public void addStock(String symbol, String name, String company, float price) throws RemoteException
{
RemoteStock u = new RemoteStockImp(symbol, name, company, price);
stocks.add(u);
System.out.println("ADDING STOCK: "+symbol+": "+name+" , "+company+" , "+price);
//UPDATE USERS LIST
writeStockList();
}


public boolean stockExist (String symbol) throws RemoteException
{
boolean stockexist = false;

for (int i=0; i < stocks.size() ; i ++)
{
String temp = ((RemoteStock) stocks.get(i)).getSymbol();
if (symbol.equals(temp))
{
stockexist = true;
}
}

return stockexist;

}
public int numStocks () throws RemoteException
{
return stocks.size();
}

public RemoteStock getStockAt(int index) throws RemoteException
{
return (RemoteStock) stocks.get(index);
}

/************LOCAL METHODS******************/

public int findUser(String userName) throws RemoteException
{
String temp;
int element = 0;
for (int x=0; x<(users.size()); x++)
{
temp = ((RemoteUser)users.get(x)).getUserName();
if (userName.equals(temp))
{
element = x;

}
}

return element;
}

public int findStock(String symbol) throws RemoteException
{
String temp;
int element = 0;
for (int x=0; x<(stocks.size()); x++)
{
temp = ((RemoteStock)stocks.get(x)).getSymbol();
if (temp.equals(symbol))
{
element = x;

}
}

return element;
}



public static void main(String[] args)
{
try
{
Server s = new ServerImp();
Naming.rebind("SERVER", s);
System.out.println("name 'SERVER' bound to the server");

}
catch (IOException e)
{
System.out.println("IOException " + e.getMessage());
}
System.out.println("===============");
System.out.println("Awaiting operations...");

//PriceChangeFeed priceFeed = new PriceChangeFeed();

}

/************************SERIALIZABLE RELATED METHODS****************/
//////WRITE AND READ METHODS FOR USERS
public void writeUserList()
{
try
{
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("userlist.xxx"));
out.writeObject(users);
out.close();
System.out.println("UserList Saved");

}
catch (IOException e)
{
System.out.println (e);
}
}


public void readUserList()
{
try
{
ObjectInputStream in = new ObjectInputStream( new FileInputStream("userlist.xxx"));
users = (ArrayList)in.readObject();
in.close();
System.out.println("UserList Loaded");

}
catch (Exception e)
{
System.out.println (e);
}
}

//////WRITE AND READ METHODS FOR STOCKS
public void writeStockList()
{
//
try
{
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("stocklist.xxx"));
out.writeObject(stocks);
out.close();
System.out.println("StockList Saved");

}
catch (IOException e)
{
System.out.println (e);
}
}


public void readStockList()
{
try
{
ObjectInputStream in = new ObjectInputStream( new FileInputStream("stocklist.xxx"));
stocks = (ArrayList)in.readObject();
in.close();
System.out.println("StockList Loaded");

}
catch (Exception e)
{
System.out.println (e);
}
}

/************************OBSERVABLE METHODS****************/
public void addObserver(Observer o) throws RemoteException
{
obs.addObserver(o);
}

public void deleteObserver(Observer o) throws RemoteException
{
obs.deleteObserver(o);
}




}





import java.rmi.*;
import java.rmi.server.*;
import java.io.*;




public class RemoteUserImp extends UnicastRemoteObject implements RemoteUser
{
private String userName,
name,
password,
email = null;

private RemoteWatchList watchList = new RemoteWatchListImp();

/** Creates a new instance of RemoteUserImp */
public RemoteUserImp(String uName, String rName, String pwd, String em) throws RemoteException
{
this.userName = uName;
this.name = rName;
this.password = pwd;
this.email = em;
}

/******************** REMOTE METHODS*******************/

public String getDetails() throws RemoteException
{
return (userName+" , "+name+" , "+email);
}

public String getUserName () throws RemoteException
{
return userName;
}

public String getName () throws RemoteException
{
return name;
}

public String getPsw () throws RemoteException
{
return password;
}

public RemoteWatchList getWatchList() throws RemoteException
{
return watchList;
}

public void editUser (String uName, String rName, String psw, String em) throws RemoteException
{
//if field not null, change it
if (uName.length() != 0)
{
userName = uName;
}

//if field not null, change it
if (rName.length() != 0)
{
name = rName;
}

//if field not null, change it
if (psw.length() != 0)
{
password = psw;
}

//if field not null, change it
if (em.length() != 0)
{
email = em;
}
}


/******************* LOCAL METHODS *********************/

public void editName (String newname)
{
name = newname ;
}

public void editUserName (String newusername) throws RemoteException
{
userName = newusername ;
}

public void editPassword (String newpwd) throws RemoteException
{
password = newpwd;
}
public void editEmail (String newemail) throws RemoteException
{
email = newemail;
}


}

import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.io.*;

public class RemoteWatchListImp extends UnicastRemoteObject implements RemoteWatchList, Observer
{
ArrayList wStocks = new ArrayList();

public RemoteWatchListImp() throws RemoteException
{
}

/***********************REMOTE METHODS************************/

public RemoteStock getStock(String symbol) throws RemoteException
{
int id = findStock(symbol);
return (RemoteStock)wStocks.get(id);
}

public RemoteStock get(int index) throws RemoteException
{
return (RemoteStock)wStocks.get(index);
}

public int getSize() throws RemoteException
{
return wStocks.size();
}

public void addStock(RemoteStock stockItem) throws RemoteException
{
wStocks.add(stockItem);


}

public void removeStock(String symbol) throws RemoteException
{
int id = findStock(symbol);
wStocks.remove(id);
}

public boolean stockExist (String symbol) throws RemoteException
{
boolean stockexist = false;

for (int i=0; i < wStocks.size() ; i ++)
{
String temp = ((RemoteStock) wStocks.get(i)).getSymbol();
if (symbol.equals(temp))
{
stockexist = true;
}
}

return stockexist;
}

/***********************LOCAL METHODS*************************/

public int findStock(String symbol) throws RemoteException
{
String temp;
int element = 0;
for (int x=0; x<(wStocks.size()); x++)
{
temp = ((RemoteStock)wStocks.get(x)).getSymbol();
if (symbol == temp)
{
element = x;

}
}

return element;
}










/**************OBSERVERABLE METHODS***********************/

public void update(Observable obj, Object arg)
{
String symbol = null;
//CHECK observable object is a string
if ((arg instanceof String)) { symbol = (String)arg;}
{
try
{
//for the contents of the watchlist
for (int i = 0; i < wStocks.size() ; i ++ )
{
//if stock symbol is in watchlist
if (symbol.equals (((RemoteStock) wStocks.get(i)).getSymbol()))
{
//remove it from the watchlist
wStocks.remove(i);
System.out.println("Stock " + symbol + " at " + i + " has been removed from a watchlist" );
}
}
}
catch (RemoteException e)
{
System.out.println("");
}
}

}















/************************SERIALIZABLE RELATED METHODS****************/
//////WRITE AND READ METHODS FOR USERS
/**
public void writeWatchList()
{
try
{
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("watchlist.dat"));
out.writeObject(wStocks);
out.close();

}
catch (IOException e)
{
System.out.println (e);
}
}


public void readWatchList()
{
try
{
ObjectInputStream in = new ObjectInputStream( new FileInputStream("watchlist.dat"));
wStocks = (ArrayList)in.readObject();
in.close();

}
catch (Exception e)
{
System.out.println (e);
}
}
**/


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top