For school we made a Java application with RMI, there are 3 applications: Server, Client and Rekenaar.
The line where it goes wrong is the Line: "test = (Range[])m.getAllRange();", it gives an dispatchUncaughtException.
package rekenaar;
import server.Opdracht;
import java.rmi.Naming;
import java.util.logging.Level;
import java.util.logging.Logger;
import interfaces.*;
import java.rmi.RemoteException;
/**
*
* @author Windows
*/
public class Rekenaar implements Runnable{
private Range range;
private IRekenCoördinator coordinator;
private long[] priemgetallen;
private Opdracht op;
private Range[] test;
public Rekenaar()
{
try{
this.coordinator = null;
this.coordinator = (IRekenCoördinator) Naming.lookup("rmi://127.0.0.1:1099/RekenC");
this.priemgetallen = coordinator.getPriemgetallen();
this.op = null;
}catch(Exception ex){ex.printStackTrace();}
}
public void Bereken()
{
try{
long rangeOndergrens = (op.getGetal()/2000) * 2000;
range = new Range(priemgetallen, rangeOndergrens);
op.setRange(range);
op.setUitkomst(range.geefSemiPriem(op.getGetal()));
coordinator.RemoveID(op.CheckRange());
IMagazijn m = op.getMagazijn();
test = (Range[])m.getAllRange();
String hoi = m.hoi();
m.AddRange(op);
}catch(RemoteException ex){ex.printStackTrace();}
}
@Override
public void run() {
KrijgtOpdracht();
}
private void KrijgtOpdracht()
{
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Rekenaar.class.getName()).log(Level.SEVERE, null, ex);
}
if(coordinator != null)
{
try{
op = (Opdracht)coordinator.getOpdracht();
}
catch (Exception ex)
{
ex.printStackTrace();
}
if(op != null ) this.Bereken();
this.run();
}
}
}
Interface:
package interfaces;
import java.io.IOException;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
*
* @author Windows
*/
public interface IMagazijn extends Remote {
/**
*
* @param op Uitgerekende opdracht waarvan de gegevens verwerkt moeten worden op de server
* @throws IOException
*/
public String hoi() throws RemoteException;
public IRange[] getAllRange() throws RemoteException;
public void AddRange(IOpdracht op) throws RemoteException;
}
And here is the Magazijn itself which gives the IRange over RMI:
package server;
import java.rmi.RemoteException;
import java.util.ArrayList;
import interfaces.*;
import java.rmi.server.UnicastRemoteObject;
public class Magazijn extends UnicastRemoteObject implements IMagazijn{
private IRange[] ranges;
private long ondergrens;
public void setOndergrens(long ondergrens) {
this.ondergrens = ondergrens;
}
private long bovengrens;
public void setBovengrens(long bovengrens) {
this.bovengrens = bovengrens;
}
private transient MagazijnManager MM;
private transient ArrayList<Opdracht> dubbeleOpdrachten;
private transient RekenCoördinator RC;
private transient int MAGAZIJN_GROOTTE;
public Magazijn(long ondergrens, long bovengrens, MagazijnManager mm, RekenCoördinator RC)throws RemoteException
{
ranges = new IRange[(int)(bovengrens-ondergrens)/2000];
this.bovengrens = bovengrens;
this.ondergrens = ondergrens;
dubbeleOpdrachten = new ArrayList<Opdracht>();
this.MM = mm;
this.RC = RC;
MAGAZIJN_GROOTTE = (int)(bovengrens - ondergrens) + 1;
}
public void AddRange(IOpdracht op) throws RemoteException
{
ArrayList<IOpdracht> returnList = new ArrayList<IOpdracht>();
ranges[op.getRangeIndex()] = op.getRange();
returnList.add(op);
for(Opdracht o : dubbeleOpdrachten)
{
if(o.getRangeIndex() == op.getRangeIndex())
{
o.setRange(op.getRange());
o.setUitkomst(o.getRange().geefSemiPriem(o.getGetal()));
returnList.add(o);
}
}
MM.StuurOpdrachtenTerug(returnList);
}
public IRange[] getAllRange() throws RemoteException
{
return ranges;
}
public String hoi() throws RemoteException
{
return "poep";
}
public IRange getRange(int rangeIndex)
{
return ranges[rangeIndex];
}
public void StuurOpdracht(Opdracht op)
{
Opdracht o =null;
o = RC.AddOpdracht(op);
if(o!=null)
{
dubbeleOpdrachten.add(o);
}
}
public long getOndergrens()
{
return ondergrens;
}
public long getBovengrens()
{
return bovengrens;
}
public int getMagazijnGrootte()
{
return MAGAZIJN_GROOTTE;
}
}
The Rekenaar got the whole class "Range" so that is not the problem. The problem is something between the casting of IRange into Range. Range itself is Serializable and implements IRange.
The error I get: Exception in thread "main" java.lang.ClassCastException: [Linterfaces.IRange; cannot be cast to [Lrekenaar.Range;