2 //=============================================================================
6 * Implementation of the File IDL module and the interfaces
7 * Descriptor and System in it.
9 * @author Irfan Pyarali
11 //=============================================================================
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
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 ());
41 FileImpl::System::open (const char *file_name
,
45 ACE_HANDLE file_descriptor
= ACE_OS::open (file_name
,
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
,
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 ());
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
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.
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 ());
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
,
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
);
172 return new File::Descriptor::DataBuffer (length
,
177 File::Descriptor::DataBuffer::freebuf (buffer
);
178 throw File::IOError ();
182 FileImpl::Descriptor::lseek (CORBA::ULong offset
,
185 ACE_HANDLE file_descriptor
= this->fd ();
187 CORBA::Long result
= (CORBA::Long
) ACE_OS::lseek (file_descriptor
,
191 throw File::IOError ();
193 return (CORBA::ULong
) result
;
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
);