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"
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
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 ());
43 FileImpl::System::open (const char *file_name
,
47 ACE_HANDLE file_descriptor
= ACE_OS::open (file_name
,
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
,
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 ());
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
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.
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 ());
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
,
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
);
180 return new File::Descriptor::DataBuffer (length
,
185 File::Descriptor::DataBuffer::freebuf (buffer
);
186 throw File::IOError ();
190 FileImpl::Descriptor::lseek (CORBA::ULong offset
,
193 ACE_HANDLE file_descriptor
= this->fd ();
195 CORBA::Long result
= (CORBA::Long
) ACE_OS::lseek (file_descriptor
,
199 throw File::IOError ();
201 return (CORBA::ULong
) result
;
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
);