You are on page 1of 16

Ex.

No: 1

DOWNLOAD FILES FROM VARIOUS SERVERS USING RMI Part 1 : Interface Defintion:
import java.rmi.*; public interface fileinterface extends Remote { public byte[] downloadfile(String s) throws RemoteException; }

Part 2. Interface Implementation:


import java.io.*; import java.rmi.*; import java.rmi.server.*; public class fileimplement extends UnicastRemoteObject implements fileinterface { private String name; public fileimplement(String s)throws RemoteException { super(); name=s; } public byte[] downloadfile(String fn) { try{ File fi=new File(fn); byte buf[]=new byte[(int)fi.length()]; BufferedInputStream bis=new BufferedInputStream(new FileInputStream(fn)); bis.read(buf,0,buf.length); bis.close(); return(buf); } catch(Exception ee) { System.out.println("Error:"+ee.getMessage()); ee.printStackTrace(); return(null);}}} Part 3 : Server Part:

import java.rmi.*; import java.io.*; import java.net.*; public class fileserver { public static void main(String args[]) { try{ fileimplement fi=new fileimplement("fileserver");

Naming.rebind("//127.0.0.1/fileserver",fi);} catch(Exception e){ System.out.println(" "+e.getMessage()); e.printStackTrace(); }} } Part 4: Client Part: import java.net.*; import java.rmi.*; import java.io.*; public class fileclient { public static void main(String[] args) { try{ InetAddress addr=InetAddress.getLocalHost(); String address=addr.toString().substring(addr.toString().indexOf("/")+1); String url="rmi://"+ address + "/fileserver"; fileinterface f1=(fileinterface)Naming.lookup(url); byte[] data=f1.downloadfile(args[0]); File ff=new File("f1.txt"); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(ff.getName())); bos.write(data,0,data.length); bos.flush(); bos.close();} catch(Exception e){ System.out.println(" "+e.getMessage()); e.printStackTrace(); }}} Part 5 Compile all the files as follows C:\sowmi\rmi>javac *.java C:\sowmi\rmi> Part 6 Generate stubs and skeletons as follows C:\sowmi\rmi>rmic fileimplement C:\sowmi\rmi> Part 7 : Start rmiregistry as given below . If registry is properly started, a window will be opened C:\sowmi\rmi>start rmiregistry C:\sowmi\rmi> Part 8 : Start the server

C:\sowmi\rmi>java fileserve r Part 9: Start the client C:\sowmi\rmi>java fileclient c:\devi\corba\StockMarketClient.java C:\sowmi\rmi>type f1.txt

Ex. No: 2

CREATE A JAVA BEAN TO DRAW VARIOUS GRAPHICAL SHAPES USING BDK OR WITHOUT BDK PROGRAM:
import java.awt.*; import java.applet.*; import java.awt.event.*; /*<applet code="shapes" width=400 height=400></applet>*/ public class shapes extends Applet implements ActionListener { List list; Label l1; Font f; public void init() { Panel p1=new Panel(); Color c1=new Color(255,100,230); setForeground(c1); f=new Font("Monospaced",Font.BOLD,20); setFont(f); l1=new Label("D R A W I N G V A R I O U S G R A P H I C A L S H A P E S",Label.CENTER); p1.add(l1); add(p1,"NORTH"); Panel p2=new Panel(); list=new List(3,false); list.add("Line"); list.add("Circle"); list.add("Ellipse"); list.add("Arc"); list.add("Polygon"); list.add("Rectangle"); list.add("Rounded Rectangle"); list.add("Filled Circle"); list.add("Filled Ellipse"); list.add("Filled Arc"); list.add("Filled Polygon"); list.add("Filled Rectangle"); list.add("Filled Rounded Rectangle"); p2.add(list); add(p2,"CENTER"); list.addActionListener(this); } public void actionPerformed(ActionEvent ae)

repaint(); } public void paint(Graphics g) { int i; Color c1=new Color(255,120,130); Color c2=new Color(100,255,100); Color c3=new Color(100,100,255); Color c4=new Color(255,120,130); Color c5=new Color(100,255,100); Color c6=new Color(100,100,255); if (list.getSelectedIndex()==0) { g.setColor(c1); g.drawLine(150,150,200,250); } if (list.getSelectedIndex()==1) { g.setColor(c2); g.drawOval(150,150,190,190); } if (list.getSelectedIndex()==2) { g.setColor(c3); g.drawOval(290,100,190,130); } if (list.getSelectedIndex()==3) { g.setColor(c4); g.drawArc(100,140,170,170,0,120); } if (list.getSelectedIndex()==4) { g.setColor(c5); int x[]={130,400,130,300,130}; int y[]={130,130,300,400,130}; g.drawPolygon(x,y,5); } if (list.getSelectedIndex()==5) { g.setColor(Color.cyan); g.drawRect(100,100,160,150); } if (list.getSelectedIndex()==6) { g.setColor(Color.blue) g.drawRoundRect(190,110,160,150,85,85); } if (list.getSelectedIndex()==7) { g.setColor(c2); g.fillOval(150,150,190,190); } if (list.getSelectedIndex()==8) { g.setColor(c3); g.fillOval(290,100,190,130); } if (list.getSelectedIndex()==9) { g.setColor(c4); g.fillArc(100,140,170,170,0,120); } if (list.getSelectedIndex()==10) { g.setColor(c5); int x[]={130,400,130,300,130}; int y[]={130,130,300,400,130};

g.fillPolygon(x,y,5); if (list.getSelectedIndex()==11) { g.setColor(Color.cyan); g.fillRect(100,100,160,150); }

if (list.getSelectedIndex()==12) { g.setColor(Color.blue); g.fillRoundRect(190,110,160,150,85,85); }}}

Ex. No:9 DEVELOP A COMPONENT FOR RETRIEVING STOCK MARKET EXCHANGE INFORMATION USING CORBA

// Define IDL Interface


module simplestocks{ interface StockMarket { float get_price(in string symbol); }; }; Note: Save the above module as simplestocks.idl Compile the saved module using the idlj compiler as follows . C:\devi\corba>idlj simplestocks.idl After compilation a sub directory called simplestocks same as module name will be created and it generates the following files as listed below. C:\devi\corba>cd simplestocks C:\devi\corba\simplestocks>dir Volume in drive C has no label. Volume Serial Number is 348A-27B7 Directory of C:\suji\corba\simplestocks 02/06/2007 11:38 AM <DIR> . 02/06/2007 11:38 AM <DIR> .. 02/06/2007 11:38 AM 2,071 StockMarketPOA.java 02/07/2007 02:15 PM 2,090 _StockMarketStub.java 02/07/2007 02:15 PM 865 StockMarketHolder.java 02/07/2007 02:15 PM 2,043 StockMarketHelper.java 02/07/2007 02:15 PM 359 StockMarket.java 02/07/2007 02:15 PM 339 StockMarketOperations.java 02/07/2007 02:08 PM 226 StockMarket.class 02/07/2007 02:08 PM 180 StockMarketOperations.class 02/07/2007 02:08 PM 2,818 StockMarketHelper.class 02/07/2007 02:08 PM 2,305 _StockMarketStub.class 02/06/2007 11:44 AM 2,223 StockMarketPOA.class 11 File(s) 15,519 bytes

// Implement the interface


import org.omg.CORBA.*; import simplestocks.*; public class StockMarketImpl extends StockMarketPOA{ private ORB orb; public void setORB(ORB v){orb=v;} 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 StockMarketImpl(){super();}} //Server Program import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.PortableServer.*; import org.omg.PortableServer.POA.*; import java.util.Properties;

import simplestocks.*;
public class StockMarketServer{ public static void main(String[] args) { try { ORB orb=ORB.init(args,null); POA rootpoa=POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); StockMarketImpl ss=new StockMarketImpl(); ss.setORB(orb); org.omg.CORBA.Object ref=rootpoa.servant_to_reference(ss); StockMarket hrf=StockMarketHelper.narrow(ref); org.omg.CORBA.Object orf=orb.resolve_initial_references("NameService"); NamingContextExt ncrf=NamingContextExtHelper.narrow(orf); NameComponent path[]=ncrf.to_name("StockMarket"); ncrf.rebind(path,hrf); System.out.println("StockMarket server is ready"); //Thread.currentThread().join(); orb.run();}catch(Exception e){ e.printStackTrace();}}}

// Client Program
import org.omg.CORBA.*; import org.omg.CosNaming.*; import simplestocks.*; import org.omg.CosNaming.NamingContextPackage.*; public class StockMarketClient{ public static void main(String[] args) { try { ORB orb=ORB.init(args,null); NamingContextExt ncRef=NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService")) //NameComponent path[]={new NameComponent("NASDAQ","")}; StockMarket market=StockMarketHelper.narrow(ncRef.resolve_str("StockMarket"));

System.out.println("Price of My company is"+market.get_price("My_COMPANY"));} catch(Exception e){ e.printStackTrace();}}}

Ex: No: 10 DEVELOP A MIDDLEWARECOMPONENT FOR RETRIEVING WEATHER FORECAST INFORMATION USING CORBA

// Define IDL Interface

module weather{ interface forecast { float get_min(); float get_max();};};


Note: Save the above module as weather.idl
Compile the saved module using the idlj compiler as follows . C:\sowmi\weather>idlj weather.idl After compilation a sub directory called weather same as module name will be created and it generates the following files as listed below. C:\sowmi\weather>cd weather C:\sowmi\weather\weather>dir Volume in drive C has no label. Volume Serial Number is 348A-27B7 Directory of C:\suji\weather\weather 03/14/2007 02:52 PM <DIR> . 03/14/2007 02:52 PM <DIR> .. 03/14/2007 02:55 PM 2,240 forecastPOA.java 03/14/2007 02:55 PM 2,729 _forecastStub.java 03/14/2007 02:55 PM 796 forecastHolder.java 03/14/2007 02:55 PM 1,926 forecastHelper.java 03/14/2007 02:55 PM 330 forecast.java 03/14/2007 02:55 PM 319 forecastOperations.java 03/15/2007 10:42 AM 2,144 forecastPOA.class 03/15/2007 10:42 AM 167 forecastOperations.class 03/15/2007 10:42 AM 207 forecast.class 03/15/2007 10:42 AM 2,724 forecastHelper.class 03/15/2007 10:42 AM 2,403 _forecastStub.class 11 File(s) 15,985 bytes 2 Dir(s) 1,726,103,552 bytes free

// Implement the interface


import org.omg.CORBA.*; import weather.*; import java.util.*; public class weatherimpl extends forecastPOA { private ORB orb; int r[]=new int[10]; float s[]=new float[10]; Random rr=new Random(); public void setORB(ORB v){orb=v;} public float get_min() { for(int i=0;i<10;i++) { r[i]=Math.abs(rr.nextInt()); s[i]=Math.round((float)r[i]*0.00000001); } float min; min=s[0]; for(int i=1;i<10;i++) { if (min>s[i]) min =s[i]; } float mintemp=Math.abs((float)(min-32)*5/9); return mintemp;} public float get_max() { for(int i=0;i<10;i++) { r[i]=Math.abs(rr.nextInt()); s[i]=Math.round((float)r[i]*0.00000001); float max; max=s[0]; for(int i=1;i<10;i++) { if (max<s[i]) max =s[i]; } float maxtemp=Math.abs((float)(max-32)*5/9); return maxtemp;} public weatherimpl(){super();}}

//Server Program import org.omg.CORBA.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.PortableServer.*; import org.omg.PortableServer.POA.*; import java.util.Properties; import weather.*; public class weatherserver{ public static void main(String[] args) { try { ORB orb=ORB.init(args,null); POA rootpoa=POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); weatherimpl ss=new weatherimpl(); ss.setORB(orb); org.omg.CORBA.Object ref=rootpoa.servant_to_reference(ss); forecast hrf=forecastHelper.narrow(ref); org.omg.CORBA.Object orf=orb.resolve_initial_references("NameService"); NamingContextExt ncrf=NamingContextExtHelper.narrow(orf); NameComponent path[]=ncrf.to_name("forecast"); ncrf.rebind(path,hrf); System.out.println("weather server is ready"); orb.run(); } catch(Exception e){ e.printStackTrace();}}}

// Client Program
import org.omg.CORBA.*; import org.omg.CosNaming.*; import weather.*; import org.omg.CosNaming.NamingContextPackage.*; import java.util.*; public class weatherclient { public static void main(String[] args) { String city[]={"Chennai ","Trichy ","Madurai ","Coimbatore","Salem Calendar cc=Calendar.getInstance(); try { ORB orb=ORB.init(args,null);

"};

NamingContextExt ncRef=NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService")); forecast fr=forecastHelper.narrow(ncRef.resolve_str("forecast")); System.out.println("\t\t\tW E A T H E R F O R E C A S T"); System.out.println("\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println(); System.out.println("\tDATE TIME CITY HIGHEST LOWEST "); System.out.println("\t TEMPERATURE TEMPERATURE"); for(int i=0;i<5;i++) { System.out.print("\t"+cc.get(Calendar.DATE)+cc.get(Calendar.MONTH)+cc.get(Calendar.YEA R)+" "+cc.get(Calendar.HOUR)+" "+city[i]+" "+" "); System.out.print(Math.floor(fr.get_min())+"\t \t"+Math.ceil(fr.get_max())); System.out.println(); }} catch(Exception e){ e.printStackTrace();}}}

Ex.No.3A IMPLEMENTATION OF 2D PROGRAM: #include<stdio.h> #include<graphics.h> #include<conio.h> int cliptest(float p,float q,float *u1,float *u2) { float r; int g; g=1; if(p<0.0) { r=q/p; if(r>*u2) g=0; else if(r>*u1) *u1=r; } else if(p>0.0) { r=q/p; if(r<*u1) g=0; else if(r<*u2) *u2=r; } else if(q<0.0) g=0; return(g); } void main() { int wx1,wx2,wy1,wy2,lx1,lx2,ly1,ly2; int d=DETECT,m; float dx,dy,u1,u2; initgraph(&d,&m,"E:\\tc\\bgi"); printf("\n Enter the window coordinates:"); scanf("%d%d%d%d",&wx1,&wy1,&wx2,&wy2);

CLIPPING

printf("\n Enter the line coordinates:"); scanf("%d%d%d%d",&lx1,&ly1,&lx2,&ly2); printf("\n Before clipping:"); rectangle(wx1,wy1,wx2,wy2); line(lx1,ly1,lx2,ly2); getch(); u1=0.0; u2=1.0; dx=lx2-lx1; if(cliptest(-dx,lx1-wx1,&u1,&u2)) if(cliptest(dx,wx2-lx1,&u1,&u2)) { dy=ly2-ly1; if(cliptest(-dy,ly1-wy1,&u1,&u2)) if(cliptest(dy,wy2-ly1,&u1,&u2)) { if(u1<1.0) lx2=lx1+u2*dx; ly2=ly1+u2*dy; } if(u1>0.0) { lx1=lx1+u1*dx; ly1=ly1+u1*dy; } cleardevice(); printf("\n After clipping:"); rectangle(wx1,wy1,wx2,wy2); line(lx1,ly1,lx2,ly2); } getch(); }

OUTPUT:
Enter the Window Co-Ordinate : 100 100 200 200 Enter the Line Co-Ordinate : 100 100 300 300 BEFORE CLIPPING

AFTER CLIPPING

You might also like