Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / examples / Borland / ChatClientWnd.cpp
blobbb100e5009728b1c881584557cc67ae63a776171
1 //---------------------------------------------------------------------------
2 #include "pch.h"
3 #pragma hdrstop
4 #include "ChatClientWnd.h"
5 //---------------------------------------------------------------------------
6 #pragma package(smart_init)
7 #pragma resource "*.dfm"
8 TChatClientWindow *ChatClientWindow;
9 //---------------------------------------------------------------------------
10 __fastcall TChatClientWindow::TChatClientWindow (TComponent* Owner)
11 : TForm (Owner),
12 orb_thread_ (0)
14 try
16 nickname_ = InputBox ("Enter Nickname",
17 "Enter the nickname you would like to use:",
18 "noname");
20 if (!OpenDialog->Execute ())
21 throw Exception ("IOR file not selected - unable to continue");
22 ior_file_name_ = OpenDialog->FileName;
24 // Retrieve the ORB.
25 orb_ = CORBA::ORB_init (_argc, _argv);
27 // Get reference to the Root POA
28 CORBA::Object_var obj =
29 orb_->resolve_initial_references ("RootPOA");
30 PortableServer::POA_var poa = PortableServer::POA::_narrow (obj);
32 // Activate the POA manager
33 PortableServer::POAManager_var mgr = poa->the_POAManager ();
34 mgr->activate ();
36 // Run the orb in a separate thread
37 orb_thread_.reset (new TORBThread (orb_));
39 // set the orb in the receiver_i_ object.
40 receiver_i_.orb (orb_);
42 // read the ior from file
43 ReadIOR (ior_file_name_);
45 CORBA::Object_var server_object =
46 orb_->string_to_object (ior_.c_str ());
48 if (CORBA::is_nil (server_object.in ()))
49 throw Exception ("Invalid IOR " + ior_);
51 server_ = Broadcaster::_narrow (server_object);
53 receiver_var_ = receiver_i_._this ();
55 // Register ourselves with the server.
56 server_->add (receiver_var_, nickname_.c_str ());
58 catch (const CORBA::Exception &e)
60 ShowMessage ("CORBA Exception in TChatClientWindow constructor: "
61 + String (e._rep_id ()));
62 throw;
65 //---------------------------------------------------------------------------
66 void __fastcall TChatClientWindow::ReadIOR (String filename)
68 std::unique_ptr<TStringList> ior (new TStringList);
69 ior->LoadFromFile (filename);
70 ior_ = ior->Text;
72 //---------------------------------------------------------------------------
73 void __fastcall TChatClientWindow::WMMessageReceived (TMessage& Message)
75 String* str = (String*)Message.WParam;
76 for (int i = 1; i <= str->Length (); i++)
77 if (ACE_OS::ace_isspace ((*str)[i]))
78 (*str)[i] = ' ';
79 OutputMemo->Lines->Append (str->Trim ());
80 delete str;
82 //---------------------------------------------------------------------------
83 void __fastcall TChatClientWindow::FormClose (TObject *,
84 TCloseAction &)
86 try
88 // Remove ourselves from the server.
89 server_->remove (receiver_var_);
90 receiver_i_.shutdown ();
92 catch (const CORBA::Exception &e)
94 ShowMessage ("CORBA Exception in FormClose: " + String (e._rep_id ()));
97 //---------------------------------------------------------------------------
98 void __fastcall TChatClientWindow::InputMemoKeyPress (TObject *, char &Key)
100 if (Key == '\n' || Key == '\r')
104 // Call the server function <say> to pass the string typed by
105 // the server.
106 server_->say (receiver_var_, InputMemo->Text.c_str ());
108 catch (const CORBA::Exception &e)
110 ShowMessage ("CORBA Exception in InputMemoKeyPress: " + String (e._rep_id ()));
113 Key = 0;
114 InputMemo->Lines->Clear ();
117 //---------------------------------------------------------------------------