• Tidak ada hasil yang ditemukan

PPT Kernel dan Filesystem

N/A
N/A
Protected

Academic year: 2023

Membagikan "PPT Kernel dan Filesystem"

Copied!
27
0
0

Teks penuh

(1)

PENS-ITS 1

Advanced Socket Programming

Politeknik Elektronika Negeri Surabaya

(2)

PENS-ITS 2

Topik

• Memberikan gambaran singkat tentang object serialization

• Menunjukkan cara kerja dengan object serialization

• Menggambarkan cara kerja dengan obyek yang sudah ada dan objek yang dibuat sendiri

• Menunjukkan bagaimana untuk mengirim objek melalui socket

• Memberikan contoh multi-threaded server

(3)

PENS-ITS 3

Object Serialization

• Object Serialization adalah suatu mekanisme dalam program untuk menyimpan state object ke sebuah file dan selanjutnya membaca kembali object tersebut untuk mengubah state object tsb atau mengirim object melalui jaringan menggunakan socket.

• Class yang diserialisasi dapat dilakukan dengan

membuat class yang mengimplementasikan interface

java.io.Serializable.

• Interface ini tidak mempunyai fungsi yang harus

diimplementasikan, hanya sebagai interface penanda saja (a maker interface).

• Interface ini digunakan untuk memberi informasi ke Java virtual machine (JVM) bahwa object ini diserialisasi.

(4)

PENS-ITS 4

Object Serialization

• Ada dua class utama yang digunakan untuk membaca dan menulis objek untuk stream yaitu

ObjectOutputStream dan ObjectInputStream.

• ObjectOutputStream menyediakan fungsi writeObject untuk menulis sebuah objek ke output stream

• ObjectInputStream menyediakan fungsi readObject untuk membaca objek dari input stream.

• Penting !!! Bahwa objek yang digunakan dengan fungsi ini harus diserialisasikan. Artinya, class harus

mengimplementasikan interface Serializable.

(5)

PENS-ITS 5

Serializing Existing Classes

Sekarang kita lihat bagaimana cara membaca / menulis obyek, atau object dari class yang tersedia, yang sudah mengimplementasikan interface

Serializable.

Class Date dapat diserialisasikan, artinya class ini telah mengimplementasikan interface Serializable.

(6)

PENS-ITS 6

Serializing Existing Classes

• Membaca serialized object dan mencetak

informasi object tsb.

(7)

PENS-ITS 7

Serializing Existing Classes

• Untuk mengetahui apakah sebuah class adalah

serializable, gunakan tool serialver yang terdapat di JDK.

• Menggunakan GUI

(8)

PENS-ITS 8

Serializing Existing Classes

• shows that the Socket class is not

serializable.

(9)

PENS-ITS 9

Serializing Custom Classes

• how to serialize a custom class. In this example, we create a custom class, UserInfo

(10)

PENS-ITS 10

Serializing Custom Classes

The next step is to create a class that creates a instance of the UserInfo class and writes the object to an output stream as shown in Code Sample 4. The output stream in this example is a file called "name.out". The important thing to note from Code Sample 4 is that the writeObject method can be called any number of times to write any number of objects to the output stream.

(11)

PENS-ITS 11

Serializing Custom Classes

Finally, we write a class that reads the objects that have been saved, and invokes a method as shown in Code Sample 5. Again, as with writeObject, the readObject method can be called any number of times to read any

number of objects from the input stream.

(12)

PENS-ITS 12

Serializing Custom Classes

• To try out this example, compile the source files:

UserInfo.java, SaveInfo.java, and ReadInfo.java. Run SaveInfo, then ReadInfo, and you would see some output similar to this:

(13)

PENS-ITS 13

Transporting Objects over Sockets

• Now that we have seen how to write and read objects to/from I/O streams in a single process, let's see how to transport objects over sockets.

First, we will see how to transport existing object

(such as the Date object), then we will see how

to transport custom objects.

(14)

D4 PENS-ITS 14

Transporting an existing object

Here we develop a multi-threaded DateServer that listens on port 3000 and waits for requests from clients. Whenever there is a request, the server

replies by sending a Date object (over sockets) to the client as shown in Code Sample 6.

(15)

PENS-ITS 15

(16)

PENS-ITS 16

Transporting an existing object

• Note: the DateServer is a multi-threaded server that is implemented by inheriting from the

Thread class. Another approach to developing multi-threaded servers is to implement the

Runnable interface instead (inheritance vs.

composition).

(17)

PENS-ITS 17

Transporting an existing object

• The client, DateClient, does not have to send any messages to the DateServer

once a connection has been established. It simply receives a Date object that

represents the current day and time of the

remote machine. The client receives the

object and prints the date as shown in

Code Sample 7.

(18)

PENS-ITS 18

Transporting an existing object

(19)

PENS-ITS 19

Transporting Custom Objects

• In the previous example, we have worked with existing objects. What if you want to transport your own custom objects. Is the process different?

• In this example, we write an array multiplier server. The client sends two objects, each representing an array; the server receives the objects, unpack them by invoking a method and multiplies the arrays together and sends the output array (as an object) to the client. The client

unpacks the array by invoking a method and prints the new array.

• We start by making the class, whose objects will be

transportable over sockets, serializable by implementing the Serializable interface as shown in Code Sample 8.

(20)

PENS-ITS 20

Transporting Custom Objects

(21)

PENS-ITS 21

Transporting Custom Objects

• The next step is to develop the client. In this example, the client creates two instances of SerializedObject and writes them to the output stream (to the server), as shown from the source code in Code Sample 9.

(22)

PENS-ITS 22

Transporting Custom Objects

(23)

PENS-ITS 23

Transporting Custom Objects

• Now we need to develop the server, ArrayMultiplier. This server is similar to Code Sample 6. The only difference is in the processing. In this example, the server receives two objects, unpacks them and then multiplies the arrays together and finally sends the output as an object to the client. The ArrayMultiplier is shown in Code Sample 10.

(24)

D4 PENS-ITS 24

Transporting Custom Objects

(25)

PENS-ITS 25

Transporting Custom Objects

(26)

PENS-ITS 26

(27)

PENS-ITS 27

Transporting Custom Objects

• To run this example, modify the ArrayClient source specifying the machine name or IP address where the ArrayMultiplier server will run. Note that if you wish to run the server and client of this

particular example on two different machines then both machines must have a copy of the SerializedObject class. This breaks

information hiding and force tight coupling. A solution to this

problem would be to write an interface that extends the Serializable interface and then have the SerializedObject class in Code Sample 8 implement the new interface. Using this technique, you only need to provide copies of the interface to the client and server, but not implementation.

• If you run the ArrayMultiplier and ArrayClient successfully, you should get the output:

• The new array is: 15 15 15 15 15 15 15

Referensi

Dokumen terkait

Modul Description Module name Course Module Module level, if applicable Bachelor of Electrical Engineering Code, if applicable 0005-210-2 Subtitle, if applicable - Course,

Nees LYMOOI 017 LC-MS/MS analysis of extract from Gendarussa ventricosa enabled the identification of 24 putative compounds Table 4.42 belonging to different chemical families.. It