Java Tutorial

 
 
 
 
----------------------------------------------------------------------------------
                    IDL
----------------------------------------------------------------------------------
module CalciApp
{
    interface Calci
         {
               void Calculator();
               oneway void shutdown();
          };
};

-------------------------------------------------------------------------------------------------
                    Server
-------------------------------------------------------------------------------------------------

import CalciApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

import java.util.Properties;

class CalciImpl extends CalciPOA 
{
          private ORB orb;

          public void setORB(ORB orb_val) 
          {
                orb = orb_val; 
          }    
          public void Calculator() 
          {
                   System.out.print(\"Calculator Starts....\");               
          }
          public void shutdown() 
          {
                  orb.shutdown(false);
          }
}

public class CalciServer 
{
           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();  

                      CalciImpl calciImpl = new CalciImpl();
                      calciImpl.setORB(orb); 

                      org.omg.CORBA.Object ref = rootpoa.servant_to_reference(calciImpl);
                      Calci href = CalciHelper.narrow(ref);  

                     org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");
                     NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

                     String name = \"Calci\";
                     NameComponent path[] = ncRef.to_name( name );
                     ncRef.rebind(path, href);

                     System.out.println(\"Calci Server ready and waiting ...\");

                     orb.run();
               }     
               catch (Exception e) 
               {
                     System.err.println(\"ERROR: \" + e);
                     e.printStackTrace(System.out);
               }      
               System.out.println(\"Calci Server Exiting ...\");    
         }
}

-------------------------------------------------------------------------------------------------
                    Client
-------------------------------------------------------------------------------------------------

import CalciApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.awt.Graphics;
import java.awt.*;

public class CalciApplet extends java.applet.Applet
{
          TextField t1;
          Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bp,bm, bml, bd, be, bst;
          int m=0;
          int op=0;
          public void init() 
          { 
        t1=new TextField(20);
                b0=new Button(\"0\");
                b1=new Button(\"1\");
                b2=new Button(\"2\");
                b3=new Button(\"3\");
                b4=new Button(\"4\");
                b5=new Button(\"5\");
                b6=new Button(\"6\");
                b7=new Button(\"7\");
                b8=new Button(\"8\");
                b9=new Button(\"9\");
                bp=new Button(\"+\");
                bm=new Button(\"-\");
                bml=new Button(\"*\");
                bd=new Button(\"/\");
                be=new Button(\"=\");
                bst=new Button(\"AC\");

        add(t1);
                add(b0); add(b1); add(b2); add(b3);
                add(b4); add(b5); add(b6); add(b7);
                add(b8); add(b9); add(bp); add(bm);
                add(bml); add(bd); add(be); add(bst);
                t1.setText(\"0\");

                try
        {
                   ORB orb = ORB.init(this, null);

                           org.omg.CORBA.Object objRef = orb.resolve_initial_references(\"NameService\");
                        NamingContext ncRef = NamingContextHelper.narrow(objRef);

            NameComponent nc = new NameComponent(\"Calci\", \"\");
                        NameComponent path[] = {nc};    
            Calci calciRef = CalciHelper.narrow(ncRef.resolve(path));

                  calciRef.Calculator();    
        }
                catch(Exception e)
                {         
                    System.out.println(\"ERROR : \" + e) ;
                        e.printStackTrace(System.out); 
                }

       }

       public boolean action(Event e1, java.lang.Object o1)
       {
                   if(e1.arg.equals(\"0\"))
                          t1.setText(t1.getText()+\"0\");
                   if(e1.arg.equals(\"1\"))
                          t1.setText(t1.getText()+\"1\");
                   if(e1.arg.equals(\"2\"))
                          t1.setText(t1.getText()+\"2\");
                   if(e1.arg.equals(\"3\"))
                          t1.setText(t1.getText()+\"3\");
                   if(e1.arg.equals(\"4\"))
                          t1.setText(t1.getText()+\"4\");
                   if(e1.arg.equals(\"5\"))
                          t1.setText(t1.getText()+\"5\");
                   if(e1.arg.equals(\"6\"))
                           t1.setText(t1.getText()+\"6\");
                   if(e1.arg.equals(\"7\"))
                          t1.setText(t1.getText()+\"7\");
                   if(e1.arg.equals(\"8\"))
                          t1.setText(t1.getText()+\"8\");
                   if(e1.arg.equals(\"9\"))
                          t1.setText(t1.getText()+\"9\");
                   if(e1.arg.equals(\"+\"))
                   {
            m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=0;
           }
                   if(e1.arg.equals(\"-\"))
                   {
                       m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=1;
                   }
                   if(e1.arg.equals(\"*\"))
                   {
            m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=2;
                   }
                   if(e1.arg.equals(\"/\"))
                   {
                       m=Integer.parseInt(t1.getText());
                        t1.setText(\"0\");
                        op=3;
                   }
                   if(e1.arg.equals(\"=\"))
                   {
                       switch(op)
                        {
                            case 0:
                                    m=m+Integer.parseInt(t1.getText());
                                        break;
                                  case 1:
                                    m=m-Integer.parseInt(t1.getText());
                                        break;
                                  case 2:
                                        m=m*Integer.parseInt(t1.getText());
                                        break;
                                  case 3:
                    m=m/Integer.parseInt(t1.getText());
                                        break;
                         }
                         t1.setText(String.valueOf(m));
                   }
                   if(e1.arg.equals(\"AC\"))
                        t1.setText(\"0\");
                   return true;
         }       
}

-------------------------------------------------------------------------------------------------
                    Applet (HTML File)
-------------------------------------------------------------------------------------------------

<HTML><HEAD>
<TITLE>Java IDL Getting Started: Running Calculator Applet</TITLE>
</HEAD>
<BODY BGCOLOR=\"#FFFFFF\">
<H1 ALIGN=CENTER>Running the Calculator Applet</H1>
<HR><P>If all goes well, the applet appears below:<P>
<APPLET CODE=CalciApplet.class       WIDTH=500        HEIGHT=300>
<PARAM name=\"org.omg.CORBA.ORBInitialHost\" value=localhost>
<PARAM name=\"org.omg.CORBA.ORBInitialPort\" value=1050>
</APPLET>
</BODY>
</HTML>

 
 
Didn't find what you were looking for? Find more on Program that enabled applet calculator