RMI Login and Threads
Hallo
i磎 new to RMI. I磛e made the RMI Tutorial from SUN. I have first 2 questions:
1)
If more clients connect to the server. Becomes every client automaticaly his own thread thread? Or must i implement "thread programming"?
2)
I want to realise user autentification by ID and Password. Is there a "simple" solution for this problem - perhabs a tutorial? Or how can i do this? How can i ensure the methods, the whole server?
Best regards
cu
Heidi
# 1
> If more clients connect to the server. Becomes every
> client automaticaly his own thread thread? Or must i
> implement "thread programming"?
Neither of the above. You can't assume anything about threading in an RMI server, but this implies that the one thing you can't assume is that it is single threaded. So you have to assume it is multithreaded and synchronize your server code accordingly. You don't have to implement any threading yourself.
> 2)
> I want to realise user autentification by ID and
> Password. Is there a "simple" solution for this
> problem - perhabs a tutorial? Or how can i do this?
> How can i ensure the methods, the whole server?
The session pattern:
public interface RemoteLogin implements Remote
{
Session login(String userid, String password) throws RemoteException;
}
public interface Session implements Remote
{
// your methods here
}
The only object you bind to the Registry is the Login object, and the implementation of login() returns a new SessionImpl object. That way nobody can even see your application methods without executing a remote login first.
ejpa at 2007-7-11 23:40:29 >

# 2
Hello,
first of all: thank you very much for your help.
But how must i implement the method "RemoteLogin"? The idea is great. But i have no idea how the method must look like?
I tried the following:
the interface "RemoteLogin":
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteLogin extends Remote
{
Hello login(String userid, String password) throws RemoteException;
}
the interface "Hello":
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
Long getDatGroesse(String datei) throws RemoteException;
String getEndlosschleife() throws RemoteException;
}
the method "login":
public Hello login(String userid, String password) {
Hello hl = null;
return hl;
}
[/code
[b]thats the Main (startint the server):[/b]
[code]
Server obj = new Server();
RemoteLogin stub = (RemoteLogin) UnicastRemoteObject.exportObject(obj, 0);
// Bind the remote object's stub in the registry
// Registry registry = LocateRegistry.getRegistry();
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("RemoteLogin", stub);
System.err.println("Server ready");
I get the following error message:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
Long getDatGroesse(String datei) throws RemoteException;
String getEndlosschleife() throws RemoteException;
}
How can i do that?
Best regards
Heidi
# 3
login() must validate the userid and password and return a new HelloImpl() or Server() or whatever your implementation of Hello is. This is the user's 'session' object and it will probably contain the userid and other stuff about the user. Not the password though.
I didn't see a 'following error message' but your code looks OK to me.
ejpa at 2007-7-11 23:40:29 >

# 4
Hello,
first of all: thank you very much for your help. But i don遲 understand the "logic" and the use of the classes.
This is my server:
public class Server implements RemoteLogin {
public Server() {
/* Konstruktor */
}
public static void main(String args[]) {
try {
Server obj = new Server();
RemoteLogin stub = (RemoteLogin) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("RemoteLogin", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
public MySaveMethods login(String userid, String password) {
MySaveMethods hl = null;
hl = new MySaveMethods();
return hl;
}
}
this is the class with the methods i want to protect:
public class MySaveMethods implements Serializable {
private static final long serialVersionUID = -3790762653917500433;
public MySaveMethods() {
super();
}
public String sayHello() {
return "Hello, world!";
}
public String getEndlosschleife() {
long i = 0;
for (int j = 0; j < 5; j++) {
i = 0;
while (i < 1111106011) {
i++;
}
}
return "32 Sekunden gewartet";
}
public Long getDatGroesse(String datei) {
File myFile = new File(datei);
System.out.println("Die Dateigroesse betraegt am Server: "
+ myFile.length() + " Byte");
return myFile.length();
}
}
that is the interface:
public interface RemoteLogin extends Remote
{
MySaveMethods login(String userid, String password) throws RemoteException;
}
Is this ok for the server? If yes, how must the client looks like? I have no idea how to use the interface. Because i have no class "MySaveMethods" at the client. Or must i also create a class "MySaveMethods" at the client?
i tried the following for the client:
I implemented the same interface "RemoteLogin"
public interface RemoteLogin extends Remote
{
MySaveMethods login(String userid, String password) throws RemoteException;
}
--> error: the class "MySaveMethods " cannot be resolved.
the client:
public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("192.168.0.2");
RemoteLogin stub = (RemoteLogin) registry.lookup("RemoteLogin");
MySaveMethods response = stub.login("username", "12345");
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
--> also error because: MySaveMethods ...
Where is my error in reasoning? How can i do that?
Can you or someone else give me an example for this problem please? If it磗 easier for you to send me a complete project to my email, then i will post the solution in the forum. My eMail: heidiweber@freenet.de
Thank you very much
see you
Heidi
# 5
Here's another crack at the classes you need. I will make up what might be reasonable class names.
1. Some kind of an Accessor.
o gets instantiated once.
o gets registered with the rmiregistry.
o implements UnicastRemoteObject.
o instantiates and keeps a reference to a Server.
o has a login() method that checks user ID and password, and if OK,
returns to the caller a reference to Server.
2. Server is the thing that does real work, which only an authenticated user can access.
o Implements UnicastRemoteObject.
3. Client
o Looks up the Accessor and send a username and password.
o gets back a reference to a Server.
o Makes remote calls on the Server.