diff options
Diffstat (limited to 'DevGuideExamples/LocalObjects')
15 files changed, 629 insertions, 0 deletions
diff --git a/DevGuideExamples/LocalObjects/Messenger/Messenger.idl b/DevGuideExamples/LocalObjects/Messenger/Messenger.idl new file mode 100644 index 00000000000..c081d4e7d6c --- /dev/null +++ b/DevGuideExamples/LocalObjects/Messenger/Messenger.idl @@ -0,0 +1,10 @@ +// $Id$ + +// Messenger.idl + +local interface Messenger +{ + boolean send_message (in string user_name, + in string subject, + inout string message); +}; diff --git a/DevGuideExamples/LocalObjects/Messenger/Messenger.mpc b/DevGuideExamples/LocalObjects/Messenger/Messenger.mpc new file mode 100644 index 00000000000..df7e7ecddd0 --- /dev/null +++ b/DevGuideExamples/LocalObjects/Messenger/Messenger.mpc @@ -0,0 +1,23 @@ +// $Id$ + +project(LocalObject*idl): taoidldefaults { + IDL_Files { + Messenger.idl + } + custom_only = 1 +} + +project(LocalObject*Server): taoserver { + exename = MessengerServer + after += LocalObject*idl + Source_Files { + Messenger_i.cpp + MessengerServer.cpp + } + Source_Files { + MessengerC.cpp + MessengerS.cpp + } + IDL_Files { + } +} diff --git a/DevGuideExamples/LocalObjects/Messenger/MessengerServer.cpp b/DevGuideExamples/LocalObjects/Messenger/MessengerServer.cpp new file mode 100644 index 00000000000..ca25998b65b --- /dev/null +++ b/DevGuideExamples/LocalObjects/Messenger/MessengerServer.cpp @@ -0,0 +1,28 @@ +// $Id$ + +#include "Messenger_i.h" +#include <iostream> +int ACE_TMAIN (int, ACE_TCHAR **) +{ + try { + // Construct a Messenger object and use it "as if" it's a corba object. + // Put it into CORBA object reference + // comparable to activation, narrow, etc. + Messenger_var messenger(new Messenger_i); + + // Send a message the the Messenger object. + CORBA::String_var message = CORBA::string_dup ("Hello!"); + messenger->send_message("TAO User", "TAO Test", message.inout()); + + // Print the Messenger's reply. + std::cout << "Reply: " << message.in() << std::endl; + } + catch(const CORBA::Exception& ex) { + std::cerr << "Caught CORBA::Exception : " << ex << std::endl; + return 1; + } + + return 0; +} + + diff --git a/DevGuideExamples/LocalObjects/Messenger/Messenger_i.cpp b/DevGuideExamples/LocalObjects/Messenger/Messenger_i.cpp new file mode 100644 index 00000000000..27d02a450dc --- /dev/null +++ b/DevGuideExamples/LocalObjects/Messenger/Messenger_i.cpp @@ -0,0 +1,30 @@ +// $Id$ + +#include "Messenger_i.h" +#include <iostream> + +// Implementation skeleton constructor +Messenger_i::Messenger_i (void) +{ +} + +// Implementation skeleton destructor +Messenger_i::~Messenger_i (void) +{ +} + +CORBA::Boolean Messenger_i::send_message ( + const char * user_name, + const char * subject, + char *& message + ) +{ + std::cout << "Message from: " << user_name << std::endl; + std::cout << "Subject: " << subject << std::endl; + std::cout << "Message: " << message << std::endl; + CORBA::string_free(message); + message = CORBA::string_dup("Thanks for the message."); + return true; + +} + diff --git a/DevGuideExamples/LocalObjects/Messenger/Messenger_i.h b/DevGuideExamples/LocalObjects/Messenger/Messenger_i.h new file mode 100644 index 00000000000..92d6825db32 --- /dev/null +++ b/DevGuideExamples/LocalObjects/Messenger/Messenger_i.h @@ -0,0 +1,32 @@ +// $Id$ + +#ifndef MESSENGERI_H_ +#define MESSENGERI_H_ + +#include "MessengerS.h" +#include "tao/LocalObject.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +#pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +//Class Messenger_i +class Messenger_i : public virtual Messenger, + public virtual ::CORBA::LocalObject +{ +public: + //Constructor + Messenger_i (void); + + //Destructor + virtual ~Messenger_i (void); + +virtual CORBA::Boolean send_message ( + const char * user_name, + const char * subject, + char *& message + ); + +}; + +#endif /* MESSENGERI_H_ */ diff --git a/DevGuideExamples/LocalObjects/Messenger/run_test.pl b/DevGuideExamples/LocalObjects/Messenger/run_test.pl new file mode 100755 index 00000000000..877c4fe6398 --- /dev/null +++ b/DevGuideExamples/LocalObjects/Messenger/run_test.pl @@ -0,0 +1,27 @@ +eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' + & eval 'exec perl -S $0 $argv:q' + if 0; + +# -*- perl -*- +# +# $Id$ + +use lib "$ENV{ACE_ROOT}/bin"; +use PerlACE::TestTarget; + +my $status = 0; + +my $server = PerlACE::TestTarget::create_target (1) || die "Create target 1 failed\n"; + +$SV = $server->CreateProcess ("MessengerServer"); + +$SV->Spawn (); + +$process_status = $SV->WaitKill ($server->ProcessStartWaitInterval()); + +if ($process_status != 0) { + print STDERR "ERROR: server returned $process_status\n"; + $status = 1; +} + +exit $status; diff --git a/DevGuideExamples/LocalObjects/ServantLocator/Messenger.idl b/DevGuideExamples/LocalObjects/ServantLocator/Messenger.idl new file mode 100644 index 00000000000..63a282a1850 --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/Messenger.idl @@ -0,0 +1,10 @@ +// $Id$ + +// Messenger.idl + +interface Messenger +{ + boolean send_message (in string user_name, + in string subject, + inout string message); +}; diff --git a/DevGuideExamples/LocalObjects/ServantLocator/MessengerClient.cpp b/DevGuideExamples/LocalObjects/ServantLocator/MessengerClient.cpp new file mode 100644 index 00000000000..8eb8b435997 --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/MessengerClient.cpp @@ -0,0 +1,71 @@ +// $Id$ + +#include "MessengerC.h" +#include <iostream> +#include "ace/Get_Opt.h" + +const ACE_TCHAR *ior = ACE_TEXT ("file://test.ior"); + +int +parse_args (int argc, ACE_TCHAR *argv[]) +{ + ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:")); + int c; + + while ((c = get_opts ()) != -1) + switch (c) + { + case 'k': + ior = get_opts.opt_arg (); + break; + + case '?': + default: + ACE_ERROR_RETURN ((LM_ERROR, + "usage: %s " + "-k <ior> " + "\n", + argv [0]), + -1); + } + // Indicates successful parsing of the command line + return 0; +} + +int ACE_TMAIN (int argc, ACE_TCHAR *argv []) +{ + try { + // Initialize the ORB + CORBA::ORB_var orb = CORBA::ORB_init (argc, argv); + + if (parse_args (argc, argv) != 0) + return 1; + + // Converts the contents of the file to an object reference + CORBA::Object_var obj = orb->string_to_object (ior); + if (CORBA::is_nil (obj.in())) { + std::cerr << "Nill Messenger reference" << std::endl; + return 1; + } + + // Narrow the object reference to a Messenger object reference + Messenger_var messenger = Messenger::_narrow(obj.in()); + if (CORBA::is_nil (messenger.in())) { + std::cerr << "Not a Messenger object reference" << std::endl; + return 1; + } + + // Create a message and send it to the Messenger + CORBA::String_var message = CORBA::string_dup("Hello!"); + messenger->send_message ("TAO User", "TAO Test", message.inout()); + + orb->destroy(); + std::cout << "Message was sent" << std::endl; + } + catch(const CORBA::Exception& ex) { + std::cerr << "Client Caught a CORBA exception: " << ex << std::endl; + return 1; + } + std::cout << "Message was sent" << std::endl; + return 0; +} diff --git a/DevGuideExamples/LocalObjects/ServantLocator/MessengerLocator_i.cpp b/DevGuideExamples/LocalObjects/ServantLocator/MessengerLocator_i.cpp new file mode 100644 index 00000000000..876fc1e4ac3 --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/MessengerLocator_i.cpp @@ -0,0 +1,54 @@ +// $Id$ + +#include "MessengerLocator_i.h" +#include "Messenger_i.h" + +#include "ace/SString.h" +#include <iostream> + +Messenger_Locator_i::Messenger_Locator_i() +{ +} + +PortableServer::Servant +Messenger_Locator_i::preinvoke (const PortableServer::ObjectId &oid, + PortableServer::POA_ptr, + const char *, + void * & cookie ) +{ + // Get the ObjectId in string format. + CORBA::String_var oid_str = PortableServer::ObjectId_to_string (oid); + + std::cout << "preinvoke called..." << oid_str << std::endl; + + // Check if the ObjectId is valid. + ACE_CString cstr(oid_str.in()); + if (cstr == "Messenger") { + // Create the required servant + PortableServer::ServantBase_var servant = new Messenger_i (); + + // Set a flag so that we know to delete it in postinvoke(). + cookie = (void *)1; + + return servant._retn(); + } + else { + throw CORBA::OBJECT_NOT_EXIST (); + } +} + +void +Messenger_Locator_i::postinvoke (const PortableServer::ObjectId &, + PortableServer::POA_ptr, + const char *, + void * cookie, + PortableServer::Servant servant) +{ + + std::cout << "postinvoke called..." << std::endl; + + // Delete the servant as it is no longer needed. + if (cookie != 0) { + delete servant; + } +} diff --git a/DevGuideExamples/LocalObjects/ServantLocator/MessengerLocator_i.h b/DevGuideExamples/LocalObjects/ServantLocator/MessengerLocator_i.h new file mode 100644 index 00000000000..c2dc71e773e --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/MessengerLocator_i.h @@ -0,0 +1,30 @@ +// $Id$ + +#ifndef MESSENGER_LOCATOR_I_H +#define MESSENGER_LOCATOR_I_H + +#include "tao/corba.h" +#include "tao/PortableServer/PortableServer.h" +#include "tao/PortableServer/ServantLocatorC.h" + +class Messenger_Locator_i : public PortableServer::ServantLocator, + public CORBA::LocalObject +{ + public: + Messenger_Locator_i(); + + // Preinvoke function + virtual PortableServer::Servant preinvoke (const PortableServer::ObjectId &oid, + PortableServer::POA_ptr poa, + const char * operation, + void * & cookie); + + // Postinvoke function + virtual void postinvoke (const PortableServer::ObjectId & oid, + PortableServer::POA_ptr poa, + const char * operation, + void * cookie, + PortableServer::Servant servant); +}; + +#endif diff --git a/DevGuideExamples/LocalObjects/ServantLocator/MessengerServer.cpp b/DevGuideExamples/LocalObjects/ServantLocator/MessengerServer.cpp new file mode 100644 index 00000000000..01f8018f71b --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/MessengerServer.cpp @@ -0,0 +1,110 @@ +// $Id$ + +#include "MessengerLocator_i.h" +#include "MessengerC.h" + +#include "tao/AnyTypeCode/TypeCode.h" +#include <iostream> +#include <fstream> +#include "ace/Get_Opt.h" + +const ACE_TCHAR *ior_output_file = ACE_TEXT ("Messenger.ior"); + +int +parse_args (int argc, ACE_TCHAR *argv[]) +{ + ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:")); + int c; + + while ((c = get_opts ()) != -1) + switch (c) + { + case 'o': + ior_output_file = get_opts.opt_arg (); + break; + + case '?': + default: + ACE_ERROR_RETURN ((LM_ERROR, + "usage: %s " + "-o <iorfile>" + "\n", + argv [0]), + -1); + } + // Indicates sucessful parsing of the command line + return 0; +} + +int ACE_TMAIN (int argc, ACE_TCHAR *argv []) +{ + try { + // Initialize the ORB + CORBA::ORB_var orb = CORBA::ORB_init(argc, argv); + + if (parse_args (argc, argv) != 0) + return 1; + + // Get a reference to the POA + CORBA::Object_var obj = orb->resolve_initial_references("RootPOA"); + PortableServer::POA_var rootPOA = PortableServer::POA::_narrow (obj.in()); + + // Active the POA Manager + PortableServer::POAManager_var mgr = rootPOA->the_POAManager(); + mgr->activate(); + + // Create the policies and assign them for the child POA + CORBA::PolicyList policies(3); + policies.length(3); + + policies[0] = rootPOA->create_id_assignment_policy(PortableServer::USER_ID); + policies[1] = rootPOA->create_request_processing_policy(PortableServer::USE_SERVANT_MANAGER); + policies[2] = rootPOA->create_servant_retention_policy(PortableServer::NON_RETAIN); + + // Create the POA with these policies + PortableServer::POA_var childPOA = rootPOA->create_POA("childPOA", mgr.in(), policies); + + // Destroy the policy objects + for (CORBA::ULong i = 0; i != policies.length(); ++i) { + policies[i]->destroy(); + } + + // Create our Messenger's ServantLocator. + PortableServer::ServantLocator_var locator = new Messenger_Locator_i; + + // Set the Servant Manager with the childPOA. + childPOA->set_servant_manager(locator.in()); + + // Get the object id for the user-created ID in the childPOA. + PortableServer::ObjectId_var child_oid = PortableServer::string_to_ObjectId("Messenger"); + + // Create the object without creating a servant. + CORBA::Object_var messenger_obj = + childPOA->create_reference_with_id(child_oid.in(), ::_tc_Messenger->id()); + + // Put the object reference into an IOR string + CORBA::String_var str = orb->object_to_string(messenger_obj.in()); + + // Write the IOR string to a file + std::ofstream iorFile(ACE_TEXT_ALWAYS_CHAR(ior_output_file)); // Throws exception if there's a problem. + iorFile << str.in(); + iorFile.close(); + + std::cout << "IOR written to the file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl; + + // Accept requests from clients. + orb->run(); + + // Release resources + rootPOA->destroy(true,true); + orb->destroy(); + } + catch(const CORBA::Exception& ex) { + std::cerr << "Server Caught a CORBA::Exception: " << ex << std::endl; + return 1; + } + + return 0; +} + + diff --git a/DevGuideExamples/LocalObjects/ServantLocator/Messenger_i.cpp b/DevGuideExamples/LocalObjects/ServantLocator/Messenger_i.cpp new file mode 100644 index 00000000000..dce1646bb24 --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/Messenger_i.cpp @@ -0,0 +1,45 @@ +// -*- C++ -*- +// +// $Id$ + +// **** Code generated by the The ACE ORB (TAO) IDL Compiler **** +// TAO and the TAO IDL Compiler have been developed by: +// Center for Distributed Object Computing +// Washington University +// St. Louis, MO +// USA +// http://www.cs.wustl.edu/~schmidt/doc-center.html +// and +// Distributed Object Computing Laboratory +// University of California at Irvine +// Irvine, CA +// USA +// http://doc.ece.uci.edu/ +// +// Information about TAO is available at: +// http://www.cs.wustl.edu/~schmidt/TAO.html + +#include "Messenger_i.h" +#include <iostream> +// Implementation skeleton constructor +Messenger_i::Messenger_i (void) + { + } + +// Implementation skeleton destructor +Messenger_i::~Messenger_i (void) +{ +} + +CORBA::Boolean +Messenger_i::send_message ( + const char * user_name, + const char * subject, + char *& message) +{ + std::cout << "Message from: " << user_name << std::endl; + std::cout << "Subject: " << subject << std::endl; + std::cout << "Message: " << message << std::endl; + return true; +} + diff --git a/DevGuideExamples/LocalObjects/ServantLocator/Messenger_i.h b/DevGuideExamples/LocalObjects/ServantLocator/Messenger_i.h new file mode 100644 index 00000000000..9af2c134dc2 --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/Messenger_i.h @@ -0,0 +1,48 @@ +// -*- C++ -*- +// +// $Id$ + +// **** Code generated by the The ACE ORB (TAO) IDL Compiler **** +// TAO and the TAO IDL Compiler have been developed by: +// Center for Distributed Object Computing +// Washington University +// St. Louis, MO +// USA +// http://www.cs.wustl.edu/~schmidt/doc-center.html +// and +// Distributed Object Computing Laboratory +// University of California at Irvine +// Irvine, CA +// USA +// http://doc.ece.uci.edu/ +// +// Information about TAO is available at: +// http://www.cs.wustl.edu/~schmidt/TAO.html + +#ifndef MESSENGERI_H_ +#define MESSENGERI_H_ + +#include "MessengerS.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +#pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +//Class Messenger_i +class Messenger_i : public virtual POA_Messenger +{ +public: + //Constructor + Messenger_i (void); + + //Destructor + virtual ~Messenger_i (void); + + virtual CORBA::Boolean send_message ( + const char * user_name, + const char * subject, + char *& message); +}; + + +#endif /* MESSENGERI_H_ */ diff --git a/DevGuideExamples/LocalObjects/ServantLocator/ServantLocator.mpc b/DevGuideExamples/LocalObjects/ServantLocator/ServantLocator.mpc new file mode 100644 index 00000000000..e1b7628f933 --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/ServantLocator.mpc @@ -0,0 +1,36 @@ +// $Id$ + +project(*idl): taoidldefaults, anytypecode { + IDL_Files { + Messenger.idl + } + custom_only = 1 +} + +project(*Server): taoserver, anytypecode, avoids_minimum_corba, avoids_corba_e_compact, avoids_corba_e_micro { + exename = MessengerServer + after += *idl + Source_Files { + Messenger_i.cpp + MessengerServer.cpp + MessengerLocator_i.cpp + } + Source_Files { + MessengerC.cpp + MessengerS.cpp + } + IDL_Files { + } +} + +project(*Client): taoexe, anytypecode, avoids_minimum_corba, avoids_corba_e_compact, avoids_corba_e_micro { + after += *idl + Source_Files { + MessengerClient.cpp + } + Source_Files { + MessengerC.cpp + } + IDL_Files { + } +} diff --git a/DevGuideExamples/LocalObjects/ServantLocator/run_test.pl b/DevGuideExamples/LocalObjects/ServantLocator/run_test.pl new file mode 100755 index 00000000000..4936c559c31 --- /dev/null +++ b/DevGuideExamples/LocalObjects/ServantLocator/run_test.pl @@ -0,0 +1,75 @@ +eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' + & eval 'exec perl -S $0 $argv:q' + if 0; + +# $Id$ +# -*- perl -*- + +use lib "$ENV{ACE_ROOT}/bin"; +use PerlACE::TestTarget; + +$status = 0; +$debug_level = '0'; + +foreach $i (@ARGV) { + if ($i eq '-debug') { + $debug_level = '10'; + } +} + +my $server = PerlACE::TestTarget::create_target (1) || die "Create target 1 failed\n"; +my $client = PerlACE::TestTarget::create_target (2) || die "Create target 2 failed\n"; + +my $iorbase = "Messenger.ior"; +my $server_iorfile = $server->LocalFile ($iorbase); +my $client_iorfile = $client->LocalFile ($iorbase); +$server->DeleteFile($iorbase); +$client->DeleteFile($iorbase); + +# start MessengerServer +$SV = $server->CreateProcess ("MessengerServer", "-ORBdebuglevel $debug_level -o $server_iorfile"); +$CL = $client->CreateProcess ("MessengerClient", "-k file://$client_iorfile"); +$server_status = $SV->Spawn (); + +if ($server_status != 0) { + print STDERR "ERROR: server returned $server_status\n"; + exit 1; +} + +if ($server->WaitForFileTimed ($iorbase, + $server->ProcessStartWaitInterval()) == -1) { + print STDERR "ERROR: cannot find file <$server_iorfile>\n"; + $SV->Kill (); $SV->TimedWait (1); + exit 1; +} + +if ($server->GetFile ($iorbase) == -1) { + print STDERR "ERROR: cannot retrieve file <$server_iorfile>\n"; + $SV->Kill (); $SV->TimedWait (1); + exit 1; +} + +if ($client->PutFile ($iorbase) == -1) { + print STDERR "ERROR: cannot set file <$client_iorfile>\n"; + $SV->Kill (); $SV->TimedWait (1); + exit 1; +} + +$client_status = $CL->SpawnWaitKill ($client->ProcessStartWaitInterval()); + +if ($client_status != 0) { + print STDERR "ERROR: client returned $client_status\n"; + $status = 1; +} + +$server_status = $SV->Kill (); + +if ($server_status != 0) { + print STDERR "ERROR: server returned $server_status\n"; + $status = 1; +} + +$server->DeleteFile($iorbase); +$client->DeleteFile($iorbase); + +exit $status; |
