0% found this document useful (0 votes)
40 views11 pages

JavaRMI Report

This document explains the various steps to be implemented to design a Java Rmi stock market application.

Uploaded by

charuljain20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views11 pages

JavaRMI Report

This document explains the various steps to be implemented to design a Java Rmi stock market application.

Uploaded by

charuljain20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA RMI

Stock Market Implementation


The JAVA RMI is an interface to implement Remote Method Invocation. It
consists of four java files i.e. Interface.java Client.java Server.java and
Implemen.java.
This project implements Stock Exchange operations.
Steps to implement JAVA RMI.
1) Create the four files mentioned above as follows:
File defining the Interface.
//EchoServer.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface EchoServer extends java.rmi.Remote
{
public String echo(String msg) throws java.rmi.RemoteException;
float get_price( String symbol) throws RemoteException;
public double input (double unit, double rate) throws
RemoteException;
}

File implementing the interface.


//EchoImpl.java
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class EchoImpl extends UnicastRemoteObject implements
EchoServer

{
public EchoImpl() throws RemoteException {
super();
System.out.println("constructor");
}
public String echo(String msg) throws java.rmi.RemoteException
{
System.out.println("Received n echo");
return msg;
}
public float get_price( String symbol )
{
float price = 0;
for( int i = 0; i < symbol.length(); i++ )
{
price += (int) symbol.charAt( i );
}
price /= 5;
return price;
}
public double input ( double unit, double rate){
double price=0;
price=unit*rate;
return price;
}
}
File for defining Client
//EchoClient.java
import java.rmi.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class EchoClient {
public static final int PORT=8080;
public static void main(String[] args)

{
String[ ] uid1 = { "S1", "S2", "B1", "B2"};
String[ ] p = {"HUTCH", "Infosys","Hutch","Infosys"};
double [ ] unit = {5,6,5,6};
double [ ] noshares = {10,4,7,3};
double [ ] total = {0,0,0,0};
int port=0;
int ch;
try {
port=Integer.parseInt(args[0]);
String messg="Hello";
System.setProperty("java.security.policy", "all_policy.policy");
System.setSecurityManager(new RMISecurityManager());
String URL="rmi://localhost:"+port+"/EchoServer";
EchoServer server=(EchoServer) Naming.lookup(URL);
System.out.println(server.echo(messg));
for (int i=0; i<4; i++)
total[i] = server.input(unit[i], noshares[i]);
System.out.println("STOCK MARKET EXCHANGE:");
System.out.println("----------------------");
System.out.println("STATUS OF NSE:");
System.out.println("------------");
System.out.println("Uid Company name \t unit\t
No.of.Shares \t total:");
for (int i=0; i<4; i++)
{
System.out.println("\t"+uid1[i]+"\t"+p[i]+"\t"+unit[i]+"\t\t"+noshares[i]+
"\t\t"+total[i]);
}
BufferedReader bufferedReader = new
BufferedReader(new
InputStreamReader(System.in));
String choice = "y";
do
{

System.out.println("\n \n \t\t Menu Option:");


System.out.println("\n1.Buy\n2.Sell\n3.Display");
System.out.println("Enter ur Choice:");
ch = Integer.parseInt(bufferedReader.readLine());
switch(ch)
{
case 1:
System.out.println("Enter Buyer ID:");
String str1 = bufferedReader.readLine();
System.out.println("Enter Company
Name:");
String str2 = bufferedReader.readLine();
System.out.println("No of Shares u want to
buy:");
double doub1 =
Double.parseDouble(bufferedReader.readLine());
System.out.println("Enter the Seller id:");
String str3 = bufferedReader.readLine();
for (int i=0; i<4; i++)
{
if (uid1 [i].equals(str1))
{
if(p [i].equals(str2))
{
noshares[i] = noshares[i]
+ doub1;
total[i] = unit[i] *
noshares[i];
}
}
}
for (int i=0; i<4; i++)
{
if (uid1 [i].equals(str3))
{

if(p [i].equals(str2))
{
noshares[i] = noshares[i]
- doub1;
total[i] = unit[i] *
noshares[i];
}
}
}
break;
case 2:
System.out.println("Enter Seller ID:");
String str4 = bufferedReader.readLine();
System.out.println("Enter Company Name:");
String str5 = bufferedReader.readLine();
System.out.println("No of Shares u want to buy:");
double doub2 =
Double.parseDouble(bufferedReader.readLine());
System.out.println("Enter the Seller id:");
String str6 = bufferedReader.readLine();
for (int i=0; i<4; i++)
{
if (uid1 [i].equals(str4))
{
if(p [i].equals(str5))
{
noshares[i] = noshares[i] doub2;
total[i] = unit[i] * noshares[i];
}
}
}
for (int i=0; i<4; i++)
{
if (uid1 [i].equals(str6))

{
if(p [i].equals(str5))
{
noshares[i] = noshares[i] +
doub2;
total[i] = unit[i] * noshares[i];
}
}
}
break;
case 3:
System.out.println("\t\t Status of NSE:");
System.out.println("\t uid \t Company name \t\t
unit \tNo.of.Share\t total:");
for (int i=0; i<4; i++)
{
System.out.println("\t"+uid1[i]+"\t"+p[i]+"\t"+unit[i]+"\t\t"
+noshares[i]+"\t\t"+total[i]);
}
break;
}
System.out.println("Do you want to continue Y/N:");
choice = bufferedReader.readLine();
}while(choice.equals("y"));
System.out.println( "The price of MY COMPANY is " +
server.get_price("MY_COMPANY") );
}
catch(Exception e)
{ e.printStackTrace();
}}}
File for setting up server
//EchoServerSetup.java
import java.rmi.*;
import java.rmi.registry.*;

public class EchoServerSetup {


public static final int PORT=8080;
public static void main(String[] args)
{
int port=0;
try {
port=PORT;
Registry reg=LocateRegistry.createRegistry(port);
}
catch(RemoteException e)
{ System.out.println(e);
}
System.out.println("Registry created on port:"+port);
try {
System.setProperty("java.security.policy", "all_policy.policy");
System.setSecurityManager(new RMISecurityManager());
EchoImpl server =new EchoImpl();
String url="rmi://localhost:"+port+"/EchoServer";
Naming.rebind(url,server);
System.out.println("Succesfull");
System.out.println( "RMI StockMarketServer ready..." );}
catch(Exception e)
{
e.printStackTrace();
}}}
2.) Now open cmd.
3.) Set the current directory to the folder where you have stored the four
files.consider all files are stored in Echo Folder.
Ex. cd D:\
cd Echo
4.)Compile all the files as follows:

javac *.java
5.)Start the rmi registry by giving the command
start rmiregistry
6.) the EchoImpl.class needs to be compiled by rmic
rmic EchoImpl
7.)Now run the server by the command:
start java EchoServerSetup
8.)
Open new command prompt window.Set the current directory to Echo and
run the client.
java EchoClient
and the output will be displayed as shown in snapshots..
Snapshots..
1.) Compiling java files and starting rmiregistry

Rmiregistry window..
2.) Compile EchoImpl through rmic and run server EchoServerSetup

3.)Server.exe

3.) Running EchoClient on new cmd window.

Note: Add the following code to java.policy file by going into


C:\Program Files\Java\jre7\lib\Security\java.policy.

grant
{
Permission java.security.AllPermission;
}

You might also like