DS Practical
DS Practical
Practical: 1
EchoClient.java
import java.io.*;
import java.net.*;
}catch(Exception e){
System.out.println(e);
}
}
}
1 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
EchoServer.java
import java.io.*;
import java.net.*;
public class EchoServer{
private ServerSocket server;
public EchoServer(int port){
try{
server = new ServerSocket(port);
}catch(Exception e){
System.out.println(e);
}
}
public void serve(){
try{
while(true){
Socket client = server.accept();
BufferedReader r = new BufferedReader(new
InputStreamReader(client.getInputStream()));
PrintWriter w = new PrintWriter(client.getOutputStream(), true);
w.println("Welcome to the Java EchoServer. Type 'bye' to close.");
String line;
do{
line = r.readLine();
if ( line != null ){
System.out.println(" hi "+ line);
w.println("Got: "+ line);
}
}while (!line.trim().equals("bye"));
client.close();
}
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args){
EchoServer s = new EchoServer(9999);
s.serve();
}
}
2 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Output:
3 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Practical: 2
AddClient
import java.rmi.Naming;
public class AddClient {
public static void main(String[] args) {
try{
String addServerURL = "rmi://"+"localhost"+"/AddServer";
double d1 = Double.valueOf(args[1]).doubleValue();
double d2 = Double.valueOf(args[2]).doubleValue();
}catch(Exception e){
System.out.println("Exception:"+ e);
}
}
}
4 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
AssServer
import java.rmi.*;
class AddServer{
public static void main(String args[]) {
try{
AddServerImpl addServerImpl = new AddServerImpl();
Naming.rebind("AddServer", addServerImpl);
}catch(Exception e){
System.out.println("Exception:" + e);
}
}
}
AddServerlmpl
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf{
public AddServerImpl() throws RemoteException{}
AddServerlntf
import java.rmi.Remote;
import java.rmi.RemoteException;
5 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Output:
Server
Client
6 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Practical: 3
Warehouse.java
import java.rmi.*;
WarhouseImpl.java
7 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
WarehouseServer.java
WarehouseClient.java
import java.rmi.*;
import java.util.*;
import javax.naming.*;
Enumeration<NameClassPair> e = namingContext.list("rmi://localhost/");
while (e.hasMoreElements())
System.out.println(e.nextElement().getName());
String url = "rmi://localhost/Warehouse";
Warehouse centralWarehouse = (Warehouse) namingContext.lookup(url); /*
typecasting to Interface */
String descr = "Blackwell Toaster";
double price = centralWarehouse.getPrice(descr);
System.out.println(descr + ": " + price);
}
}
8 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Output:
Server
Client
9 |P a ge 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Practical: 4
Deposit.java
import java.io.*;
Account.java
import java.io.*;
class Account {
PrintWriter out;
10 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
int getBalance() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
return Deposit.balance;
}
void setBalance(int balance) { try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
Deposit.balance = balance;
}
}
DepositThread.jav
Output:
11 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Practical: 5
Server.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
Client.java
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
private static DataOutputStream dataOutputStream = null;
private static DataInputStream dataInputStream = null;
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 5000)) {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
while (true) {
System.out.print("input> ");
String message = scanner.nextLine();
12 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
dataOutputStream.writeUTF(message);
if (message.equalsIgnoreCase("exit()"))
break;
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
Output:
Server
Client
13 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Program II
FileServer_1.java
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import
java.net.ServerSocket; import
java.net.Socket;
14 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
fos.write(buffer, 0, read);
}
fos.close();
dis.close();
}
FileClient_1.java
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException; import
java.net.Socket;
private Socket s;
fis.close();
dos.close();
}
15 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
}
}
Output:
Server
Client
16 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Program III
Server_filw.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
receiveFile("Xenum_1.txt");
receiveFile("account_1.doc");
dataInputStream.close();
dataOutputStream.close(); clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
17 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Client_file.java
Import java.io.*; import java.net.Socket;
dataInputStream.close();
dataInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
18 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Output:
Server
Client
19 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Practical: 6
20 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
2. Choreography-
Choreography, in contrast, does not rely on a central coordinator. Rather, each
Web service involved in the choreography knows exactly when to execute its
operations and with whom to interact. Choreography is a collaborative effort
focusing on the exchange of messages in public business processes. All
participants in the choreography need to be aware of the business process,
operations to execute, messages to exchange, and the timing of message
exchanges.
21 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Practical: 7
Hello.idl
module HelloApp
{
interface Hello
{
string sayHello(); /* declaration of method */
double square(in string number); /* declaration of method */
// oneway void shutdown();
};
};
HelloImpl.java
import HelloApp.*; import
org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*; import
org.omg.PortableServer.*; import
org.omg.PortableServer.POA; import java.util.Properties;
22 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
HelloServer.java
import HelloApp.*; import
org.omg.CosNaming.*;
import
org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*; import
org.omg.PortableServer.*; import
org.omg.PortableServer.POA;
catch (Exception e) {
System.err.println("ERROR: " + e);
e.printStackTrace(System.out);
}
23 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
HelloClient.java
Import HelloApp.*; import org.omg.CosNaming.*;
import
org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*; import java.util.*;
public class HelloClient {
static Hello helloImpl;
24 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Output:
Server
Client
25 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Practical: 8
Web Services
Using Erl’s terminology, this business process can be broken down into two “independent
units of logic”—the Purchase Order Service and theInventory Management Service. As
services participating in an SOA composite application, these “units of logic” can be
described in terms of their input and output messages. The Purchase Order Service (upper-
right corner of Figure 2–1) is responsible for providing the following service:
As indicated in Figure 2–1, the Purchase Order Service is implemented using Java EE 5.
The other service composed by the Order Management System is the Inventory
Management System. It is responsible for providing the following service:
26 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
delivery dates for the items. As indicated in Figure 2–1, the Order Management Service
is implemented using Java EE 5.
27 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
b. RESTful services are stateless. As Fielding writes in Section 5.1.3 of his thesis, “each
request from client to server must contain all the information necessary to understand
the request and cannot take advantage of any stored context on the server.”
c. RESTful services have a uniform interface. This constraint is usually taken to mean
that the only allowed operations are the HTTP operations: GET, POST, PUT, and
DELETE.
d. REST-based architectures are built from resources (pieces of information) that are
uniquely identified by URIs. For example, in a RESTful purchasing system, each
purchase order has a unique URI.
e. REST components manipulate resources by exchanging representations of the
resources. For example, a purchase order resource can be represented by an XML
document. Within a RESTful purchasing system, a purchase order might be updated by
posting an XML document containing the changed purchase order to its URI.
These are the basic principles behind REST. However, when people talk about the benefits
of RESTful systems today, they usually are not strictly applying these principles. For
example, among REST advocates, keeping shopping cart data on the server and
maintaining a session related to the shopping process that is using the cart is acceptable.2
In fact, the XML/HTTP Binding provided by JAX-WS for implementing RESTful services
provides for session management capabilities using cookies, URL rewriting, and SSL
session IDs.
More significant deviations from Fielding’s definition of REST involve getting around
the “uniform interface” constraint by embedding verbs and parameters inside URLs. The
Amazom.com REST interface, for example, includes verbs in query strings and doesn’t
have unique URIs for each resource. Systems like this, although labelled as RESTful, are
really starting to look very much like RPC using XML over HTTP without SOAP.
RESTful Web Services in contrast to SOAP Web Services. Table illustrates the principal
differences.
Table – RESTful Web Services vs SOAP Web Services
REST SOAP
Message Format XML XML inside a SOAP Envelope
Interface Definition none WSDL
Transport HTTP HTTP, FTP, MIME, JMS, SMTP, etc.
This is consistent with common usage in the REST versus SOAP debates. REST uses
simple XML over HTTP without a WSDL interface definition.
28 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
The next few sections focus on uploading/downloading XML documents with bare-
bones RESTful Web Services. I show how to write clients for RESTful services with
and without JWS. This material may seem very basic to advanced Java programmers,
but it is always good to review the basics before diving into a complex subject like
SOA with Java Web Services.
Since this is a book about Java, I start with the assumption that the EISs have Java
APIs for accessing the needed records. The challenge addressed in the next few
sections is to deploy a Java API as a Web service or to invoke a Web service using
Java.
Getting EIS Records from a REST Service without Using JWS
This section briefly examines how to get an XML document from a RESTful Web
service. In this example, the Web service is accessed with an HTTP GET request. The
client application needs to issue the HTTP GET and process the HTTP response stream
that contains the XML document. Instead of using the JWS APIs (e.g., JAX-WS), I
simply use the javax.net.HttpURLConnection class to handle most of the work related
to generating the HTTP GET and processing the response.
29 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
Figure 4: The client uses the HttpURLConnection class to make an HTTP GET
request and receive an HTTP response.
Program 1 shows the client-side code for issuing the HTTP GET request and
receiving the XML document via the HTTP response. Notice that the String used to
construct the URL instance is passed to the client as args[0]. The
HttpULRConnection—con—doesn’t send the HTTP request until its connect ()
method gets invoked. Before this happens, the setRequestMethod() is invoked to
specify that a GET request should be sent.
30 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
In this program, the HttpURLConnection class does all the work. It sends the HTTP GET
request to the Web service URL6 and provides access to the response as an InputStream.
Now, let’s look at how this is done using JWS.
Program 2 shows the code used to implement the JAX-WS version of GetNewOrders.
Browsing through this code, you can see some of the awkwardness that comes from applying
the WSDL-oriented Service API in a REST context. First, notice that you must create
QName instances for the Service instance and the “port” that corresponds to the RESTful
Web service. In a SOAP scenario, these qualified names would correspond to the WSDL
definitions for the wsdl: service and wsdl: port. Since there is no WSDL when invoking a
RESTful service, these QName instances are gratuitous in this example. They are required by
the API, but not used to invoke the RESTful service.
31 | P a g e 190950131124_JANKI SHAH
ITM Universe, Vadodara
Bachelor of Computer Science, Engineering
SEM-7th 3170719- Distributed System Practical
32 | P a g e 190950131124_JANKI SHAH