summaryrefslogtreecommitdiff
path: root/CIAO/DAnCE/tools/Convert_Plan/Convert_Plan_Impl.cpp
blob: 6142a707c31ca5373823ea768f50997b84649573 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// $Id$

#include "Convert_Plan_Impl.h"
#include "ace/OS_NS_stdio.h"
#include "tao/CDR.h"
#include "Deployment/Deployment_PlanErrorC.h"
#include "Config_Handlers/XML_File_Intf.h"
#include "Config_Handlers/Common.h"
#include "Logger/Log_Macros.h"

namespace DAnCE
{
  Deployment::DeploymentPlan * 
  Convert_Plan::read_xml_plan (const ACE_TCHAR *filename)
  {
    if (!filename)
      {
        DANCE_ERROR (2, (LM_WARNING, DLINFO ACE_TEXT ("Convert_Plan::read_xml_plan - ")
                         ACE_TEXT ("Passed a null pointer, returning.\n")));
      }

    CIAO::Config_Handlers::XML_File_Intf xml_intf (filename);
    xml_intf.add_search_path (ACE_TEXT("ACE_ROOT"), ACE_TEXT("/docs/schema/"));
    xml_intf.add_search_path (ACE_TEXT("TAO_ROOT"), ACE_TEXT("/docs/schema/"));
    xml_intf.add_search_path (ACE_TEXT("CIAO_ROOT"), ACE_TEXT("/docs/schema/"));
    xml_intf.add_search_path (ACE_TEXT("DANCE_ROOT"), ACE_TEXT("/docs/schema/"));
    
    try
      {
        return xml_intf.release_plan ();  
      }
    catch (const CIAO::Config_Handlers::Config_Error &ex)
      {
        throw ::Deployment::PlanError (ACE_TEXT_ALWAYS_CHAR (ex.name_.c_str ()),
                                       ACE_TEXT_ALWAYS_CHAR (ex.error_.c_str ()));
      }
  }

  Deployment::DeploymentPlan * 
  Convert_Plan::read_cdr_plan (const ACE_TCHAR *filename)
  {
    if (!filename)
      {
        DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("Convert_Plan::read_cdr_plan - ")
                         ACE_TEXT ("Passed a null pointer, returning.\n")));
        return 0;
      }
    
    FILE *file = ACE_OS::fopen (filename, "r");
    
    if (!file)
      {
        DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("Convert_Plan::read_cdr_plan - ")
                         ACE_TEXT ("Error: Unable to open file for reading %s\n"),
                         filename));
        return 0;
      }

    char byte_order;
    size_t n = ACE_OS::fread (&byte_order, sizeof (byte_order), 1, file);
    
    if (n == 0)
      {
        DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("Convert_Plan::read_cdr_plan - ")
                         ACE_TEXT ("Byte order not written to file\n")));
        ACE_OS::fclose (file);
        return 0;
      }

    ACE_UINT32 bufsize (0);
    n = ACE_OS::fread (&bufsize, sizeof (bufsize), 1, file);
    
    if (n == 0)
      {
        DANCE_ERROR (1, (LM_ERROR, DLINFO ACE_TEXT ("Convert_Plan::read_cdr_plan - ")
                         ACE_TEXT ("Plan size not written to file\n")));
        ACE_OS::fclose (file);
        return 0;
      }
    
    if (byte_order != ACE_CDR_BYTE_ORDER)
      {
        DANCE_DEBUG (6, (LM_DEBUG, DLINFO ACE_TEXT ("Convert_Plan::read_cdr_plan - ")
                         ACE_TEXT ("Plan appears encoded in a different endian.\n")));
        ACE_CDR::swap_4 (reinterpret_cast<char *> (&bufsize),
                         reinterpret_cast<char *> (&bufsize));
      }
    
    
    char *buffer = 0;
    ACE_NEW_RETURN (buffer,
                    char[bufsize],
                    0);
  
#if defined (ACE_INITIALIZE_MEMORY_BEFORE_USE)
    ACE_OS::memset(buffer, 0, sizeof (buffer));
#endif /* ACE_INITIALIZE_MEMORY_BEFORE_USE */
    
    // Make sure <buffer> is released automagically.
    ACE_Auto_Basic_Array_Ptr<char> b (buffer);
  
    n = ACE_OS::fread (buffer, 1, bufsize, file);
    ACE_OS::fclose (file);

    if (n != bufsize)
      {
        DANCE_ERROR (1, (LM_ERROR, DLINFO "Convert_Plan_Impl::read_cdr_plan - read %u bytes "
                         "instead of %u bytes\n", n, bufsize));
        return 0;
      }
  
    ACE_Message_Block mb (ACE_CDR::MAX_ALIGNMENT + bufsize);
    ACE_CDR::mb_align (&mb);
    mb.copy (buffer, bufsize);
  
    TAO_InputCDR input_cdr (&mb);
    input_cdr.reset_byte_order ((int) byte_order);
  
    DANCE_DEBUG (9, (LM_TRACE, DLINFO "Convert_Plan_Impl::read_cdr_plan - "
                     "Reading file %s in %s endian format with size %u\n",
                     filename, 
                     ACE_CDR_BYTE_ORDER ? ACE_TEXT("little") : ACE_TEXT("big"),
                     bufsize));
  
  
    Deployment::DeploymentPlan *retval = 0;
    
    ACE_NEW_RETURN (retval, Deployment::DeploymentPlan (), 0);
  
    if (!(input_cdr >> *retval))
      {
        DANCE_ERROR (1, (LM_ERROR, ACE_TEXT ("Convert_Plan_Impl::read_cdr_plan - ")
                         ACE_TEXT ("Failed to demarshal plan\n")));
        delete retval;
        return 0;
      }
  
  
    return retval;
  }

  bool
  Convert_Plan::write_cdr_plan (const ACE_TCHAR *output_filename, Deployment::DeploymentPlan &plan)
  {
    if (!output_filename)
      {
        DANCE_ERROR (1, (LM_ERROR, DLINFO "Convert_Plan_Impl::write_cdr_plan - passed nil file name\n"));
        return false;
      }    
  
    TAO_OutputCDR output_cdr;
  
    if (output_cdr << plan)
      {
        FILE *file = ACE_OS::fopen (output_filename, "w");
        
        if (file == 0)
          {
            DANCE_ERROR (1, (LM_ERROR, DLINFO "Convert_Plan_Impl::write_cdr_plan - failed to open file %s\n",
                             output_filename));
            return false;
          }
        
        // First write the byte order
        char byte_order = ACE_CDR_BYTE_ORDER;
        size_t n = ACE_OS::fwrite (&byte_order, 1, sizeof(byte_order), file);
        
        // Next write the length
        ACE_UINT32 cdr_length (output_cdr.total_length ());
        n += ACE_OS::fwrite (&cdr_length, 1, sizeof (cdr_length), file);

        DANCE_DEBUG (9, (LM_TRACE, DLINFO  "Convert_Plan_Impl::write_cdr_plan - "
                         ACE_TEXT ("Writing plan to file %s in %s endian format and length %u\n"),
                         output_filename,
                         ACE_CDR_BYTE_ORDER ? ACE_TEXT("little") : ACE_TEXT("big"),
                         cdr_length));
        
        // Now write the IDL structure.
        for (const ACE_Message_Block *output_mb = output_cdr.begin ();
             output_mb != 0;
             output_mb = output_mb->cont ())
          {
            n += ACE_OS::fwrite (output_mb->rd_ptr (),
                                 1,
                                 output_mb->length (),
                                 file);
          }
          
        ACE_OS::fclose (file);
        
        size_t total_size = sizeof (byte_order) + sizeof (cdr_length) + cdr_length;
        if (n != total_size)
          {
            DANCE_ERROR (1, (LM_ERROR, ACE_TEXT ("Convert_Plan::write_cdr_plan - ")
                             ACE_TEXT ("Error: Unexpected number of bytes written: %u instead of %u\n"),
                             n, total_size));
          }
      }
    else
      {
        DANCE_ERROR (1, (LM_ERROR, ACE_TEXT ("Convert_Plan::write_cdr_plan - ")
                         ACE_TEXT ("Failed to mashal deployment plan\n")));
        return false;
      }
  
    return true;    
  }
}