Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / File_IO / server.cpp
blobce2a1d2b538cff0f4f3959df56e3a185a4d8dcc0
1 // -*- C++ -*-
2 #include "File_i.h"
3 #include "tao/debug.h"
4 #include "ace/SString.h"
5 #include "ace/Get_Opt.h"
6 #include "ace/Task.h"
7 #include "ace/OS_NS_unistd.h"
8 #include "ace/OS_NS_fcntl.h"
10 static const ACE_TCHAR *ior_output_file = ACE_TEXT("ior");
11 static const int nthreads = 2;
14 /**
15 * Run a server thread
17 * Use the ACE_Task_Base class to run server threads
19 class Worker : public ACE_Task_Base
21 public:
22 Worker (CORBA::ORB_ptr orb);
23 // ctor
25 virtual int svc (void);
26 // The thread entry point.
28 private:
29 CORBA::ORB_var orb_;
30 // The orb
34 static int
35 parse_args (int argc, ACE_TCHAR **argv)
37 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:d"));
38 int c;
40 while ((c = get_opts ()) != -1)
41 switch (c)
43 case 'o':
44 ior_output_file = get_opts.opt_arg ();
45 break;
46 case 'd':
47 TAO_debug_level++;
48 break;
49 case '?':
50 default:
51 ACE_ERROR_RETURN ((LM_ERROR,
52 "usage: %s "
53 "[-oior_output_file]"
54 "[-d]"
55 "\n",
56 argv [0]),
57 -1);
60 // Indicates successful parsing of command line.
61 return 0;
65 int
66 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
68 ACE_HANDLE handle = ACE_OS::open ("big.txt",
69 O_RDWR | O_CREAT,
70 ACE_DEFAULT_FILE_PERMS);
72 ACE_OS::lseek (handle, 1024*1024*10, SEEK_SET);
73 ACE_OS::write (handle, "", 1);
74 ACE_OS::close (handle);
76 try
78 // Initialize the ORB
79 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
81 int result = parse_args (argc, argv);
82 if (result != 0)
83 return result;
85 // Obtain the RootPOA.
86 CORBA::Object_var obj =
87 orb->resolve_initial_references ("RootPOA");
89 // Narrow the object reference to a POA reference
90 PortableServer::POA_var root_poa =
91 PortableServer::POA::_narrow (obj.in ());
93 PortableServer::POAManager_var poa_manager =
94 root_poa->the_POAManager ();
96 CORBA::PolicyList policies (5);
97 policies.length (5);
99 // ID Assignment Policy
100 policies[0] =
101 root_poa->create_id_assignment_policy (PortableServer::USER_ID);
103 // Lifespan Policy
104 policies[1] =
105 root_poa->create_lifespan_policy (PortableServer::PERSISTENT);
107 // Request Processing Policy
108 policies[2] =
109 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
111 // Servant Retention Policy
112 policies[3] =
113 root_poa->create_servant_retention_policy (PortableServer::RETAIN);
115 // Id Uniqueness Policy
116 policies[4] =
117 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
119 ACE_CString name = "firstPOA";
120 PortableServer::POA_var first_poa =
121 root_poa->create_POA (name.c_str (),
122 poa_manager.in (),
123 policies);
125 for (CORBA::ULong i = 0;
126 i < policies.length ();
127 ++i)
129 CORBA::Policy_ptr policy = policies[i];
130 policy->destroy ();
133 // Create a File System Implementation object in first_poa
134 FileImpl::System file_system_impl (first_poa.in ());
136 PortableServer::ObjectId_var file_system_oid =
137 PortableServer::string_to_ObjectId ("FileSystem");
139 first_poa->activate_object_with_id (file_system_oid.in (),
140 &file_system_impl);
142 CORBA::Object_var file_system =
143 first_poa->id_to_reference (file_system_oid.in ());
145 // Get the IOR for the "FileSystem" object
146 CORBA::String_var file_system_ior =
147 orb->object_to_string (file_system.in ());
149 ACE_DEBUG ((LM_DEBUG,"%C\n",
150 file_system_ior.in ()));
152 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
153 if (output_file == 0)
154 ACE_ERROR_RETURN ((LM_ERROR,
155 "Cannot open output file for writing IOR: %s",
156 ior_output_file),
157 -1);
158 ACE_OS::fprintf (output_file, "%s", file_system_ior.in ());
159 ACE_OS::fclose (output_file);
161 // set the state of the poa_manager to active i.e ready to
162 // process requests
163 poa_manager->activate ();
165 Worker worker (orb.in ());
166 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
167 nthreads) != 0)
168 ACE_ERROR_RETURN ((LM_ERROR,
169 "Cannot activate client threads\n"),
172 worker.thr_mgr ()->wait ();
174 first_poa->destroy(1, 1);
176 root_poa->destroy(1, 1);
178 orb->destroy();
180 catch (const CORBA::Exception& ex)
182 ex._tao_print_exception ("EXCEPTION CAUGHT");
183 return -1;
186 return 0;
190 // ****************************************************************
192 Worker::Worker (CORBA::ORB_ptr orb)
193 : orb_ (CORBA::ORB::_duplicate (orb))
198 Worker::svc (void)
202 // Run the ORB for atmost 75 seconds
203 ACE_Time_Value tv (75, 0);
204 this->orb_->run (tv);
206 catch (const CORBA::Exception& ex)
208 ex._tao_print_exception ("Worker::svc");
209 return -1;
212 return 0;