4 #include "ace/FILE_Addr.h"
5 #include "ace/FILE_Connector.h"
6 #include "ace/FILE_IO.h"
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_string.h"
10 const size_t MAX_UINT32_STR_LEN
= 11; // Largest UINT32 is 8589934591 + NUL is 11 characters
13 Nestea_i::Nestea_i (CORBA::ORB_ptr orb
, const ACE_TCHAR
*filename
)
16 orb_
= CORBA::ORB::_duplicate(orb
);
18 this->data_filename_
= ACE::strnew (filename
);
20 // @@ This should probably be called from somewhere else
25 Nestea_i::~Nestea_i ()
27 delete [] this->data_filename_
;
31 // Add <cans> number of cans to the bookshelf.
34 Nestea_i::drink (CORBA::Long cans
)
37 ACE_DEBUG ((LM_DEBUG
, "Nestea_i::drink %d cans\n", cans
));
45 // Removes <cans> number of cans from the bookshelf.
48 Nestea_i::crush (CORBA::Long cans
)
51 ACE_DEBUG ((LM_DEBUG
, "Nestea_i::crush %d cans\n", cans
));
53 if (static_cast<ACE_UINT32
> (cans
) > this->cans_
)
62 // Returns the number of cans in the bookshelf.
65 Nestea_i::bookshelf_size ()
68 ACE_DEBUG ((LM_DEBUG
, "Nestea_i::bookshelf_size\n"));
73 // Returns comments about your collection.
76 Nestea_i::get_praise ()
79 ACE_DEBUG ((LM_DEBUG
, "Nestea_i::get_praise\n"));
81 if (this->cans_
> 500)
82 return CORBA::string_dup ("Man, that is one excellent Nestea Collection!");
83 else if (this->cans_
> 250)
84 return CORBA::string_dup ("We are getting into the big leagues now!");
85 else if (this->cans_
> 100)
86 return CORBA::string_dup ("Things are looking up!");
87 else if (this->cans_
> 0)
88 return CORBA::string_dup ("Well, it is a start. Drink more Nestea!");
90 return CORBA::string_dup ("No cans, no praise.");
97 ACE_DEBUG ((LM_DEBUG
, "Nestea_i::shutdown\n"));
102 // Saves bookshelf data to a file.
105 Nestea_i::save_data ()
108 ACE_FILE_Connector connector
;
110 if (connector
.connect (file
,
111 ACE_FILE_Addr (this->data_filename_
),
113 ACE_Addr::sap_any
) == -1)
114 ACE_ERROR_RETURN ((LM_ERROR
,
117 this->data_filename_
),
120 char str
[MAX_UINT32_STR_LEN
];
122 ACE_OS::sprintf (str
, "%d", this->cans_
);
124 return file
.send_n (str
, ACE_OS::strlen (str
) + 1);
128 // Loads bookshelf data from a file.
131 Nestea_i::load_data ()
134 ACE_FILE_Connector connector
;
136 if (connector
.connect (file
,
137 ACE_FILE_Addr (this->data_filename_
),
139 ACE_Addr::sap_any
) == -1)
140 ACE_ERROR_RETURN ((LM_ERROR
,
143 this->data_filename_
),
146 char str
[MAX_UINT32_STR_LEN
];
148 int len
= file
.recv (str
, MAX_UINT32_STR_LEN
);
152 this->cans_
= ACE_OS::atoi (str
);