CORBA-based distributed programming (9)

zhaozj2021-02-16  49

6.2 Chat software based on CORBA technology:

This program implements the interconnection of CORBA and Tuxedo, which has made a technical foundation for the integration of the data network integrated business billing and management system (LICMS) of the joint system integrated software company.

The interface definition (IDL) of the software is as follows:

// chat.idl

Module chat {

Typedef string user;

Typedef string time;

Struct message {

User sender;

User Receiver;

Time TimeStamp;

String content;

}

Typedef sequence userlist;

Typedef sequern messageList;

Interface chatserver {

Short getonlineusers (Out UserList Onlineuses);

Short GetMessages (Out Messagelist Msglist, in User RegName);

Short registeruser (in User regName);

Short unregisteruser (in user unregname);

Short sendMessage (in message sendmsg);

}

}

The server's server-side SKELETON (skeleton) is defined as follows:

// Server.cpp: defines the entry point for the console application.

//

#include

#include "chatserverimpl.h"

#if Defined (_VIS_STD)

# include

#ELSE

# include

#ENDIF

INT Main (int Argc, char * const * argv)

{

Try {

// Initialize the ORB.

Corba :: ORB_VAR ORB = CORBA :: ORB_INIT (ARGC, ARGV);

// Get The Property Manager; NOTINE THE VALUE RETURNED

// is Not Placed INTO A 'VAR' TYPE.

VispropertyManager_ptr pm = orb-> getPropertyManager ();

PM-> AddProperty ("vbroker.se.myse.scms", "scmlist");

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.manager.type", "socket");

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.manager.connectionmax", (CORBA :: Ulong) 100);

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.manager.connectionmaxidle",

(CORBA :: Ulong) 300);

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.type", "iiop");

// We Just Add The Port Host Will Be Set To Null // And Will Be Filled with The Default Host

PM-> AddProperty ("Vbroker.se.myse.scm.scmlist.listener.port", (CORBA :: Ulong) 55000);

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.listener.proxyport", (CORBA :: Ulong) 0);

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.dispatcher.type",

"Threadpool");

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.dispatcher.threadmax", (CORBA :: Ulong) 100);

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.dispatcher.threadmin", (CORBA :: Ulong) 5);

PM-> AddProperty ("vbroker.se.myse.scm.scmlist.dispatcher.threadmaxidle",

(CORBA :: Ulong) 300);

// Get a reason to the root poa

Corba :: Object_var obj = orb-> resolve_initial_references ("rootpoa");

PortableServer :: Poa_var Rootpoa = PortablesRver :: PoA ::_narrow (obj);

// Create the Policies

Corba ::_var Seany (New Corba :: Any);

// the server_engine_policy_type requires a sequence, EVEN IF

// only one engine is being specified.

Corba :: stringsequence_var engines = new corba :: stringsequence (1 ul);

Engines-> Length (1 ul);

Engines [(CORBA :: Ulong) 0] = CORBA :: String_dup ("Myse");

Seany << = Engines;

Corba :: PolicyList_Var Policies = New Corba :: PolicyList (2UL);

Policies-> Length (2UL);

Policies [(CORBA :: Ulong) 0] = ORB-> CREATE_POLICY (Portableserverext :: Server_Engine_Policy_TYPE, Seany);

Policies [(CORBA :: Ulong) 1] = rootpoa-> create_lifespan_policy (portableserver :: personistent);

// Get the Poa Manager

PortablesRVER :: Poamanager_var POA_MANAGER = rootpoa-> the_poamanager ();

// Create Our Poa with Our Policies

PortablesRVER :: PoA_var mypoa = rootpoa-> create_poa ("chat_server_poa", POA_MANAGER,

POLICIES);

// Create the Servant

ChatServerImpl ManagerSerVant;

// Decide on the ID for the servant

PortablesRVER :: ObjectId_var managerid =

PortableServer :: string_to_objectid ("chatserver");

// Activate the servant with the ID on mypoa

Mypoa-> Activate_Object_With_ID (ManagerID, & ManagerServant);

// Obtain the Reference

CORBA :: Object_var ref = mypoa-> servant_to_reference (& managerServant);

Corba :: string_var string_ref = orb-> Object_to_string (ref.in ());

OFSTREAM REFFILE ("ref.dat");

REFFILE << string_ref << endl;

REFFILE.CLOSE ();

/ *

// Initialize the ORB.

Corba :: ORB_VAR ORB = CORBA :: ORB_INIT (ARGC, ARGV);

// Get a reason to the root poa

Corba :: Object_var obj = orb-> resolve_initial_references ("rootpoa");

PortableServer :: Poa_var Rootpoa = PortablesRver :: PoA ::_narrow (obj);

Corba :: PolicyList Policies;

Policies.length (1);

Policies [(CORBA :: Ulong) 0] = rootpoa-> create_lifespan_policy (

PortableServer :: Persistent;

// Get the Poa Manager

PortablesRVER :: Poamanager_var POA_MANAGER = rootpoa-> the_poamanager ();

// Create mypoa with the right policies

Portableserver :: POA_VAR mypoa = rootpoa-> create_poa ("chat_server_poa",

POA_MANAGER,

POLICIES);

// Create the Servant

ChatServerImpl ManagerSerVant;

// Decide on the ID for the servant

PortablesRVER :: ObjectId_var managerid =

PortableServer :: string_to_objectid ("chatserver");

// Activate the servant with the ID on mypoa

Mypoa-> Activate_Object_With_ID (ManagerID, & ManagerServant);

* /

// Activate the Poa Manager

POA_MANAGER-> Activate (); /// Wait for Incoming Requests

Cout << "ManagerServant Server Ready << ENDL;

ORB-> Run ();

}

Catch (Const Corba :: EXCEPTION & E) {

CERR << E << Endl;

Return 1;

}

Return 0;

}

The client's client STUB (stub) is defined as follows:

// ORBCLIENT.H

#if! Defined CORBA_CLIENT_H_

#define corba_client_h_

#include "chat_c.h"

// USE_STD_NS

Class corba_client {

Private:

Corba :: ORB_VAR ORB;

// PortablesRver :: ObjectId_var managerId;

Chat :: Chat Server_var chatserver;

Chat :: messagelist_var msglist;

Chat :: UserList_var usrlist;

CHAR Username [40];

INT Osagentport;

Static corba_client * _manager;

CORBA_CLIENT (INT port, char * name);

PUBLIC:

Static CORBA_CLIENT * GetInstance (int port, char * name);

Static void destroyinstance ();

~ CORBA_CLIENT ();

INT CORBA_CLIENT :: ORB_INIT ();

Int getonlineusers ();

INT GET_USER_BY_NUM (Char * Name, INT message);

Int getMessages ();

INT GET_MESSAGE_BY_NUM (Char * Sender, Char * Receiver, Char * TimeStamp, Char * Content, Int Num);

Int registeruser ();

Int unregisteruser ();

Int SendMessage (Const Char * Receiver, Const Char * Content);

}

#endif //! defined corba_client_h_

//Orbclient.cpp

#include "stdafx.h"

#include "orbclient.h"

#include

CORBA_CLIENT * CORBA_CLIENT :: _ manager = 0;

CORBA_CLIENT :: CORBA_CLIENT (INT port, char * name) {

Osagentport = port;

STRCPY (UserName, Name);

}

CORBA_CLIENT :: ~ CORBA_CLIENT () {

}

INT CORBA_CLIENT :: ORB_INIT ()

{

COUT << "Begin C ORB_INIT" << Endl;

INT ARGC = 3;

Char * argv [3];

Argv [0] = "CHAT";

Argv [1] = "-orbid";

Argv [2] = "BEA_IIOP";

Const int maxbuf = 1024;

CHAR IOR [Maxbuf]; TRY {

// Initialize the ORB.

Corba :: ORB_VAR ORB = CORBA :: ORB_INIT (ARGC, ARGV);

// default to checking

Corba :: string_var filename (CORBA :: string_dup ("ref.dat"));

// Convert from String to Object

IFStream in (filename);

In.getLine (IOR, Maxbuf);

Corba :: Object_var object = orb-> string_to_object (ior);

// Locate An Account Manager. Give The Full Poa Name and The Servant ID.

Chat Server = Chat :: ChatServer :: _ Narrow (Object);

/ *

ORB = CORBA :: ORB_INIT (ARGC, ARGV);

Timebase :: Timet Timeout = 20000000;

Corba :: timesout_value;

Timeout_Value << = timeout;

Corba :: Policy_Var Policy;

Policy = ORB-> CREATE_POLICY (Messaging :: relative_rt_timeout_policy_type, timeout_value);

COUT << "OK 1" << endl;

ManagerID = portableserver :: string_to_ObjectId ("chatserver");

COUT << "OK 2" << endl;

Chat Server = Chat :: ChatServer :: _ bind ("/ chat_server_poa", managerid;

Corba :: PolicyList Policies;

Policies.length (1);

Policies [0] = CORBA :: Policy :: _ duplicate (policy);

Corba :: Object_var Pmobj = orb-> resolve_initial_references ("ORBPolicyManager");

Corba :: PolicyManager_var orb_mgr = CORBA :: PolicyManager :: _ Narrow (PMOBJ);

ORB_MGR-> set_policy_overrides (policies, corba :: set_override);

COUT << "END C init" << endl;

* /

}

Catch (CORBA :: Object_not_exist & e)

{

Cout << "Chat Server Is Not Start Up!" << Endl;

Return -1;

}

Catch (CORBA :: EXCEPTION & E)

{

COUT << "CORBA Exception:" << E.GET_ID () << endl;

Return -1;

}

Catch (...) {

COUT << "There is Some Unknown Error While Inititial ORB" << Endl;

Return -1;

}

Return 0;

}

CORBA_CLIENT * CORBA_CLIENT :: GetInstance (int port, char * name) {

IF (! _manager)

{

_Manager = new corba_client (port, name);

#ifdef _Debug

COUT << "new corba_client" << endl;

#ENDIF

}

Return_Manager;

}

Void corba_client :: destroyInstance () {

#ifdef _Debug

Cout << "delete corba_client" << Endl;

#ENDIF

IF (_Manager)

Delete_manager;

_Manager = 0;

}

INT CORBA_CLIENT :: getonlineusers () {

IF ((chat :: chatserver_ptr) chatserver == NULL)

Return -1;

INT I;

Try {

Cout << "Start getonlineusers ..." << endl;

IF ((chat :: chatserver_ptr) Chat Server! = null)

I = ChatServer-> getonlineusers (usrlist);

Cout << "End getonlineusers ..." << Endl;

}

Catch (CORBA :: EXCEPTION & E)

{

CERR << E.GET_ID () << endl;

i = -2;

}

Return I;

}

INT CORBA_CLIENT:: GET_USER_BY_NUM (Char * Name, INT NUM)

{

IF (Num <0 || Num> USRLIST-> length ())

Return -1;

IF ((chat :: chatserver_ptr) Chat Server! = null) {

Chat :: UserList :: _ forseq_var * Item = & (* usrlist) [NUM]);

STRCPY (Name, * Item); // ??

}

Return 0;

}

INT CORBA_CLIENT :: getMessages () {

IF ((chat :: chatserver_ptr) chatserver == NULL)

Return -1;

INT I;

Try {

Cout << "Start GetMessages ..." << Endl;

IF ((chat :: chatserver_ptr) Chat Server! = null)

i = chatserver-> getMessages (msglist, username);

Cout << "End GetMessages ..." << Endl;

}

Catch (CORBA :: EXCEPTION & E)

{

CERR << E.GET_ID () << endl;

i = -2;

}

Return I;

}

INT CORBA_CLIENT:: GET_MESSAGE_BY_NUM (Char * Sender, Char * Receiver, Char * TimeStamp, Char * Content, Int Num)

{

IF (Num <0 || Num> msglist-> length ()) Return -1;

IF ((chat :: chatserver_ptr) Chat Server! = null) {

Chat :: Message * Item = & ((* msglist) [NUM]);

STRCPY (Sender, Item-> Sender);

STRCPY (Receiver, Item-> Receiver);

STRCPY (TimeStamp, Item-> TimeStamp);

Strcpy (content, item-> content);

}

Return 0;

}

INT CORBA_CLIENT :: Registeruser ()

{

IF ((chat :: chatserver_ptr) chatserver == NULL)

Return -1;

INT I;

Try {

i = chatserver-> registeruser (username);

}

Catch (CORBA :: EXCEPTION & E)

{

CERR << E.GET_ID () << endl;

i = -2;

}

Return I;

}

INT CORBA_CLIENT :: Unregisteruser ()

{

IF ((chat :: chatserver_ptr) chatserver == NULL)

Return -1;

INT I;

Try {

i = chatserver-> unregisteruser (UserName);

}

Catch (CORBA :: EXCEPTION & E)

{

CERR << E.GET_ID () << endl;

i = -2;

}

Return I;

}

INT CORBA_CLIENT :: SendMessage (Const Char * Receiver, Const Char * CONTENT)

{

IF ((chat :: chatserver_ptr) chatserver == NULL)

Return -1;

INT I;

Try {

Chat :: message msg;

Msg.sender = CORBA :: string_dup (sender);

Msg.RecEiver = CORBA :: string_dup (receiver);

Msg.content = CORBA :: string_dup (content);

I = Chat Server-> SendMessage (MSG);

}

Catch (CORBA :: EXCEPTION & E)

{

CERR << E.GET_ID () << endl;

i = -2;

}

Return I;

}

The development environment of this software uses Visual C , Visibroker version of CORBA middleware products and Tuxedo 8.1.

In addition to the function of sending messages, receiving messages, etc., it also implements many of the functions such as online users, group issuance, and sending and receiving Chinese messages, and solves the Chinese messaging problem of headache.

转载请注明原文地址:https://www.9cbs.com/read-23142.html

New Post(0)