Index: S.No Name of Program Date Remark
Index: S.No Name of Program Date Remark
Index: S.No Name of Program Date Remark
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,a[100],m,j,c[100][100],d[100],temp,s[100][100],g,r[100][100];
clrscr();
printf("\nenter the no. of process in the distributed system=");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("\nenter the no. of events in each process=");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++) {
printf("\nenter the drift velocity of each process");
scanf("%d",&d[i]);
}
printf("\the overall configuration of the distributed system is");
for(i=0;i<n;i++)
{
printf("\nthe process P[%d] has %d events in it and its drift rate is
%d",i,a[i],d[i]);
}
//for each process, entering happened before relationship of events within
that process
for(j=0;j<n;j++)
{
for(i=0;i<a[j];i++)
{
printf("\nenter the timestamp of %d event of process P[%d]",i+1,j);
scanf("%d",&c[j][i]);
}
}
//computing happened before reln. among the events of single process
for(j=0;j<n;j++)
{
for(i=0;i<a[j];i++)
{
if(c[j][i]>c[j][i+1])
{
temp=c[j][i];
c[j][i]=c[j][i+1];
c[j][i+1]=temp;
}
}
}
for(j=0;j<n;j++)
{
printf("\nthe non-decreasing order of events of a process P[%d] are",j);
for(i=0;i<a[j];i++)
{
printf("\nthe c[%d][%d] event with timestamp value %d is %d in the
process",j,i,c[j][i],i);
}
}
//considering the inter-process communication messages
printf("\nenter the no. of sending events=");
scanf("%d",&m);
for(g=0;g<m;g++)
{
printf("\nenter the timestamp index of send message event=");
scanf("%d\t%d",&s[j][i]);
s[j][i]=c[j][i];
}
for(g=0;g<m;g++)
{
printf("\nthe sending events in the distributed system are s[%d][%d] with
timestamp values %d",j,i,s[j][i]);
}
//establishing happened before relationship among the events of the processes
//using the implementation rule 1 and 2
c[j][i]=c[j][i]+d[j];//incrementing the values
//for IPC
c[j][i]=max(c[j][i]
getch();
}
Distributed Chat Server using TCP Sockets
/*
A basic extension of the java.applet.Applet class
*/
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class chatServer extends Applet
{
client clientObject;
String line;
public void init()
{
super.init();
setLayout(null);
addNotify();
resize(640,480);
txtName = new java.awt.TextField();
txtName.setText("ID");
txtName.reshape(72,12,168,27);
txtName.setFont(new Font("Dialog", Font.BOLD, 14));
txtName.setBackground(new Color(16776960));
add(txtName);
lblIp = new java.awt.Label("IP Address");
lblIp.reshape(240,12,96,24);
lblIp.setFont(new Font("Dialog", Font.BOLD, 14));
add(lblIp);
lblPort = new java.awt.Label("Port");
lblPort.reshape(480,12,48,24);
lblPort.setFont(new Font("Dialog", Font.BOLD, 14));
add(lblPort);
txtPort = new java.awt.TextField();
txtPort.setText("4321");
txtPort.reshape(528,12,72,27);
txtPort.setFont(new Font("Dialog", Font.BOLD, 14));
txtPort.setBackground(new Color(16776960));
add(txtPort);
txtArea = new java.awt.TextArea();
txtArea.setEditable(false);
txtArea.reshape(24,72,588,288);
txtArea.setBackground(new Color(65535));
add(txtArea);
btnConnect = new java.awt.Button("Connect");
btnConnect.reshape(276,432,84,34);
add(btnConnect);
txtField = new java.awt.TextField();
txtField.reshape(24,384,587,32);
txtField.setFont(new Font("Dialog", Font.BOLD, 14));
txtField.setBackground(new Color(16776960));
add(txtField);
txtIPaddr = new java.awt.TextField();
txtIPaddr.setText("161.178.121.1");
txtIPaddr.reshape(336,12,144,27);
txtIPaddr.setFont(new Font("Dialog", Font.BOLD, 14));
txtIPaddr.setBackground(new Color(16776960));
add(txtIPaddr);
label1 = new java.awt.Label("Enter your ID and press connect to begin");
label1.reshape(156,48,312,19);
label1.setFont(new Font("Dialog", Font.BOLD, 14));
label1.setBackground(new Color(12632256));
add(label1);
lblName = new java.awt.Label("Name");
lblName.reshape(12,12,60,24);
lblName.setFont(new Font("Dialog", Font.BOLD, 14));
add(lblName);
}
public boolean action(Event event, Object arg)
{
if (event.target == btnConnect)
{
btnConnect.disable();
txtField.requestFocus();
clientObject = new client(this, txtIPaddr.getText(),
txtPort.getText());
return true;
}
if (event.target == txtField)
{
line = txtField.getText();
txtField.setText("");
String lineOut = "00000000|" + txtName.getText() + "|" + line
+ "|";
clientObject.pout.println(lineOut);
return true;
}
return super.action(event, arg);
}
java.awt.TextField txtName;
java.awt.Label lblIp;
java.awt.Label lblPort;
java.awt.TextField txtPort;
java.awt.TextArea txtArea;
java.awt.Button btnConnect;
java.awt.TextField txtField;
java.awt.TextField txtIPaddr;
java.awt.Label label1;
java.awt.Label lblName;
}
class client extends Thread {
DataInputStream dis;
Socket sock;
PrintStream pout;
InputStream in;
OutputStream out;
DataInputStream din;
DataOutputStream dout;
String IPA;
chatServer myClient;
String port;
int portInt;
client(chatServer myClient, String IPA, String port) {
this.myClient = myClient;
this.IPA = IPA;
this.port = port;
portInt = Integer.valueOf(port).intValue();
start();
}
public void run() {
System.out.println("client thread started");
try {
sock = new Socket(IPA, portInt);
System.out.println("connection made");
in = sock.getInputStream();
out = sock.getOutputStream();
pout = new PrintStream(out);
din = new DataInputStream(in);
dout = new DataOutputStream(out);
while(true) {
String request = din.readLine();
myClient.txtArea.appendText(request + "\n");
}
}
catch (UnknownHostException e ) {System.out.println("can't find host"); }
catch ( IOException e ) {System.out.println("Error connecting to host");}
}
}
RPC mechanism
// Initialization of winsock
WSAStartup(wVersionRequested, &wsaData);
// Do a gethostbyname()
host = gethostbyname(argv[1]);
if (host == NULL)
{
printf(" *** ERROR - IP address for '%s' not be found \n", host_name);
exit(1);
}
// Close the *.exe file that was sent to the server (remote)
close(fh);
// Cleanup Winsock
WSACleanup();
// Initialization of winsock
WSAStartup(wVersionRequested, &wsaData);
// Accept a connection, the accept will block and then return with
// remote_addr filled in.
addr_len = sizeof(remote_addr);
remote_s = accept(local_s, (struct sockaddr*)&remote_addr, &addr_len);
// Cleanup Winsock
WSACleanup();
}
// Do forever
while(1)
{
// Winsock initialization
WSAStartup(wVersionRequested, &wsaData);
// Create a socket
remote_s = socket(AF_INET, SOCK_STREAM, 0);
// Fill-in my socket's address information and bind the socket
remote_addr.sin_family = AF_INET; // Address family to use
remote_addr.sin_port = htons(PORT_NUM); // Port number to use
remote_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP addr
bind(remote_s, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
// Close sockets
closesocket(remote_s);
closesocket(local_s);
// Cleanup Winsock
WSACleanup();
// Close sockets
closesocket(remote_s);
closesocket(local_s);
// Cleanup Winsock
WSACleanup();
The demo application is inspired by a cryptographic service where the client makes a
request for encrypting and decrypting a plaint text to a server which performs the
operations. The encryption algorithm is based on the Caesar cipher, which is one of the
simplest encryption techniques. It's a type of substitution cipher in which each letter in the
plain text is replaced by a letter, some fixed number of positions down the alphabet.
Besides this technique, I added a XOR operation with an encryption key after the shift
operation to obscure the relationship. In the image below, you can see this technique:
Caesar cipher
The next step is to implement the servant class. For this purpose, we will need to create a
class for the servant that inherits from the skeleton class, as shown below:
#include "OB/CORBA.h"
#include "crypt_skel.h"
#include <iostream>
#include <string>
{
public:
CryptographicImpl() {}
}
r[info.length() - 1] = '\0';
return r;
}
};
The servant implements the CORBA object methods functionality. The servant is called by
the skeleton when a client calls a CORBA object method implemented by the servant. The
Orbacus IDL compiler generates an empty implementation of the servant, called
cryptimpl.h.
Before ending the server side implementation, it's necessary to create an entry point that
instantiates and activates the servant class. We will have to implement a server with the
CORBA initialization. The following code explains this issue:
#include <iostream>
#include "OB/CORBA.h"
#include <OB/Cosnaming.h>
#include "crypt.h"
#include "cryptimpl.h"
3 policies[0] =
rootPOA-
>create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
// Get the POA manager object
4 PortableServer::POAManager_var manager = rootPOA->the_POAManager();
CosNaming::Name name;
name.length(1);
name[0].id = (const char *) "CryptographicService";
name[0].kind = (const char *) "";
// Bind the object into the name service
10 nc->rebind(name,o);
} catch(const CORBA::Exception& e) {
// Handles CORBA exceptions
cerr << e << endl;
return 1;
}
return 0;
}
1. Initializes the CORBA ORB.
2. Gets the root POA. The server object must be registered using an object adapter. In
this case, we register the object in the root POA. In most cases, this object adapter
is enough.
3. Set the POA policies to SINGLE_THREAD_MODEL. This POA policy manages one
request at a time, instead of multithreading, so we get it multithread-aware. If you
want to use multithread capabilities, read a CORBA book about JTCThreads. This
approach is safe and clear, but has bad performance.
4. A POA Manager is obtained. The Manager controls a set of object adapters, allowing
them to work.
10. The CORBA reference from a servant is registered in the Name Service using a
naming context, through a call to rebind.
The client side is a program that allows to communicate with the remote CORBA object
instantiated in a server. The following code is a sample of a client invoking the remote
object:
#include <iostream>
#include <string>
#include "OB/CORBA.h"
#include "OB/Cosnaming.h"
#include "crypt.h" // Include the stub
try {
// Initialize the ORB
1 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
CosNaming::Name name;
name.length(1);
name[0].id = (const char *) "CryptographicService";
name[0].kind = (const char *) "";
// Invoke the root context to retrieve the object reference
3 CORBA::Object_var managerObj = nc->resolve(name);
// Narrow the previous object to obtain the correct type
4 ::CaesarAlgorithm_ptr manager =
::CaesarAlgorithm::_narrow(managerObj.in());
string info_in,info_out,exit,dummy;
::CaesarAlgorithm::charsequence* inseq;
unsigned long key,shift;
try{
do{
cout << "\nCryptographic service client" << endl;
cout << "----------------------------" << endl;
} while (cin.fail());
} while (cin.fail());
4. Get a pointer to the CaesarAlgorithm interface from the CORBA object reference.
5. Invoke the first remote method, passing three input arguments, and return a CORBA
sequence.
6. Invoke the second remote method, passing the previous returned sequence by
value.
Once we have implemented the client and the server, it's time to connect them. Let's start
with the server. Previously, to run it, we had to call the Orbacus Name Service application
by searching for nameserv.exe. Therefore, the call must be done in this form:
Instead of using the localhost, we can use an external LAN or Internet IP address by
replacing localhost with the IP.
Objective:
Theory:
RMI applications are often comprised of two separate programs : a server and client to invoke the
events and some of object make reference to them accessible and wait for client to invoke methods on
these objects.
A typical application gets remote reference to one or more remote objects in the server and the client
and pass information back and forth.
SERVER CLIENT
Request message
do operation
.
. Get request
Reply Message
(wait)
Select object
.
Execute
(continue)
Method
Request – Reply Communication for RMI
Client Server
Skeleton of B
Request
Object A Object B Remote Objects B
Reply
Remote Reference Remote Reference
Module
Module Communication
Request
Object Proxy for Remote Object for B
A B
Reply
Role of Proxy & Skeleton in RMI
import java.io.*;
import java.net.*;
class client1 {
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
message = inFromUser.readLine();
outToServer.writeBytes(message + '\n');
return_message = inFromServer.readLine();
clientSocket.close();
}
}
import java.io.*;
import java.net.*;
class client2 {
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
message = inFromUser.readLine();
outToServer.writeBytes(message + '\n');
return_message = inFromServer.readLine();
clientSocket.close();
}
}
import java.io.*;
import java.net.*;
class server {
while(true) {
//For client1;
BufferedReader inFromClient1 =
new BufferedReader(new
InputStreamReader(connectionSocket1.getInputStream()));
DataOutputStream outToClient1 =
new DataOutputStream(connectionSocket1.getOutputStream());
//for client2;
Socket connectionSocket2 = welcomeSocket2.accept();
BufferedReader inFromClient2 =
new BufferedReader(new
InputStreamReader(connectionSocket2.getInputStream()));
DataOutputStream outToClient2 =
new DataOutputStream(connectionSocket2.getOutputStream());
client_msg1 = inFromClient1.readLine();
msg_client2 = client_msg1+ '\n';
outToClient2.writeBytes(msg_client2);
client_msg2=inFromClient2.readLine();
msg_client1=client_msg2+'\n';
outToClient1.writeBytes(msg_client1);
}
}
}