summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/ImplRepo/nestea_i.cpp
blob: 0df205d1dc76d76e93d04434805ef124e3fcacca (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
// $Id$

#include "nestea_i.h"
#include "tao/corba.h"
#include "ace/FILE_Connector.h"

const size_t MAX_UINT32_STR_LEN = 11;  // Largest UINT32 is 8589934591 + NUL is 11 characters

ACE_RCSID(ImplRepo, nestea_i, "$Id$")

// Constructor

Nestea_i::Nestea_i (const char *filename, int shutdown)
: shutdown_ (shutdown),
  cans_ (0)
{
  this->data_filename_ = ACE::strnew (filename);
  
  // @@ This should probably be called from somewhere else
  this->load_data ();
}


// Destructor

Nestea_i::~Nestea_i (void)
{
  delete this->data_filename_;
}


// Add <cans> number of cans to the bookshelf.

void 
Nestea_i::drink (CORBA::Long cans,
                 CORBA::Environment &ACE_TRY_ENV)
{
  ACE_UNUSED_ARG (ACE_TRY_ENV);

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Nestea_i::drink %d cans\n", cans));

  this->cans_ += cans;

  if (this->shutdown_ != 0)
    TAO_ORB_Core_instance ()->orb ()->shutdown ();

  this->save_data ();
}


// Removes <cans> number of cans from the bookshelf.

void 
Nestea_i::crush (CORBA::Long cans,
                 CORBA::Environment &ACE_TRY_ENV)
{
  ACE_UNUSED_ARG (ACE_TRY_ENV);

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Nestea_i::crush %d cans\n", cans));

  if (cans > this->cans_)
    this->cans_ = 0;
  else
    this->cans_ -= cans;
  
  if (this->shutdown_ != 0)
    TAO_ORB_Core_instance ()->orb ()->shutdown ();

  this->save_data ();
}


// Returns the number of cans in the bookshelf.

CORBA::Long 
Nestea_i::bookshelf_size (CORBA::Environment &ACE_TRY_ENV)
{
  ACE_UNUSED_ARG (ACE_TRY_ENV);

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Nestea_i::bookshelf_size\n"));

  if (this->shutdown_ != 0)
    TAO_ORB_Core_instance ()->orb ()->shutdown ();

  return this->cans_;
}

// Returns comments about your collection.

char *
Nestea_i::get_praise (CORBA::Environment &ACE_TRY_ENV)
{
  ACE_UNUSED_ARG (ACE_TRY_ENV);

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG, "Nestea_i::get_praise\n"));

  if (this->shutdown_ != 0)
    TAO_ORB_Core_instance ()->orb ()->shutdown ();

  if (this->cans_ > 500)
    return CORBA::string_dup ("Man, that is one excellent Nestea Collection!");
  else if (this->cans_ > 250)
    return CORBA::string_dup ("We are getting into the big leagues now!");
  else if (this->cans_ > 100)
    return CORBA::string_dup ("Things are looking up!");
  else if (this->cans_ > 0)
    return CORBA::string_dup ("Well, it is a start. Drink more Nestea!");
  else
    return CORBA::string_dup ("No cans, no praise.");
}


// Saves bookshelf data to a file.

int 
Nestea_i::save_data (void)
{
  ACE_FILE_IO file;
  ACE_FILE_Addr filename (this->data_filename_);

  char str[MAX_UINT32_STR_LEN];

  ACE_OS::sprintf (str, "%d", this->cans_);

  return file.send_n (str, ACE_OS::strlen (str) + 1);
}


// Loads bookshelf data from a file.

int 
Nestea_i::load_data (void)
{
  ACE_FILE_IO file;
  ACE_FILE_Addr filename ("nestea.dat");

  char str[12];

  int len = file.recv (str, ACE_OS::strlen (str) + 1);
  str[len] = 0;

  if (len > 0)
    this->cans_ = ACE_OS::atoi (str);
  else
    this->cans_ = 0;
  return 0;
}