summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/new_RepositoryManager/RMadmin.cpp
blob: eb7b50a99524eea2fd5249a189bf9d83da97fa09 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// $Id$

/***
 * @file RMClient.cpp
 *
 * @author Stoyan Paunov <spaunov@isis.vanderbilt.edu>
 **/

#include "new_RepositoryManagerC.h"
#include "Options.h"

#include "ace/OS_NS_fcntl.h"	  //for open
#include "ace/OS_NS_unistd.h"	  //for close
#include "ace/OS_NS_sys_stat.h"   //for filesize and fstat and mkdir

#include "Config_Handlers/DnC_Dump.h"

#include <iostream>
using namespace std;

//IOR file of the Sender
const char * ior = "file://RepositoryManagerDeamon.ior";


///=============================COUPLE OF HELPER METHORS==================================
CORBA::Octet* read_from_disk (
	const char* full_path,
	size_t &length
	);

int write_to_disk (
	const char* full_path, 
	const CORBA::Octet* buffer, 
	size_t length
	);
///========================================================================================


///main function that provides a sample interface for RM clients

int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  ACE_TRY_NEW_ENV
    {
      // Initialize orb
      CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, 
											""ACE_ENV_ARG_PARAMETER);

      ACE_TRY_CHECK;


      CORBA::Object_var obj =
        orb->string_to_object (ior
                               ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

	  CIAO::new_RepositoryManagerDaemon_var rm =
		  CIAO::new_RepositoryManagerDaemon::_narrow (obj.in ()
			                          ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (rm.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR, 
                             "Unable to acquire new_RepositoryManagerDaemon's objref\n"),
                            -1);
        }


		Options* options = Options::instance ();
		options->parse_args (argc, argv);
		
		if (options->shutdown_)
		{
			rm->shutdown ();
		}
		else if (options->install_)
		{
			size_t length = 0;	 
			CORBA::Octet* buffer = read_from_disk (options->local_path_.c_str (), length);

			Deployment::Package* the_package = new Deployment::Package (
			length,				//max of the sequence
			length,				//length of the sequence
			buffer,				//data to be stored within the sequence
			true				//take ownership of the data
			);		

			try
			{
				rm->installPackage (options->package_.c_str (), *the_package, false);
			}
			catch (CORBA::Exception &ex)
			{
				cout << "\nPackage is already in the repository!\n";
			}

			cout << "\nReassuring that the package in the repository ..." << endl;
			try
			{
				Deployment::Package_var package_back = rm->findPackageByName (options->package_.c_str ());
				cout << "The package was found!" << endl;
				cout << "Its size is " << package_back->length () << endl;
			}
			catch (CORBA::Exception &ex)
			{
				cout << "\nNo such package!" << endl;
			}
		}
		else if (options->delete_)
		{
			try
			{
				rm->deletePackage (options->package_.c_str ());
				cout << options->package_.c_str () << " deleted" << endl;
			}
			catch (CORBA::Exception &ex)
			{
				cout << "\nNo such package!" << endl;
			}

		}
		else if (options->plan_)
		{

			Deployment::DeploymentPlan_var plan = rm->retrievePlan (options->package_.c_str ());

			Deployment::DnC_Dump::dump (plan);
		}
		else if (options->artifact_ != "")
		{
			try
			{
				Deployment::Implementation_var impl = rm->findImplementationByName (options->artifact_.c_str (), 
																					options->package_.c_str());
				write_to_disk (impl->name, impl->the_implementation.get_buffer (),impl->the_implementation.length ()); 
			}
			catch (CORBA::Exception &ex)
			{
				cout << "\nNo such implementation artifact in that package!" << endl;
			}

		}


      orb->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
			                     "Unknown exception \n");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}



CORBA::Octet* read_from_disk (
	const char* full_path,
	size_t &length
	)
{
	//open the file

	ACE_HANDLE handle = ACE_OS::open (full_path, O_RDONLY);
    if (handle == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("[RM::read_from_disk] file open error")),
                           0);

	ACE_stat file_info;

	ACE_OS::fstat (handle, &file_info);

	CORBA::Octet* buffer = new CORBA::Octet[file_info.st_size];

	if (buffer == 0)
		return 0;

	//read the contents of the file into the buffer
	if (ACE_OS::read_n (handle, buffer, file_info.st_size) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
						   ACE_TEXT ("[RM::write_to_disk] file write error")),
                           0);

	// Close the file handle
    ACE_OS::close (handle);

	length = file_info.st_size;
	return buffer;
}


int write_to_disk (
	const char* full_path, 
	const CORBA::Octet* buffer, 
	size_t length
	)
{
	
	// Open a file handle to the local filesystem
    ACE_HANDLE handle = ACE_OS::open (full_path, _O_CREAT | _O_TRUNC | O_WRONLY);
    if (handle == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("[RM::write_to_disk] file creation error")),
                           -1);

	//write the data to the file
	if (ACE_OS::write (handle, buffer, length) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
						   ACE_TEXT ("[RM::write_to_disk] file write error")),
                           -1);

	// Close the file handle
    ACE_OS::close (handle);

	return 1;
}