summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/Default_Servant/File_i.cpp
blob: 6414ac87d277e0f1d4f36f70c7627de664befea9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "File_i.h"

FileImpl::System::System (PortableServer::POA_ptr poa)
  : poa_ (PortableServer::POA::_duplicate (poa)),
    fd_servant_ (poa)
{
  CORBA::Environment env;
  poa->set_servant (&this->fd_servant_, env);
  ACE_ASSERT (env.exception () == 0);
}

FileImpl::System::~System (void)
{
}

PortableServer::POA_ptr 
FileImpl::System::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

File::Descriptor_ptr 
FileImpl::System::open (const char *file_name, 
                        CORBA::Long flags,
                        CORBA::Environment &env)
{
  ACE_HANDLE file_descriptor = ACE_OS::open (file_name,
                                             flags);

  if (file_descriptor == ACE_INVALID_HANDLE)
    {
      CORBA::Exception *exception = new File::IOError (errno);
      env.exception (exception);
      return 0;
    }

  char file_descriptor_buffer[BUFSIZ];
  ACE_OS::sprintf (file_descriptor_buffer,
                   "%ld",
                   (CORBA::Long) file_descriptor);

  PortableServer::ObjectId_var oid =
    PortableServer::string_to_ObjectId (file_descriptor_buffer);

  CORBA::Object_var obj =
    this->poa_->create_reference_with_id (oid.in (), 
                                          this->_interface_repository_id (),
                                          env);
  if (env.exception () != 0)
    return File::Descriptor::_nil ();

  File::Descriptor_var fd =
    File::Descriptor::_narrow (obj.in (), env);

  if (env.exception () != 0)
    return File::Descriptor::_nil ();

  return fd._retn ();
}

FileImpl::Descriptor::Descriptor (PortableServer::POA_ptr poa)
  : poa_ (PortableServer::POA::_duplicate (poa))
{
}

FileImpl::Descriptor::~Descriptor (void)
{
}

PortableServer::POA_ptr 
FileImpl::Descriptor::_default_POA (CORBA::Environment &env)
{
  return PortableServer::POA::_duplicate (this->poa_.in ());
}

ACE_HANDLE
FileImpl::Descriptor::fd (CORBA::Environment &env)
{
  File::Descriptor_var me = this->_this (env);

  if (env.exception () != 0)
    return ACE_INVALID_HANDLE;

  PortableServer::ObjectId_var oid =
    this->poa_->reference_to_id (me.in (), env);

  if (env.exception () != 0)
    return ACE_INVALID_HANDLE;

  CORBA::String_var s =
    PortableServer::ObjectId_to_string (oid.in ());

  return (ACE_HANDLE) ::atol (s.in ());
}

CORBA::Long 
FileImpl::Descriptor::write (const File::Descriptor::DataBuffer &buffer, 
                             CORBA::Environment &env)
{
  ACE_HANDLE file_descriptor = this->fd (env);

  if (env.exception () != 0)
    return 0;
  
  const CORBA::Octet *data = &buffer[0];

  ssize_t len = ACE_OS::write (file_descriptor,
                               data,
                               buffer.length ());
  if (len > 0)
    return len;
  else
    {
      CORBA::Exception *exception = new File::IOError (errno);
      env.exception (exception);
      return 0;
    }
}

File::Descriptor::DataBuffer *
FileImpl::Descriptor::read (CORBA::Long num_bytes, 
                            CORBA::Environment &env)
{
  ACE_HANDLE file_descriptor = this->fd (env);

  if (env.exception () != 0)
    return 0;
  
  CORBA::Octet *buffer = File::Descriptor::DataBuffer::allocbuf (num_bytes);
  int length = ACE_OS::read (file_descriptor, buffer, num_bytes);

  if (length > 0)
    return new File::Descriptor::DataBuffer (length,
                                             length,
                                             buffer,
                                             CORBA::B_TRUE);
  else
    {
      File::Descriptor::DataBuffer::freebuf (buffer);
      CORBA::Exception *exception = new File::IOError (errno);
      env.exception (exception);
      return 0;
    }
}
  
CORBA::ULong
FileImpl::Descriptor::lseek (CORBA::ULong offset, 
                             CORBA::Long whence,  
                             CORBA::Environment &env)
{
  ACE_HANDLE file_descriptor = this->fd (env);

  if (env.exception () != 0)
    return 0;
  
  CORBA::ULong result = ACE_OS::lseek (file_descriptor,
                                       offset,
                                       whence);
  if (result == -1)
    {
      CORBA::Exception *exception = new File::IOError (errno);
      env.exception (exception);
      return 0;
    }
  else
    return result;
}

void
FileImpl::Descriptor::destroy (CORBA::Environment &env)
{
  ACE_HANDLE file_descriptor = this->fd (env);

  if (env.exception () != 0)
    return;

  int result = ACE_OS::close (file_descriptor);

  if (result != 0)
    {
      CORBA::Exception *exception = new File::IOError (errno);
      env.exception (exception);
      return;
    }
}