Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / POA / Default_Servant2 / File_i.cpp
blobdfdc1f3496be6f1a2b56dca59834ce7eabb91f56
2 //=============================================================================
3 /**
4 * @file File_i.cpp
6 * Implementation of the File IDL module and the interfaces
7 * Descriptor and System in it.
9 * @author Irfan Pyarali
11 //=============================================================================
14 #include "File_i.h"
15 #include "ace/OS_NS_stdio.h"
16 #include "ace/OS_NS_fcntl.h"
17 #include "ace/OS_NS_unistd.h"
18 #include "ace/Truncate.h"
19 #include "tao/PortableServer/PortableServer.h"
21 // IDL File::System constructor
22 FileImpl::System::System (CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
23 : orb_ (CORBA::ORB::_duplicate (orb)),
24 poa_ (PortableServer::POA::_duplicate (poa)),
25 // Create the Default Descriptor Servant
26 fd_servant_ (poa)
28 // set the default servant of the POA
29 poa->set_servant (&this->fd_servant_);
32 FileImpl::System::~System ()
36 PortableServer::POA_ptr
37 FileImpl::System::_default_POA ()
39 return PortableServer::POA::_duplicate (this->poa_.in ());
42 File::Descriptor_ptr
43 FileImpl::System::open (const char *file_name,
44 CORBA::Long flags)
46 // Do an ACE_OS::open
47 ACE_HANDLE file_descriptor = ACE_OS::open (file_name,
48 flags,
49 ACE_DEFAULT_FILE_PERMS);
51 if (file_descriptor == ACE_INVALID_HANDLE)
53 throw File::IOError ();
56 char file_descriptor_buffer[BUFSIZ];
58 // convert ACE_HANDLE to a string
59 ACE_OS::sprintf (file_descriptor_buffer,
60 "%ld",
61 ACE_Utils::truncate_cast<long int> ((intptr_t)file_descriptor));
63 //Create an objectID from the ACE_HANDLE string
64 PortableServer::ObjectId_var oid =
65 PortableServer::string_to_ObjectId (file_descriptor_buffer);
67 // create an object reference with the specified ObjectID got
68 // from ACE_HANDLE string
69 CORBA::Object_var obj =
70 this->poa_->create_reference_with_id (oid.in (),
71 "IDL:File/Descriptor:1.0");
73 // Narrow the object reference to a File Descriptor
74 File::Descriptor_var fd =
75 File::Descriptor::_narrow (obj.in ());
78 return fd._retn ();
81 void
82 FileImpl::System::shutdown ()
84 this->orb_->shutdown (false);
87 // IDL File::Descriptor constructor
88 FileImpl::Descriptor::Descriptor (PortableServer::POA_ptr poa)
89 : poa_ (PortableServer::POA::_duplicate (poa))
93 FileImpl::Descriptor::~Descriptor ()
97 PortableServer::POA_ptr
98 FileImpl::Descriptor::_default_POA ()
100 return PortableServer::POA::_duplicate (this->poa_.in ());
103 //Extracts the ACE_HANDLE from the passed object reference
104 ACE_HANDLE
105 FileImpl::Descriptor::fd ()
108 // One way of getting our id.
111 // Get a reference to myself
112 File::Descriptor_var me = this->_this ();
114 // Get the ObjectId from the reference
115 PortableServer::ObjectId_var oid1 =
116 this->poa_->reference_to_id (me.in ());
119 // Another way of getting our id.
122 PortableServer::ObjectId_var oid2 =
123 this->poa_->servant_to_id (this);
126 // Yet another way of getting our id.
129 int argc = 0;
130 ACE_TCHAR **argv = 0;
131 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
133 // Get the POA Current object reference
134 CORBA::Object_var obj =
135 orb->resolve_initial_references ("POACurrent");
137 // Narrow the object reference to a POA Current reference
138 PortableServer::Current_var poa_current =
139 PortableServer::Current::_narrow (obj.in ());
141 PortableServer::ObjectId_var oid3 =
142 poa_current->get_object_id ();
144 ACE_ASSERT (oid1.in () == oid2.in ());
145 ACE_ASSERT (oid2.in () == oid3.in ());
147 // Convert the ObjectId to a string
148 CORBA::String_var s =
149 PortableServer::ObjectId_to_string (oid1.in ());
151 // Get the ACE_HANDLE from the string
152 return (ACE_HANDLE)(intptr_t)ACE_OS::atol (s.in ());
155 CORBA::Long
156 FileImpl::Descriptor::write (const File::Descriptor::DataBuffer &buffer)
158 ACE_HANDLE file_descriptor = this->fd ();
160 const CORBA::Octet *data = &buffer[0];
162 ssize_t len = ACE_OS::write (file_descriptor,
163 data,
164 buffer.length ());
165 if (len > 0)
166 return len;
168 throw File::IOError ();
171 File::Descriptor::DataBuffer *
172 FileImpl::Descriptor::read (CORBA::Long num_bytes)
174 ACE_HANDLE file_descriptor = this->fd ();
176 CORBA::Octet *buffer = File::Descriptor::DataBuffer::allocbuf (num_bytes);
177 int length = ACE_OS::read (file_descriptor, buffer, num_bytes);
179 if (length > 0)
180 return new File::Descriptor::DataBuffer (length,
181 length,
182 buffer,
185 File::Descriptor::DataBuffer::freebuf (buffer);
186 throw File::IOError ();
189 CORBA::ULong
190 FileImpl::Descriptor::lseek (CORBA::ULong offset,
191 CORBA::Long whence)
193 ACE_HANDLE file_descriptor = this->fd ();
195 CORBA::Long result = (CORBA::Long) ACE_OS::lseek (file_descriptor,
196 offset,
197 whence);
198 if (result == -1)
199 throw File::IOError ();
201 return (CORBA::ULong) result;
204 void
205 FileImpl::Descriptor::destroy ()
207 // Get the ACE_HANDLE for this object reference
208 ACE_HANDLE file_descriptor = this->fd ();
210 // Close the file corresponding to this object reference.
211 ACE_OS::close (file_descriptor);