Download Java RMI

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Dynamic Host Configuration Protocol wikipedia , lookup

Remote Desktop Services wikipedia , lookup

Lag wikipedia , lookup

Transcript
CS603
Communication Mechanisms:
DCE RPC (cont.)
23 January 2002
Sample IDL for
message/reply RPC
/* greet.idl
* The "greet" interface. */
[uuid(3d6ead56-06e3-11ca-8dd1-826901beabcd), version(1.0)]
/* import “like_c_includes.idl” */
/* type declarations (typedef) */
interface greetif
{
const long int REPLY_SIZE = 100;
void greet(
[in]
handle_t h,
[in, string] char client_greeting[],
[out, string] char server_reply[REPLY_SIZE]
);
}
Using defined interface:
Client
/* greet_client.c - Client of "greet" interface. usage: greet_client <CDS pathname>*/
#include <stdio.h> #include <dce/rpc.h> #include "greet.h“ #include "util.h"
Int main(int argc, char *argv[]) {
rpc_ns_handle_t import_context; handle_t binding_h; error_status_t status;
idl_char reply[REPLY_SIZE];
/* Start importing servers using the name specified on the command line. */
rpc_ns_binding_import_begin(rpc_c_ns_syntax_default, (unsigned_char_p_t)
argv[1],
greetif_v1_0_c_ifspec, NULL, &import_context, &status);
ERROR_CHECK(status, "Can't begin import");
/* Import the first server (we could iterate here, but we'll just take the first one). */
rpc_ns_binding_import_next(import_context, &binding_h, &status);
ERROR_CHECK(status, "Can't import");
/* Make the remote call. */
greet(binding_h, (idl_char *) "hello, server", reply);
printf("The Greet Server said: %s\n", reply);
}
Using defined interface:
Server
/* greet_manager.c - Implementation of "greet" interface. */
#include <stdio.h>
#include "greet.h"
void greet(
handle_t h,
idl_char *client_greeting,
idl_char *server_reply
)
{
printf("The client says: %s\n", client_greeting);
strcpy(server_reply, "Hi, client!");
}
Using defined interface:
Setting up listener
/* greet_server.c - usage: greet_server <CDS pathname> */
#include <stdio.h>
#include <dce/dce_error.h>
#include <dce/rpc.h>
#include "greet.h"
#include "util.h"
Int main(int argc, char *argv[]) {
unsigned32 status; rpc_binding_vector_t *binding_vector;
/* Register interface with RPC runtime. */
rpc_server_register_if(greetif_v1_0_s_ifspec, NULL, NULL, &status);
ERROR_CHECK(status, "Can't register interface");
/* Use all protocol sequences that are available. */
rpc_server_use_all_protseqs(rpc_c_protseq_max_reqs_default, &status);
ERROR_CHECK(status, "Can't use protocol sequences");
/* Get the binding handles generated by the runtime. */
rpc_server_inq_bindings(&binding_vector, &status);
ERROR_CHECK(status, "Can't get bindings for server");
Using defined interface:
Server
/* Register assigned endpoints with endpoint mapper. */
rpc_ep_register(greetif_v1_0_s_ifspec, binding_vector, NULL,
(unsigned_char_p_t) "greet server version 1.0", &status);
ERROR_CHECK(status, "Can't register with endpoint map");
/* Export ourselves into the CDS namespace. */
rpc_ns_binding_export(
rpc_c_ns_syntax_default, (unsigned_char_p_t) argv[1],
greetif_v1_0_s_ifspec, binding_vector, NULL, &status);
ERROR_CHECK(status, "Can't export into CDS namespace");
/* Start listening for calls. */
printf("Listening...\n");
rpc_server_listen(rpc_c_listen_max_calls_default, &status);
ERROR_CHECK(status, "Can't start listening for calls");
}
CS603
Communication Mechanisms:
Java RMI
23 January 2002
Java RMI
• Overview
– Supports remote invocation of Java objects
– Key: Java Object Serialization
Stream objects over the wire
– Language specific
• History
–
–
–
–
Goal: RPC for Java
First release in JDK 1.0.2, used in Netscape 3.01
Full support in JDK 1.1, intended for applets
JDK 1.2 added persistent reference, custom
protocols, more support for user control.
Java RMI
• Advantages
– True object-orientation: Objects as arguments and
values
– Mobile behavior: Returned objects can execute on
caller
– Integrated security
– Built-in concurrency (through Java threads)
• Disadvantages
– Java only
• Advertises support for non-Java
• But this is external to RMI – requires Java on both sides
Java RMI
Components
• Base RMI classes
– Extend these to get RMI functionality
• Java compiler – javac
– Recognizes RMI as integral part of language
• Interface compiler – rmic
– Generates stubs from class files
• RMI Registry – rmiregistry
– Directory service
• RMI Run-time activation system – rmid
– Supports activatable objects that run only on demand
Java RMI classes
• Java.rmi.Remote
– Interface supporting remote objects
• java.rmi.server.UnicastRemoteObject
– Continuously running server
• java.rmi.activation.Activatable
– Server started by rmid daemon
• java.rmi.naming
– Lookup: Returns stub given a name
• java.rmi.RMISecurityManager
– Validates rights to access downloaded object
Java RMI
Registry Operation
Java RMI
Object Serialization
• Key difference from DCE: Can send object to be
invoked at remote site
– Allows objects as arguments/results
• Mechanism: Object Serialization
– Object passed must inherit from serializable
– Provides methods to translate object to/from byte
stream
• Security issues:
– Ensure object not tampered with during transmission
– Solution: Class-specific serialization
Throw it on the programmer
Building a Java RMI
Application
• Define remote interface
– Extend java.rmi.Remote
• Create server code
– Implements interface
– Creates security manager, registers with registry
• Create client code
– Define object as instance of interface
– Lookup object in registry
– Call object
• Compile and run
– Run rmic on compiled classes to create stubs
– Start registry
– Run server then client
Java RMI
Sample interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Java RMI
Sample Client
import java.rmi.Naming;
import java.rmi.RemoteException;
public class HelloClient {
public static void main(String args[]) {
String message = "blank";
Hello obj = null;
try { obj = (Hello)Naming.lookup("//myhost/HelloServer");
message = obj.sayHello();
System.out.println(message);
} catch (Exception e) {
System.out.println("HelloClient exception: " + e.getMessage());
e.printStackTrace();
} } }
Java RMI:
Example Server
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
public class HelloServer extends UnicastRemoteObject implements Hello {
public HelloServer() throws RemoteException { super(); }
public String sayHello() { return "Hello World!"; }
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager()); }
try { HelloServer obj = new HelloServer();
Naming.rebind("//myhost/HelloServer", obj);
System.out.println("HelloServer bound in registry");
} catch (Exception e) {
System.out.println("HelloServer err: " + e.getMessage());
e.printStackTrace();
}}}
Java RMI Example:
Compile and Run
• javac Hello.java HelloServer.java
HelloClient.java
• rmic –d `pwd` HelloServer
• rmiregistry & # not in same directory
• java -Djava.rmi.server.codebase=file:///`pwd`/
-Djava.security.policy=opensocket HelloServer
– opensocket: grant { permission
java.net.SocketPermission "*", "connect"; };
• Java HelloClient