Problems with my rmi program is that when i send an echo message i get a null value but i initialize all the values. it seems like it does not register the values that i have put in for it.
Server
package server;
import interfaces.Compute;
import interfaces.Pi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class ComputeEngine extends UnicastRemoteObject implements Compute {
protected ComputeEngine() throws RemoteException {
super();
}
public String executeTask(Pi t){
System.out.println(t.message);
return t.execute();
}
public String executeTask2(Pi t) {
return t.execute2();
}
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
String name = "Echo";
Compute engine = new ComputeEngine();
Naming.rebind(name, engine);
name = "Compute";
Naming.bind(name, engine);
System.out.println("System bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception:");
e.printStackTrace();
}
}
}
Client
package client;
import interfaces.Compute;
import interfaces.Pi;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
public class Client {
Scanner scan = new Scanner(System.in);
int v1 = 0, v2 = 0;
String echoMessage = null;
public static void main(String args[]) {
Client e = new Client();
String name, output;
Registry registry;
Compute comp;
Pi task;
//System.setProperty("java.security.policy","file:./security.policy");
//if (System.getSecurityManager() == null) {
// System.setSecurityManager(new SecurityManager());
//}
System.out.println("Enter something: ");
String input = e.scan.nextLine();
int temp = e.processInput(input);
try {
switch(temp){
case 0:
name = "Echo";
registry = LocateRegistry.getRegistry();
comp = (Compute) registry.lookup(name);
task = new Pi(e.echoMessage);
output = comp.executeTask(task);
System.out.println(output);
break;
case 1:
name = "Compute";
registry = LocateRegistry.getRegistry();
comp = (Compute) registry.lookup(name);
task = new Pi(e.v1,e.v2);
output = comp.executeTask2(task);
System.out.println(output);
break;
default:
break;
}
} catch (Exception e1) {
System.err.println("ComputePi exception:");
e1.printStackTrace();
}
}
private int processInput(String input){
String temp = input.toUpperCase();
String arr[];
if(temp.startsWith("ECHO")){
arr = input.split(" ", 2);
echoMessage = arr[1];
return 0;
}
else if(Character.isDigit(temp.charAt(0))){
arr = input.split(" ");
v1 = Integer.parseInt(arr[0]);
v2 = Integer.parseInt(arr[2]);
return 1;
}
return -1;
}
}
Interface
package interfaces;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Remote {
String executeTask(Pi t) throws RemoteException;
String executeTask2(Pi t) throws RemoteException;
}
Remote object
package interfaces;
import java.io.Serializable;
public class Pi implements Serializable {
private static final long serialVersionUID = 227L;
public static String message = null;
private static int value1 = 0, value2= 0;
public Pi(String mess) {
this.message = mess;
}
public Pi(int v1, int v2) {
this.value1 = v1;
this.value2 = v2;
}
public String execute() {
return returnEcho(message);
}
public String execute2(){
return addNumbers(value1, value2);
}
public static String addNumbers(int v1, int v2){
int total = v1 + v2;
return Integer.toString(total);
}
public static String returnEcho(String n1) {
return n1;
}
}
The symptom you describe in your comment is quite impossible.
Possibly you really mean that the fields of 't' are null, which is explained by the fact that they are static and therefore not serialized with the object.
NB The remote object in this system is not Pi but ComputeEngine.