///////////////////////////////////////////////////////////// // ORBIX Example // Documentation on ORBIX and more information on this example // is at // http://www.cis.umassd.edu/~aschaefer/orbixdoc/ // or at // http://www.apl.jhu.edu/~ded/doc/ // For general information about CORBA and OMG // http://www.omg.org/ /*//////////////////////////////////////////////////////////// Summary of Programming Steps 1. Define the IDL interfaces. 2. Implement these interfaces with C++ classes, 3. Write a server main which creates instances of the classes, informs Orbix when initialisation has been done and the server is ready to accept requests. 4. Register the server, using putit. 5. Write a client main to bind to and use the server's objects. /////////////////////////////////////////////////////////////*/ // Files written by programmer ///////////////////////////////////////////////////////////// // grid.idl // compiled with // % idl -B -S grid.idl interface Grid { readonly attribute short height; readonly attribute short width; void set(in short n, in short m, in long value); long get(in short n, in short m); }; //------------------------------------------------------------ // grid.hh -- A header file to be included into the implementation // of the grid and into all clients. This defines the // C++ view of the IDL. It is created by compiler. class Grid : public virtual CORBA::Object { // Corba::Object, as provided by Orbix, has a specification // that is many pages long. public: virtual CORBA::Short height( CORBA::Environment& IT_env = CORBA::default_environment); virtual CORBA::Short width( CORBA::Environment& IT_env = CORBA::default_environment); virtual void set(CORBA::Short n, CORBA::Short m, CORBA::Long value, CORBA::Environment& IT_env = CORBA::default_environment); virtual CORBA::Long get(CORBA::Short n, CORBA::Short m, CORBA::Environment& IT_env = CORBA::default_environment); // NOTE: Here are additional declarations including ones for Grid_var // and for GridBOAImpl }; //------------------------------------------------------------ // grid_i.h -- generated by compiler with name grid.ih and // renamed by us grid_i.h class Grid_i : public virtual GridBOAImpl { CORBA::Short m_height; CORBA::Short m_width; CORBA::Long** m_a; public: // Constructor. Grid_i(CORBA::Short h, CORBA::Short w); // Destructor. virtual ~Grid_i(); // Functions corresponding to the two IDL // readonly attributes. virtual CORBA::Short width( CORBA::Environment&); virtual CORBA::Short height( CORBA::Environment&); // Functions corresponding to the two IDL // operations. virtual void set(CORBA::Short n, CORBA::Short m, CORBA::Long value, CORBA::Environment&); virtual CORBA::Long get(CORBA::Short n, CORBA::Short m, CORBA::Environment&); }; //------------------------------------------------------------ // grid_i.c - Generated by compiler, modified by programmer #include "grid_i.h" Grid_i::Grid_i(CORBA::Short h, CORBA::Short w) : m_height(h), m_width(w) { m_a = new CORBA::Long*[h]; CORBA::Short i; for (i = 0; i < h; i++) m_a[i] = new CORBA::Long[w]; } Grid_i::~Grid_i() { CORBA::Short i; for (i = 0; i < m_height; i++) delete[] m_a[i]; delete[] m_a; } CORBA::Short Grid_i::height(CORBA::Environment&) { return m_height; } CORBA::Short Grid_i::width(CORBA::Environment&) { return m_width; } void Grid_i::set(CORBA::Short n, CORBA::Short m, CORBA::Long value, CORBA::Environment&) { m_a[n][m] = value; } CORBA::Long Grid_i::get(CORBA::Short n, CORBA::Short m, CORBA::Environment&) { return m_a[n][m]; } //------------------------------------------------------------ // Srv_Main.c -- Server in which the grid object will run // impl_is_ready is the name for this server // in the implementation repository. // The server is registered with the command // % putit GridSrv // or to put it on a remote system // % putit -h GridSrv #include "grid_i.h" #include main() { // We could create any number of objects // here but we just create one. Grid_i myGrid(100,100); // Orbix objects can be explicitly named, // but this is not required in this simple // example. try { CORBA::Orbix.impl_is_ready("GridSrv"); } catch(CORBA::SystemException& se) { cerr << "Unexpected Exception: " << endl << &se; } cout << "server terminating" << endl; } /------------------------------------------------------------ // Client.c -- It will be invoked with % client #include "grid.hh" #include #include main(int argc, char *argv[]) { Grid_var gVar; CORBA::Short h, w; CORBA::Long v; try { gVar = Grid::_bind(":GridSrv", argv[1]); } catch(CORBA::SystemException& se) { cerr << "Bind to object failed" << endl; cerr << "Unexpected exception:" << endl << &se; exit(1); } try { h = gVar->height(); w = gVar->width(); } catch(CORBA::SystemException& se) { cerr << "Call to height or width failed" << endl; cerr << "Unexpected exception:" << endl << &se; exit(1); } cout << "height is " << h << endl; cout << "width is " << w << endl; try { gVar->set(2,4,123); v = gVar->get(2,4); } catch(CORBA::SystemException& se) { cerr << "Call to set or get failed" << endl; cerr << "Unexpected exception:" << endl << &se; exit(1); } } //------------------------------------------------------------