Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte...

44
Pemrograman Remote Method Invocation Husni

Transcript of Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte...

Page 1: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Pemrograman Remote Method Invocation

Husni

Page 2: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Sasaran Hasil Belajar

Setelah menyelesaikan pelajaran mengenai RMI ini, diharapkan anda:

• Memahami bagaimana proses-proses di dalam RMI bekerja

• Mampu mengimplementasi RMI (pada Bahasa Java)

• Mampu menjalankan client dan server RMI

• Mengetahui poin-poin penting keamanan RMI

2

Page 3: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Mengenal RMI

1. Dalam lingkungan distribusi, sering diharapkan mampu memanggil

metode pada obyek jauh (yaitu pada obyek yang bertempat di sistem

lain). RMI (Remote Method Invocation) menyediakan suatu sarana bebas

platform untuk melakukan ini.

2. Sekali suatu referensi ke remote object diperoleh, metode dari obyek

tersebut dapat dipanggil dengan cara yang sama seperti obyek berada di

lokal.

3. RMI akan menggunakan aliran byte untuk mentransfer data dan

pemanggilan metode.3

Page 4: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Gambaran Aplikasi RMI

4

Page 5: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Model Client Server RMI

5

Page 6: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Proses RMI

6

Page 7: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Sistem RMI

7

Page 8: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Proses RMI Dasar

1. The server program that has control of the remote object registers an interface with a naming service.

2. The interface contains the signatures for those methods of the object that the server wishes to make publicly available.

3. Stub. 4. Skeleton. 5. The client program invokes a method of the remote object.6. An equivalent method is being called in the stub.7. Marshalling.8. UnMarshalling.9. Finally, the skeleton calls the implementation of the method on the

server.8

Page 9: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

RMI Process

9

Page 10: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Arsitektur RMI Umum

• Pertama, server harus mengikatkan (bind) nama-nya ke registry

• Client mencari (lookup) nama server di dalam registry untuk membangun referensi remote

• Stub menyusun (serializing) parameter-parameter ke skeleton, skeleton memanggil metode remote dan menyusun hasil balik ke stub tersebut

10

Page 11: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Rincian ImplementasiPaket yang digunakan dalam implementasi aplikasi client-server RMI adalah

➢java.rmi

➢java.rmi.server dan

➢java.rmi.registry

Langkah-langkah dasarnya adalah

1. Membuat interface.

2. Mendefinisikan suatu kelas yang mengimplementasikan interface ini.

3. Membuat proses server.

4. Membuat proses client.11

Page 12: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kompilasi dan Eksekusi

1. Kompilasikan semua file dengan javac.

2. Buka command prompt pertama, jalankan registry RMI.

3. Buka jendela (console) baru dan jalankan server

4. Buka console ketiga dan jalankan client-nya.

12

Page 13: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kompilasi dan Eksekusi (Lanj.)1. Kompilasikan semua file dengan javac.

• javac Hello.java

• javac HelloImpl.java

• javac HelloServer.java

• javac HelloClient.java

2. Jalankan registry RMI.

Masukkan perintah: rmiregistry

Saat ini dieksekusi, indikasi bahwa sesuatu telah terjadi hanyalah perubahan pada judul jendela command prompt.

13

Page 14: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kompilasi dan Eksekusi (Lanj.)

3. Buka console baru dan jalankan server.

Dari jendela baru tersebut, panggil Java compiler: java HelloServer

Output server terlihat sebagai berikut:

4. Buka console baru ketiga dan jalankan client.

Lagi, panggil Java compiler: java HelloClient

Output diperlihatkan sebagai berikut:

14

Page 15: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Pemanfaatan Kehebatan RMI

Dalam aplikasi RMI nyata, banyak metode dan mungkin banyak obyek yang dikembangkan. Pada demikian, ada 2 kemungkinan strategi yang dapat diadopsi, yaitu:

➢Menggunakan instance tunggal dari kelas Implementations untuk menangani instance dari suatu kelas yang mempunyai metode untuk dipanggil secara remote. Lewatkan instance dari kelas tersebut sebagai argumen dari constructor bagi kelas Implementations.

➢Menggunakan kelas Implementations secara langsung untuk menyimpan data dan metode yang diperlukan, membuat instance dari kelas ini, bukan menggunakan kelas-kelas terpisah.

15

Page 16: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Keamanan RMI

➢File java.policy mendefinisikan batasan-batasan keamanan.

➢File java.security mendefinisikan properti keamanan.

➢Implementasi kebijakan keamanan dikontrol oleh suatu obyekl dari kelas RMISecurityManager (sub-kelas dari SecurityManager ).

➢Kita harus membuat security manager sendiri yang mengeksten RMISecurityManager. Manajer keamanan ini harus menyediakan suatu definisi untuk metode checkPermission , yang mengambil satu argumen dari kelas Permission dari paket java.security .

16

Page 17: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Tutorial Pemrograman Java RMI

Page 18: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Java RMI

• RMI = Remote Method Invocation

• Allows a method to be invoked that resides on a different JVM (Java Virtual Machine):▪ Either a remote machine

▪ Or same machine, different processes

✓Each process runs on a different Java Virtual Machines (JVM)

✓Different address space per process/JVM

RMI provides object-oriented RPC (Remote Procedure Calls)

Page 19: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Proses yang Terlibat

• Client▪ Process that is invoking a method on a remote object

• Server▪ Process that owns the remote object

▪ To the server, this is a local object

• Object Registry (rmiregistry)▪ Name server that associates objects with names

▪ A server registers an object with rmiregistry

▪ URL namespace✓ rmi://hostname:port/pathname

✓Contoh: rmi://crapper.pk.org:12345/MyServer

Nomor Port

Page 20: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Classes & Interfaces needed for Java RMI

• Remote: for accessing remote methods▪ Used for remote objects

• Serializable: for passing parameters to remote methods▪ Used for parameters

• Juga diperlukan:▪ RemoteException: network or RMI errors can occur

▪ UnicastRemoteObject: used to export a remote object reference or obtain a stub for a remote object

▪ Naming: methods to interact with the registry

Page 21: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kelas Remote

• Remote class (remote object)▪ Instances can be used remotely

▪ Works like any other object locally

▪ In other address spaces, object is referenced with an object handle✓The handle identifies the location of the object

▪ If a remote object is passed as a parameter, its handle is passed

Page 22: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Interface Serializable

• java.io.Serializable interface (serializable object)

▪ Allows an object to be represented as a sequence of bytes

▪ Allows instances of objects to be copied between address spaces✓Can be passed as a parameter or be a return value to a remote object

✓Value of object is copied (pass by value)

▪ Any objects that may be passed as parameters should be defined to implement the java.io.Serializable interface✓Good news: you rarely need to implement anything

o All core Java types already implement the interface

o For your classes, the interface will serialize each variable iteratively

Page 23: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kelas Remote

• Classes that will be accessed remotely have two parts:1. interface definition

2. class definition

• Remote interface▪ This will be the basis for the creation of stub functions

▪ Must be public

▪ Must extend java.rmi.Remote

▪ Every method in the interface must declare that it throws java.rmi.RemoteException

• Remote class▪ implements Remote interface

▪ extends java.rmi.server.UnicastRemoteObject

Page 24: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Contoh Program Super Sederhana

• Client invokes a remote method with strings as parameter

• Server returns a string containing the reversed input string and a message

Page 25: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Definisikan Interface Remote: SampleInterface.java

import java.rmi.Remote;

import java.rmi.RemoteException;

public interface SampleInterface extends Remote {

public String invert(String msg) throws RemoteException;

}

• Interface is public

• Extends the Remote interface

• Defines methods that will be accessed remotely▪ We have just one method here: invert

• Each method must throw a RemoteException▪ In case things go wrong in the remote method invocation

Page 26: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Definisikan Kelas Remote (Sample.java)

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.rmi.server.*;

public class Sample

extends UnicastRemoteObject

implements SampleInterface {public Sample() throws RemoteException { }public String invert(String m) throws RemoteException {

// return input message with characters reversedreturn new StringBuffer(m).reverse().toString();

}

}

• Defines the implementation of the remote methods

• It implements the interface we defined

• It extends the java.rmi.server.UnicastRemoteObject class▪ Defines a unicast remote object whose references are valid only while the server process is alive.

Page 27: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Selanjutnya...

• Kita sudah mempelajari:▪ The remote interface definition: SampleInterface.java

▪ The server-side (remote) class: Sample.java

• Selanjutnya, we’ll write the server: SampleServer.java

• Two parts:1. Create an instance of the remote class

2. Register it with the name server (rmiregistry)

Page 28: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kode Server (SampleServer.java)

• Create the objectnew Sample()

• Register it with the name server (rmiregisty)Naming.rebind("Sample”, new Sample())

• rmiregistry runs on the server▪ The default port is 1099

▪ The name is a URL format and can be prefixed with a hostname and port: “//localhost:1099/Server”

Page 29: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kode Server: Bagian 1 (SampleServer.java)

import java.rmi.Naming;

import java.rmi.RemoteException;

import java.rmi.server.UnicastRemoteObject;

public class SampleServer {

public static void main(String args[]) {

if (args.length != 1) {

System.err.println("usage: java SampleServer rmi_port");

System.exit(1);

}

Page 30: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kode Server: Bagian 2 (SampleServer.java)try {

// first command-line arg: the port of the rmiregistry

int port = Integer.parseInt(args[0]);

// create the URL to contact the rmiregistry

String url = "//localhost:" + port + "/Sample";

System.out.println("binding " + url);

// register it with rmiregistry

Naming.rebind(url, new Sample());

// Naming.rebind("Sample", new Sample());

System.out.println("server " + url + " is running...");

}

catch (Exception e) {

System.out.println("Sample server failed:" +

e.getMessage());

}

}

}

Page 31: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

File Kebijakan (Policy)

• When we run the server, we need to specify security policies

• A security policy file specifies what permissions you grant to the program

• This simple one grants all permissions

grant {

permission java.security.AllPermission;

};

Page 32: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Client

• The first two arguments will contain the host & port

• Look up the remote function via the name server

• This gives us a handle to the remote methodSampleInterface sample = (SampleInterface)Naming.lookup(url);

• Call the remote method for each argumentsample.invert(args[i]));

• We have to be prepared for exceptions

Page 33: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kode Client: Bagian 1 (SampleClient.java)

public class SampleClient {

public static void main(String args[]) {

try {

// basic argument count check

if (args.length < 3) {

System.err.println(

"usage: java SampleClient rmihost rmiport string... \n");

System.exit(1);

}

// args[0] contains the hostname, args[1] contains the port

int port = Integer.parseInt(args[1]);

String url = "//" + args[0] + ":" + port + "/Sample";

System.out.println("looking up " + url);

// look up the remote object named “Sample”

SampleInterface sample = (SampleInterface)Naming.lookup(url);

Page 34: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kode Client: Bagian 2 (SampleClient.java)

// args[2] onward are the strings we want to reverse

for (int i=2; i < args.length; ++i)

// call the remote method and print the return

System.out.println(sample.invert(args[i]));

} catch(Exception e) {

System.out.println("SampleClient exception: " + e);

}

}

}

Page 35: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Kompilasi

• Compile the interface and classes:javac SampleInterface.java Sample.java

javac SampleServer.java

• And the client…javac SampleClient.java

(you can do it all on one command: javac *.java)

• Note – Java used to use a separate RPC compiler▪ Since Java 1.5, Java supports the dynamic generation of stub classes at

runtime

▪ In the past, one had to use an RMI compiler, rmic

▪ If you want to, you can still use it but it’s not needed

Page 36: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Menjalankan...

• Start the object registry (in the background):rmiregistry 12345 &• An argument overrides the default port 1099

• Start the server (giving it the port of the rmi registry):java -Djava.security.policy=policy SampleServer 12345

• Run the client:java SampleClient svrname 12345 testing abcdefgh▪ Where svrname is the name of the server host▪ 12345 is the port number of the name server: rmiregistry, not the service!

• See the output:gnitsethgfedcba

Page 37: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

RMI

Sedikit Sisi Dalamnya

Page 38: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Interface

• Interfaces define behavior

• Classes define implementation

• RMI: two classes support the same interface▪ client stub

▪ server implementation

Page 39: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Arsitektur Tiga Layer

Stub functions Application interaction. Marshaling & unmarshaling

Remote reference layer Handles the creation & management of remote objects. Deals with thesemantics of remote requests (how they behave).

Transport layer Setting up connections and sending/receiving data

Page 40: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Server

• Server creates an instance of the server object▪ extends UnicastRemoteObject

▪ TCP socket is bound to an arbitrary port number

▪ thread is created which listens for connections on that socket

• Server registers object▪ RMI registry is an RMI server (accepts RMI calls)

▪ Hands the registry the client stub for that server object✓contains information needed to call back to the server (hostname, port)

Page 41: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Client

• Client obtains stub from registry

• Client issues a remote method invocation▪ stub class creates a RemoteCall

✓opens socket to the server on port specified in the stub

✓sends RMI header information

▪ stub marshals arguments over the network connection✓uses methods on RemoteCall to obtain a subclass of ObjectOutputStream

✓knows how to deal with objects that extend java.rmi.Remoteo serializes Java objects over socket

▪ stub calls RemoteCall.executeCall()✓causes the remote method invocation to take place

Page 42: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Server (Lanj.)

• Server accepts connection from client

• Creates a new thread to deal with the incoming request

• Reads header information▪ creates RemoteCall to deal with unmarshaling RMI arguments

• Calls dispatch method of the server-side stub (skeleton)▪ calls appropriate method on the object

▪ sends result to network connection via RemoteCall interface

▪ if server threw exception, that is marshaled instead of a return value

Page 43: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Client (Lanj.)

• The client unmarshals the return value of the RMI▪ using RemoteCall

• value is returned from the stub back to the client code▪ or an exception is thrown to the client if the return was an exception

Page 44: Pemrograman Remote Method Invocation · Sasaran Hasil Belajar ... RMI akan menggunakan aliran byte untuk mentransfer data dan pemanggilan metode. 3. ... (lookup) nama server di

Pertanyaan?