Distributed System Lab Manual
Distributed System Lab Manual
SSASIT, Surat
Laboratory Manual
For
DISTRIBUTED SYSTEMS
(180701)
B.E. (COMPUTER)
SEM VIII
January 2013
SSASIT, Surat
Index
Sr.
No.
1.
2.
3.
Topic
RPC Programming
1. Implement PI calculation Service
2. Implement Calculator Service using SUN RPC.
3. Implement RPC programming on windows using DCOM.
RMI Programming
1. Implementation of Hello Word Service using JAVA RMI
2. Implementation of Calculator Service using JAVA RMI
3. Implement RMI IIOP Programming.
Thread Programming in Java
1. Write an application that executes two threads. One thread display
HELLO WOLD every 1000 milliseconds and another thread display
How Are You every 2000 milliseconds. Create the threads by
implementing Runnable interface.
2. Implement Multithreaded Echo server using Socket.
3. Implement producer consumer example.
Page
No.
3
12
14
4.
19
5.
29
6.
34
7.
35
8.
36
9.
47
~2~
SSASIT, Surat
EXPERIMENT 1
AIM:
RPC Programming
4. Implement PI calculation Service
5. Implement Calculator Service using SUN RPC.
6. Implement RPC programming on windows using DCOM.
(1) Implement PI calculation Service
Create a pi.x file
Code of pi.x
program PIPROG {
version CALCU_PIVERS {
double CALCU_PI() = 1;
} = 1;
} = 0x39876543;
Compile a pi.x file using,
rpcgen -a pi.x
where: option a tells rpcgen to generate all of the supporting files
It will generate following files.
pi_server.c
#include <rpc/rpc.h>
#include "pi.h"
double * calcu_pi_1_svc(void *argp, struct svc_req *rqstp){
static double pi;
double sum = 0;
int i;
int sign;
for (i=1; i<10000000; i++ ){
~3~
SSASIT, Surat
sign = (i+1) % 2;
if ( sign == 0 )
sign = 1;
else
sign = -1;
sum += 1 / (2*(double)i -1) * (double)sign;
}
pi = 4 * sum;
return (&pi);
}
#include <stdio.h>
#include <rpc/rpc.h>
#include "pi.h"
main(int argc, char *argv[]){
CLIENT * clnt;
double *result_1;
char *host;
char * calcu_pi_1_arg;
if (argc < 2){
printf("usage: %s server_host\n", argv[0]);
exit(1);
}
host = argv[1]; /* server host name */
clnt = clnt_create(host, PIPROG, CALCU_PIVERS, "tcp");
if (clnt == (CLIENT *) NULL){
/* Couldn't establish connection with server. Print error message and die. */
clnt_pcreateerror(host);
exit(1);
}
/* call remote procedure on the server side */
result_1 = calcu_pi_1((void *)&calcu_pi_1_arg, clnt);
if (result_1 == (double *) NULL){
/* An error occurred while calling the server. Print error message and die.*/
clnt_perror (clnt, "call failed");
exit (1);
}
printf("PI value is= %f\n" , *result_1);
clnt_destroy(clnt);
~4~
SSASIT, Surat
exit(0);
}
~5~
SSASIT, Surat
Server Programm:
Calc_server.c
#include "Calc.h"
#include <rpc/rpc.h>
int * add_1_svc(operands *argp, struct svc_req *rqstp){
static int result;
result = argp->x + argp->y;
return (&result);
}
int * sub_1_svc(operands *argp, struct svc_req *rqstp){
static int result;
result = argp->x - argp->y;
return (&result);
}
int * mult_1_svc(operands *argp, struct svc_req *rqstp){
static int result;
result = argp->x * argp->y;
return (&result);
}
int * divi_1_svc(operands *argp, struct svc_req *rqstp){
static int result;
result = argp->x / argp->y;
return (&result);
}
~6~
SSASIT, Surat
Client program:
Calc_client.c
#include <rpc/rpc.h>
#include <stdio.h>
#include "Calc.h"
int main( int argc, char *argv[]){
CLIENT *clnt;
char o;
Operands ops;
int *result;
if (argc!=5){
fprintf(stderr,"Usage: %s hostname num1 num\n",argv[0]);
exit(0);
}
clnt = clnt_create(argv[1], Calc_PROG, Calc_VERSION, "udp");
if (clnt == (CLIENT *) NULL){
clnt_pcreateerror(argv[1]);
exit(1);
}
ops.x = atoi(argv[2]);
ops.y = atoi(argv[3]);
o=argv[4];
If(o==+){
result = add_1(&ops,clnt);
}else if(o==-){
result = sub_1(&ops,clnt);
}else if(o==*){
result = mult_1(&ops,clnt);
}else if (o==/){
result = divi_1(&ops,clnt);
}
if (result==NULL) {
fprintf(stderr,"Trouble calling remote procedure\n");
exit(0);
}
printf("%d
return(0);
~7~
SSASIT, Surat
~8~
SSASIT, Surat
Status=RpcServerUseProtseqEp((unsignedchar*)("ncacn_ip_tcp"),RPC_C_PROTSE
Q_MAX_REQS_DEFAULT,(unsigned char*)("9191"),NULL);
/* Arguments:(1) Use TCP/IP protocol,
(2) Backlog q length for TCP/IP,
(3) TCP/IP port to use, No security. */
if(status){
exit(status);
}
Status = RpcServerRegisterIf(DoRPC_v1_0_s_ifspec, NULL,NULL);
// Registers the DoRPC interface.
/*Arguments:(1)Interface to register.
(2)Use the MIDL generated entry-point vector.
(3) Use the MIDL generated entry-point vector. */
if (status)
exit(status);
// Start to listen for remote procedure calls for all registered interfaces.
// This call will not return until RpcMgmtStopServerListening is called.
status = RpcServerListen( 1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, FALSE);
~9~
SSASIT, Surat
~ 10 ~
SSASIT, Surat
RpcEndExcept
status = RpcStringFree(&szStringBinding);
// Free the memory allocated by a string. String to be freed.
if(status){
exit(status);
}
// Releases binding handle resources and disconnects from the server.
status = RpcBindingFree(&hDoRPCBinding);
// Frees the implicit binding handle defined in the IDL file.
if (status){
exit(status);
}
return 0;
}
void* __RPC_USER midl_user_allocate(size_t size){ //Memory allocation function for RPC.
return malloc(size);
}
void __RPC_USER midl_user_free(void* p){ // Memory deallocation function for RPC.
free(p);
}
~ 11 ~
SSASIT, Surat
EXPERIMENT 2
~ 12 ~
SSASIT, Surat
~ 13 ~
SSASIT, Surat
EXPERIMENT 3
Exercise
(1) Differentiate Thread and Process.
Thread
Process
Threads (Light weight Processes) share the processes have their own address.
address space of the process that created it
Threads have direct access to the data
segment of its process
Threads can directly communicate with other
threads of its process
~ 14 ~
SSASIT, Surat
Thread started
new
Runnable
Unlock signal
Await Sleep
interval expires
Await Lock
waiting
Timed waiting
terminate
New: A new thread begins its life cycle in the new state. It remains in this state
until the program starts the thread. It is also referred to as a born thread.
Runnable: After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
Waiting: Sometimes a thread transitions to the waiting state while the thread
waits for another thread to perform a task.A thread transitions back to the
runnable state only when another thread signals the waiting
thread to continue executing.
~ 15 ~
SSASIT, Surat
Timed waiting: A runnable thread can enter the timed waiting state for a
specified interval of time. A thread in this state transitions back to the runnable
state when that time interval expires or when the event it is waiting for occurs.
Terminated: A runnable thread enters the terminated state when it completes its
task or otherwise terminates
thread.It is done
void start( );
we can define the code that constitutes the new thread inside
run()
method.
run() method can call other methods, use other classes, and declare variables, just like
the main thread can.
public void run( )
~ 16 ~
SSASIT, Surat
class Callme
{
void call(String msg) {
System.out.print("[" + msg);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable {
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s) {
target = targ;
msg = s;
t = new Thread(this);
t.start();
}
public void run() {
synchronized(target) {
target.call(msg);
}
}
}
class Synch {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
~ 17 ~
SSASIT, Surat
OUTPUT
~ 18 ~
SSASIT, Surat
EXPERIMENT4
AIM: Mobile Agent (IBMs Aglet) Programming
LOGIC:
(1) Mobile agent (IBMs Aglet) Programming
What is Mobile Agent?
-
~ 19 ~
SSASIT, Surat
OAA
TACOMA (C++)
What is Aglets ?
Java based mobile agent toolkit developed by IBM
The name originates from Aglet =Agent + Applet
Aglet properties:
Mobile Java object that can visit Aglet enabled hosts
Executes in a Context; there may be many contexts on a node
An Aglet server hosts N contexts
Aglet = Aglet state (variable values) + Aglet code (Java class)
Autonomous
Runs in a separate thread when it arrives at a host
Reactive
Responds to messages that are sent to it. A message is an object
exchanged by aglets
Message passing is synchronous and asynchronous
Is unique
A globally unique identifier is bound to each aglet
Mobility level = weak mobility
Step2 :
Install both.
For installing Aglet SDK:
download - Aglets1_0_3.class
Aglet SDK Page and install as following :
~ 20 ~
SSASIT, Surat
~ 21 ~
SSASIT, Surat
After finishing this setup you will get installed aglets in C:\Aglets 1.0.3 folder
~ 22 ~
SSASIT, Surat
~ 23 ~
SSASIT, Surat
Step 5 :-
Step 6:-
~ 24 ~
SSASIT, Surat
~ 25 ~
SSASIT, Surat
Select example.hello.HelloAglet and click create this will create window like below
Now type what ever you want and click GO button this will create window like below
~ 26 ~
SSASIT, Surat
Once U unzip the pack u will fine Sub Folder like bin, Cnf, public.
Run ant
To Run ant, at given path simply type ant. bat( e.g. C:\aglets-2.0.2\bin\ant.bat)
~ 27 ~
SSASIT, Surat
Tahiti server will ask u to enter the login name and password. (In login name type
anonymous and in password field type: aglets and Tahiti Gui is appeared on the
screen.
Start working with the aglets lifecycle for example creating, dispatching, etc.
Get the file of agent (java compiled file) from public sub directory).
Problems:
~ 28 ~
SSASIT, Surat
EXPERIMENT 5
~ 29 ~
SSASIT, Surat
obj = orb.resolve_initial_references("RootPOA");
rootPOA = POAHelper.narrow(obj);
}catch (org.omg.CORBA.ORBPackage.InvalidName e) { }
CounterImpl c_impl = new CounterImpl();
Counter c = c_impl._this(orb);
try{
FileOutputStream file = new FileOutputStream("Counter.ref");
PrintWriter writer = new PrintWriter(file);
String ref = orb.object_to_string(c);
writer.println(ref);
writer.flush();
file.close();
out.println("Server started." + " Stop: Ctrl-C");
}catch (IOException ex){
out.println("File error: " + ex.getMessage());
exit(2);
}
rootPOA.the_POAManager().activate();
orb.run();
}catch(Exception ex) {
out.println("Exception: " + ex.getMessage());
exit(1);
}
}
}
Client.java
import java.io.*;
import java.util.*;
import org.omg.CORBA.*;
import static java.lang.System.*;
public class Client{
public static void main(String[] args) {
try {
Properties props = getProperties();
ORB orb = ORB.init(args, props);
String ref = null;
org.omg.CORBA.Object obj = null;
try {
Scanner reader = new Scanner(new File("Counter.ref"));
ref = reader.nextLine();
}catch (IOException ex) {
out.println("File error: " + ex.getMessage());
exit(2);
~ 30 ~
SSASIT, Surat
}
obj = orb.string_to_object(ref);
if (obj == null) {
out.println("Invalid IOR");
exit(4);
}
Counter c = null;
try {
c = CounterHelper.narrow(obj);
}catch (BAD_PARAM ex) {
out.println("Narrowing failed");
exit(3);
}
int inp = -1;
do {
out.print("Counter value: " + c.value() + "\nAction (+/-/e)? ");
out.flush();
do {
try {
inp = in.read();
}catch (IOException ioe) {
}
} while (inp != '+' && inp != '-' && inp != 'e');
if (inp == '+')
c.inc();
else if (inp == '-')
c.dec();
} while (inp != 'e');
}catch (Exception ex) {
out.println("Exception: " + ex.getMessage());
exit(1);
}
}
}
OUTPUT
Compile the Remote Interface
idlj fall Counter.idl
Compile Clinet & Server file
javac Server.java
javac Client.java
~ 31 ~
SSASIT, Surat
When the parameter setting fall is chosen, the JDKs IDL to Java compiler generates the
following files:
CounterPOA.java
_CounterStub.java
CounterHolder.java
CounterHelper.java
Counter.java
CounterOperations.java
All these files are needed to implement the server application. The client application needs all
files except CounterPOA.java.
Run Client and Server file
Start java Server
Java Client
~ 32 ~
SSASIT, Surat
Server.java
Client.java
Problem Statement:
~ 33 ~
SSASIT, Surat
~ 34 ~
SSASIT, Surat
EXPERIMENT7
~ 35 ~
SSASIT, Surat
EXPERIMENT8
4. In the Name and Location page, enter the project name (for this example HelloWorld)
and specify the project location or accept the defaults.
~ 36 ~
SSASIT, Surat
5. Click Finish.
The Projects window now contains a project node for the BPEL Module project.
~ 37 ~
SSASIT, Surat
2. Enter a name for the process file name (HelloWorldProcess for this example), and
click Finish.
The new BPEL file opens in the Design view of the BPEL Designer.
If the Palette and Properties windows are not displayed in your current view, click Windows > Reset Windows on the NetBeans menu.
~ 38 ~
SSASIT, Surat
~ 39 ~
SSASIT, Surat
~ 40 ~
SSASIT, Surat
~ 41 ~
SSASIT, Surat
~ 42 ~
SSASIT, Surat
7. To save your changes click the Save All icon in the IDE menu bar.
Creating a Composite Application Project
A BPEL Module project is not directly deployable. You must first add a BPEL Module
project, as a JBI module, to a Composite Application project. You can then deploy the
Composite Application project. Deploying the project makes the service assembly available
to the application server and enables its service units to run.
To Create a New Composite Application Project
1. Choose File > New Project (Ctrl-Shift-N).
2. In the Categories list choose Service Oriented Architecture, in the Projects list choose
Composite Application, and click Next.
3. In the Name and Location page, change the project name to HelloWorldApplication,
and specify the location of project files.
4. To set the new Composite Application the main project as main, leave the Set as Main
Project checkbox selected, and click Finish.
5. To add the BPEL Module as a JBI module to the Composite Application project,
right-click the new Composite Application and choose Add JBI Module.
The Select Project dialog box opens.
6. Select the HelloWorld project you created earlier and click Add Project JAR Files.
The Select Project dialog box closes and the HelloWorld.jar file is added to the JBI Modules
node of the HelloWorldApplication Composite Application
Building and Deploying the Composite Application Project
Building a project compiles the BPEL source file and packages the BPEL file and web
service artifacts, including WSDL and XSD files, into a JAR archive. Deploying the project
compiles the files in the Composite Application project, packages the compiled BPEL and
related web service artifacts (including WSDL and XSD files) into an archive, and deploys
them to the Application Server.
To Build and Deploy the Composite Application Project
1. Right-click the Composite Application project's node, and choose Build.
~ 43 ~
SSASIT, Surat
When the build is complete the Output window reports Build Successful. If the Output
window is not visible, choose Window -> Output -> Output.
2. Right-click the Composite Application project's node, and choose Deploy.
3. Deployment has succeeded when you see a Build successful message in the GlassFish
tab of the Output window.
4. Open the Services window and expand Servers -> GlassFish V2 -> JBI -> Service
Assemblies to see your new deployed Service Assembly.
If you do not see the deployed project, right-click the Service Assemblies node and choose
Refresh.
~ 44 ~
SSASIT, Surat
Note
If the Source Editor does not contain a tab for Input.xml, double-click the Input node in the
Projects window to open the file.
<syn:paramA>?string?<syn:paramA>
b. Replace ?string? with Hello World, so that the line appears as follows:
<syn:paramA>Hello World<syn:paramA>
c. From the NetBeans IDE menu bar, click Save All.
6. In the Projects window, double-click the Output node under Test -> TestCase1.
Output.xml is opened in the Source Editor. Initially, Output.xml is empty until the first test
run populates the file.
7. In the Projects window, right-click the TestCase1 node and choose Run from the popup menu.
When the Overwrite Empty Output dialog box appears, click Yes to accept new output. The
first test run populates the Output.xml file displayed in the Source Editor.
~ 45 ~
SSASIT, Surat
The test compares the output to the contents of the output file. Because the first run of
the test has nothing to compare itself to, the first test fails. Subsequent test runs will
compare their output with the contents of Output.xml and should succeed.
8. Run the test again.
The test case is compared to the current output file and succeeds.
Problem Statement:
~ 46 ~
SSASIT, Surat
EXPERIMENT9
STEP-1)
Create WebApplication|MyWebService
SourcePackage | Create Web Service( Rigth Click ).
| Create Package Server ( Rigth Click ).
|First_Web_Service( Name Of the File).
~ 47 ~
SSASIT, Surat
/*
* First_Web_Service.java *
* Created on July 2, 2007, 10:24 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Server;
import javax.jws.WebService;
@WebService()
public class First_Web_Service {
}
STEP-2 )
First_Web_Service Right Click.
|-- go to WEB-Service Option.
|-- Add Operation.
~ 48 ~
SSASIT, Surat
STEP-3)
Name: - say Hello.
Return Type: - String
Click on OK Button.
~ 49 ~
SSASIT, Surat
STEP-4)
In First_Web_Service Class.
Return null;
change it to
SSASIT;
~ 50 ~
SSASIT, Surat
STEP-5)
Deploy Project.
~ 51 ~
SSASIT, Surat
~ 52 ~
SSASIT, Surat
~ 53 ~
SSASIT, Surat
d) Web-Service
|--- First_Web_Service ( Right Click ).
|-- Test Web-Service.
~ 54 ~
SSASIT, Surat
~ 55 ~
SSASIT, Surat
STEP-5)
a) Source Package Right Click
new - Web-Service Client .
~ 56 ~
SSASIT, Surat
~ 57 ~
SSASIT, Surat
~ 58 ~
SSASIT, Surat
Step 6)
a) Open the Java File in client Package. In that where main () is written, Right click on
main.
b) Select Web-Service client Resources
c) Call Web-Service operation.
~ 59 ~
SSASIT, Surat
~ 60 ~
SSASIT, Surat
~ 61 ~
SSASIT, Surat
OUTPUT.
Problem Statement:
~ 62 ~