summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/Default_Servant/client.cpp
blob: 757d6c5c1bd37a11f55be3a4b38d6640d711bd46 (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
#include "ace/streams.h"
#include "ace/Get_Opt.h"
#include "FileC.h"

static char *ior = 0;
static char *filename = "test";
static char *message = "POA rules!!";

static int
parse_args (int argc, char **argv)
{
  ACE_Get_Opt get_opts (argc, argv, "k:");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        ior = get_opts.optarg;
        break;
      case 'f':
        filename = get_opts.optarg;
        break;
      case 'm':
        message = get_opts.optarg;
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           "-k IOR"
                           "\n",
                           argv [0]),
                          -1);
      }

  if (ior == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Please specify the IOR for the servant"), -1);

  // Indicates successful parsing of command line.
  return 0;
}

int
main (int argc, char **argv)
{
  CORBA::Environment env;

  CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, 0, env);
  if (env.exception () != 0)
    {
      env.print_exception ("CORBA::ORB_init");
      return -1;
    }

  parse_args (argc, argv);

  CORBA::Object_var object = orb->string_to_object (ior, env);
  if (env.exception () != 0)
    {
      env.print_exception ("CORBA::ORB::string_to_object");
      return -1;
    }

  File::System_var file_system = File::System::_narrow (object.in (), env);
  if (env.exception () != 0)
    {
      env.print_exception ("File::System::_narrow");
      return -1;
    }

  File::Descriptor_var fd = file_system->open (filename, O_CREAT | O_RDWR, env);
  if (env.exception () != 0)
    {
      env.print_exception ("File::System::open");
      return -1;
    }

  int message_length = ACE_OS::strlen (message) + 1;
  CORBA::Octet *buffer = File::Descriptor::DataBuffer::allocbuf (message_length);
  ACE_OS::strcpy ((char *) buffer, message);
  File::Descriptor::DataBuffer data_sent (message_length, message_length, buffer, CORBA::B_TRUE);

  fd->write (data_sent, env);
  if (env.exception () != 0)
    {
      env.print_exception ("File::Descriptor::write");
      return -1;
    }

  fd->lseek (0, SEEK_SET, env);
  if (env.exception () != 0)
    {
      env.print_exception ("File::Descriptor::lseek");
      return -1;
    }

  File::Descriptor::DataBuffer_var data_received = fd->read (message_length, env);
  if (env.exception () != 0)
    {
      env.print_exception ("File::Descriptor::read");
      return -1;
    }

  char *result = (char *) &data_received[0];
  cout << result << endl;

  fd->destroy (env);
  if (env.exception () != 0)
    {
      env.print_exception ("File::Descriptor::destroy");
      return -1;
    }

  return 0;
}