Skip to content

Instantly share code, notes, and snippets.

@mbapna
Last active August 28, 2017 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbapna/016fabb5fde0e9893f47ea80da2e8db6 to your computer and use it in GitHub Desktop.
Save mbapna/016fabb5fde0e9893f47ea80da2e8db6 to your computer and use it in GitHub Desktop.
Hospital Inventory RMI (Server and Client)
RMI Client Side
import java.rmi.*;
import java.rmi.server.*;
public class room0{
public static void main(String[] args){
room0 Roomzero=new room0();
Roomzero.addtoanotherroom(0,1,"ventilators",2);
/*Any methods that need to be called should be called from here*/
/*Roomzero.addfrom(…….);
/*Roomzero.gettable();*/
}
public String addtoanotherroom(int room1int,int room2int, String item1, int numberofitems){
try {
if (room1int != 0) {
System.out.print("Can only affect your own room!");
return "Can only affect your own room!";
}
RemoteInterface service = (RemoteInterface) Naming.lookup("rmi://localhost/Hospitalsys");
String stringroom0=service.addtoanotherroom(0, room2int, item1, numberofitems);
System.out.print(stringroom0);
return stringroom0;
}
catch(Exception ex){
/*Must throw exception because method can throw RemoteException*/
return "Could not complete";
}}
public String addtoownroom(int room1int,int room2int, String item1, int numberofitems){
try{
if (room1int != 0) {return "Can only affect your own room!";}
RemoteInterface service = (RemoteInterface)Naming.lookup("rmi://127.0.0.1/Hospitalsys");
String stringroom0=service.addtoownroom(0, room2int, item1, numberofitems);
System.out.print(stringroom0);
return stringroom0;
}
catch(Exception ex){
return("Could not complete");
}}
public int[][] totable(){
try{
RemoteInterface service = (RemoteInterface)Naming.lookup("rmi://127.0.0.1/Hospitalsys");
int[][]table1= service.totable();
for (int i = 0; i < table1.length; i++) {
for (int j = 0; j < table1[i].length; j++) {
System.out.print(table1[i][j] + " ");
}
System.out.println();
}
return table1;
}
catch(Exception ex){
int[][] a=new int[1][1];
a[0][0]=1;
return a;
}}}
RMI Server Side
Room:
public class Room {
public int ventilators=5;
public int dialysismachines=3;
public int heartlungmachines=2;
}
RemoteInterface:
import java.rmi.*;
public interface RemoteInterface extends Remote{
public String addtoanotherroom(int room1int,int room2int, String item, int numberofitems) throws RemoteException;
public String addtoownroom(int room1int,int room2int, String item, int numberofitems) throws RemoteException;
public int[][] totable() throws RemoteException;
}
NegativeNumberException:
import java.io.*;
public class negativenumberexception extends Exception{
public negativenumberexception(){
super("Can’t be a negative number");
}}
Hospital:
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
/*Hospital System objects will be remote objects. The methods implement the Remote Interface*/
public class Hospital extends UnicastRemoteObject implements RemoteInterface{
static int roomoriginalitem=0;
static int numberofrooms=12;
static List<Room> hospital1 = new ArrayList<Room>();
public Hospital() throws RemoteException{
}
public static void main(String[]args){
try{
/*Initialize a hospital with 12 rooms. */
for(int i=1;i<numberofrooms;i++) {
hospital1.add(new Room());
}
RemoteInterface service=new Hospital();
Naming.rebind("Hospitalsys",service);
}
catch(Exception e){
e.printStackTrace();
}}
/*Must be synchronized so that negative number exception is only caught when needed. Adding (numberofitems) item to room 2.*/
public synchronized String addtoanotherroom(int room1int,int room2int, String item1, int numberofitems) {
try{
Room room1=hospital1.get(room1int);
Room room2=hospital1.get(room2int);
if (item1.toLowerCase()=="ventilators"||item1.toLowerCase()=="dialysismachines"||item1.toLowerCase()=="heartlungmachines"){
if (item1.toLowerCase()=="ventilators")
roomoriginalitem=room1.ventilators;
room1.ventilators=room1.ventilators-numberofitems;
if (item1.toLowerCase()=="dialysismachines") {
roomoriginalitem=room1.dialysismachines;
room1.dialysismachines=room1.dialysismachines-numberofitems;}
if (item1.toLowerCase()=="heartlungmachines") {
roomoriginalitem=room1.heartlungmachines;
room1.heartlungmachines=room1.heartlungmachines-numberofitems;}
if (roomoriginalitem-numberofitems<0 && item1.toLowerCase()=="ventilators") {
room1.ventilators=roomoriginalitem;
throw new negativenumberexception();}
if (roomoriginalitem-numberofitems<0 && item1.toLowerCase()=="dialysismachines") {
room1.dialysismachines=roomoriginalitem;
throw new negativenumberexception();}
if (roomoriginalitem-numberofitems<0 && item1.toLowerCase()=="heartlungmachines") {
room1.heartlungmachines=roomoriginalitem;
/*Used in case needed to refer to original value if transaction can’t take place*/
throw new negativenumberexception();}
if (item1.toLowerCase()=="ventilators")
room2.ventilators=room2.ventilators+numberofitems;
if (item1.toLowerCase()=="dialysismachines")
room2.dialysismachines=room2.dialysismachines+numberofitems;
if (item1.toLowerCase()=="heartlungmachines") {
room2.heartlungmachines=room2.heartlungmachines+numberofitems; }
/*The room where the item was originally in loses items. The room where the item was placed gains the same number of items.*/
/*No negative amount of items. Success!*/
return" Success";
}
else{
return "Invalid entry entered";
}}
catch(negativenumberexception e){
/*No changes are made to table if this exception is called*/
return "Can’t be negative items in a room";
}}
public synchronized String addtoownroom(int room2int,int room1int, String item1, int numberofitems) {
try{
Room room1=hospital1.get(room1int);
Room room2=hospital1.get(room2int);
if (item1.toLowerCase()=="ventilators"||item1.toLowerCase()=="dialysismachines"||item1.toLowerCase()=="heartlungmachines"){
if (item1.toLowerCase()=="ventilators")
roomoriginalitem=room1.ventilators;
room1.ventilators=room1.ventilators-numberofitems;
if (item1.toLowerCase()=="dialysismachines") {
roomoriginalitem=room1.dialysismachines;
room1.dialysismachines=room1.dialysismachines-numberofitems;}
if (item1.toLowerCase()=="heartlungmachines") {
roomoriginalitem=room1.heartlungmachines;
room1.heartlungmachines=room1.heartlungmachines-numberofitems;}
if (roomoriginalitem-numberofitems<0 && item1.toLowerCase()=="ventilators") {
room1.ventilators=roomoriginalitem;
throw new negativenumberexception();}
if (roomoriginalitem-numberofitems<0 && item1.toLowerCase()=="dialysismachines") {
room1.dialysismachines=roomoriginalitem;
throw new negativenumberexception();}
if (roomoriginalitem-numberofitems<0 && item1.toLowerCase()=="heartlungmachines") {
room1.heartlungmachines=roomoriginalitem;
/*Used in case needed to refer to original value if transaction can’t take place*/
throw new negativenumberexception();}
if (item1.toLowerCase()=="ventilators")
room2.ventilators=room2.ventilators+numberofitems;
if (item1.toLowerCase()=="dialysismachines")
room2.dialysismachines=room2.dialysismachines+numberofitems;
if (item1.toLowerCase()=="heartlungmachines") {
room2.heartlungmachines=room2.heartlungmachines+numberofitems; }
/*The room where the item was originally in loses items. The room where the item was placed gains the same number of items.*/
/*No negative amount of items. Success!*/
return" Success";
}
else{
return "Invalid number entered";
}}
catch(negativenumberexception e){
/*No changes are made to table if this exception is called*/
return "Can’t be negative items in a room";
}}
/*Displays 3*12 matrix of items in each room*/
public int[][] totable(){
int[][] table=new int[3][];
for (int rooms=0; rooms<numberofrooms; rooms++) {
for (int itemofintrigue=0; itemofintrigue<3; itemofintrigue++) {
if (itemofintrigue==0)
table[itemofintrigue][rooms]=hospital1.get(rooms).ventilators;
if (itemofintrigue==1)
table[itemofintrigue][rooms]=hospital1.get(rooms).dialysismachines;
if (itemofintrigue==2)
table[itemofintrigue][rooms]=hospital1.get(rooms).heartlungmachines;
}}
return table;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment