Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / File_IO / File_i.cpp
blobf1f3f2e03ac823ff7d241ce599fa62751366c7b6
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"
20 // IDL File::System constructor
21 FileImpl::System::System (PortableServer::POA_ptr poa)
22 : poa_ (PortableServer::POA::_duplicate (poa)),
23 // Create the Default Descriptor Servant
24 fd_servant_ (poa)
26 // set the default servant of the POA
27 poa->set_servant (&this->fd_servant_);
30 FileImpl::System::~System ()
34 PortableServer::POA_ptr
35 FileImpl::System::_default_POA ()
37 return PortableServer::POA::_duplicate (this->poa_.in ());
40 File::Descriptor_ptr
41 FileImpl::System::open (const char *file_name,
42 CORBA::Long flags)
44 // Do an ACE_OS::open
45 ACE_HANDLE file_descriptor = ACE_OS::open (file_name,
46 flags,
47 ACE_DEFAULT_FILE_PERMS);
49 if (file_descriptor == ACE_INVALID_HANDLE)
51 throw File::IOError ();
54 char file_descriptor_buffer[BUFSIZ];
56 // convert ACE_HANDLE to a string
57 ACE_OS::sprintf (file_descriptor_buffer,
58 "%ld",
59 ACE_Utils::truncate_cast<long int> ((intptr_t)file_descriptor));
61 //Create an objectID from the ACE_HANDLE string
62 PortableServer::ObjectId_var oid =
63 PortableServer::string_to_ObjectId (file_descriptor_buffer);
65 // create an object reference with the specified ObjectID got
66 // from ACE_HANDLE string
67 CORBA::Object_var obj =
68 this->poa_->create_reference_with_id (oid.in (),
69 "IDL:File/Descriptor:1.0");
71 // Narrow the object reference to a File Descriptor
72 File::Descriptor_var fd =
73 File::Descriptor::_narrow (obj.in ());
76 return fd._retn ();
79 // IDL File::Descriptor constructor
80 FileImpl::Descriptor::Descriptor (PortableServer::POA_ptr poa)
81 : poa_ (PortableServer::POA::_duplicate (poa))
85 FileImpl::Descriptor::~Descriptor ()
89 PortableServer::POA_ptr
90 FileImpl::Descriptor::_default_POA ()
92 return PortableServer::POA::_duplicate (this->poa_.in ());
95 //Extracts the ACE_HANDLE from the passed object reference
96 ACE_HANDLE
97 FileImpl::Descriptor::fd ()
100 // One way of getting our id.
103 // Get a reference to myself
104 File::Descriptor_var me = this->_this ();
106 // Get the ObjectId from the reference
107 PortableServer::ObjectId_var oid1 =
108 this->poa_->reference_to_id (me.in ());
111 // Another way of getting our id.
114 PortableServer::ObjectId_var oid2 =
115 this->poa_->servant_to_id (this);
118 // Yet another way of getting our id.
121 int argc = 0;
122 ACE_TCHAR **argv = 0;
123 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
125 // Get the POA Current object reference
126 CORBA::Object_var obj =
127 orb->resolve_initial_references ("POACurrent");
129 // Narrow the object reference to a POA Current reference
130 PortableServer::Current_var poa_current =
131 PortableServer::Current::_narrow (obj.in ());
133 PortableServer::ObjectId_var oid3 =
134 poa_current->get_object_id ();
136 ACE_ASSERT (oid1.in () == oid2.in ());
137 ACE_ASSERT (oid2.in () == oid3.in ());
139 // Convert the ObjectId to a string
140 CORBA::String_var s =
141 PortableServer::ObjectId_to_string (oid1.in ());
143 // Get the ACE_HANDLE from the string
144 return (ACE_HANDLE)(intptr_t)ACE_OS::atol (s.in ());
147 CORBA::Long
148 FileImpl::Descriptor::write (const File::Descriptor::DataBuffer &buffer)
150 ACE_HANDLE file_descriptor = this->fd ();
152 const CORBA::Octet *data = &buffer[0];
154 ssize_t len = ACE_OS::write (file_descriptor,
155 data,
156 buffer.length ());
157 if (len > 0)
158 return len;
160 throw File::IOError ();
163 File::Descriptor::DataBuffer *
164 FileImpl::Descriptor::read (CORBA::Long num_bytes)
166 ACE_HANDLE file_descriptor = this->fd ();
168 CORBA::Octet *buffer = File::Descriptor::DataBuffer::allocbuf (num_bytes);
169 ssize_t length = ACE_OS::read (file_descriptor, buffer, num_bytes);
171 if (length > 0)
172 return new File::Descriptor::DataBuffer (length,
173 length,
174 buffer,
177 File::Descriptor::DataBuffer::freebuf (buffer);
178 throw File::IOError ();
181 CORBA::ULong
182 FileImpl::Descriptor::lseek (CORBA::ULong offset,
183 CORBA::Long whence)
185 ACE_HANDLE file_descriptor = this->fd ();
187 CORBA::Long result = (CORBA::Long) ACE_OS::lseek (file_descriptor,
188 offset,
189 whence);
190 if (result == -1)
191 throw File::IOError ();
193 return (CORBA::ULong) result;
196 void
197 FileImpl::Descriptor::destroy ()
199 // Get the ACE_HANDLE for this object reference
200 ACE_HANDLE file_descriptor = this->fd ();
202 // Close the file corresponding to this object reference.
203 ACE_OS::close (file_descriptor);