summaryrefslogtreecommitdiff
path: root/TAO/examples/Simple/bank/Bank_Client_i.cpp
blob: 9dd30909273f6f0c94af3e0848ed994b215a9918 (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
//$Id$

#include "Bank_Client_i.h"
#include "ace/Get_Opt.h"
#include "ace/Read_Buffer.h"

// This is the interface program that accesses the remote object

// Constructor.
Bank_Client_i::Bank_Client_i (void)
{
  //no-op
}

//Destructor.
Bank_Client_i::~Bank_Client_i (void)
{
  //no-op
}


int
Bank_Client_i::run (const char *name,
                    int argc,
                    char *argv[])
{
  // Initialize the client.
  if (client.init (name,argc, argv) == -1)
    return -1;

  ACE_TRY_NEW_ENV
    {
      this->check_accounts ();
      if (client.shutdown () == 1)
        client->shutdown (ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_DEBUG ((LM_DEBUG,
                  "\nException caught in run\n"));
    }
  ACE_ENDTRY;

  return 0;
}

int
Bank_Client_i::check_accounts (void )
{
  ACE_TRY_NEW_ENV
    {
      ACE_DEBUG ((LM_DEBUG,
                  "\nTests for account with same name"));
      this->test_for_same_name (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG,
                  "\nTests for account with different names"));
      this->test_for_different_name (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG,
                  "\nTests for overdrafts"));
      this->test_for_overdraft (ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_DEBUG ((LM_DEBUG,
                  "(\nFrom Bank_Client_i::check_accounts()"));
    }
  ACE_ENDTRY;
  return 0;
}

// This method tests whether an account with a
// a same name can be opened

void
Bank_Client_i::test_for_same_name (CORBA::Environment &ACE_TRY_ENV)
{

  const char *name = "Name";
  CORBA::Float initial_bal = 0.00;

  Bank::Account_var acct_id1 = client->open (name,
                                             initial_bal,
                                             ACE_TRY_ENV);

  Bank::Account_var acct_id2 = client->open (name,
                                             initial_bal,
                                             ACE_TRY_ENV);

  ACE_ASSERT (acct_id1->_is_equivalent ((CORBA::Object *) acct_id2.in ()) != 0);

  client->close (acct_id1.in (),
                 ACE_TRY_ENV);

  client->close (acct_id2.in (),
                 ACE_TRY_ENV);
}

// This method tests whether an account with different names can be opened

void
Bank_Client_i::test_for_different_name (CORBA::Environment &ACE_TRY_ENV)
{
  const char *name1 = "Name1";
  const char *name2 = "Name2";

  CORBA::Float initial_bal = 0.0;

  Bank::Account_var acct_id1 = client->open (name1,
                                             initial_bal,
                                             ACE_TRY_ENV);

  Bank::Account_var acct_id2 = client->open (name2,
                                             initial_bal,
                                             ACE_TRY_ENV);
  ACE_ASSERT (acct_id1->_is_equivalent ((CORBA::Object *)acct_id2.in ()) == 0);

  client->close (acct_id1.in (),
                 ACE_TRY_ENV);

  client->close (acct_id2.in (),
                 ACE_TRY_ENV);
}

// This method tests the Overdraft exception.

void
Bank_Client_i::test_for_overdraft (CORBA::Environment &ACE_TRY_ENV)
{

  CORBA::Float initial_bal = 100.0;
  const char *name = "Name";

  Bank::Account_var acct_id = client->open (name,
                                            initial_bal,
                                            ACE_TRY_ENV);
  acct_id->deposit (100.00,
                    ACE_TRY_ENV);

  acct_id->withdraw (acct_id->balance (ACE_TRY_ENV) + 20);

  client->close (acct_id.in (),
                 ACE_TRY_ENV);
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class Client<Bank::AccountManager,Bank::AccountManager_var>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate Client<Bank,Bank_var>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */