summaryrefslogtreecommitdiff
path: root/TAO/tests/File_IO/client.cpp
blob: 757ba9a4afebea57cd54a9e88443fe89fcd3a8ff (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
// $Id$

//===================================================================
//  = LIBRARY
//      TAO/tests/POA/Default_Servant/client
//
//  = FILENAME
//     client.cpp
//
//  = DESCRIPTION
//      A client program for the File IDL module
//
//  = AUTHOR
//     Irfan Pyarali
//
//====================================================================

#include "FileC.h"
#include "tao/debug.h"
#include "ace/streams.h"
#include "ace/Get_Opt.h"
#include "ace/Read_Buffer.h"
#include "ace/OS.h"
#include "ace/Thread_Manager.h"

ACE_RCSID(Default_Servant, client, "client.cpp,v 1.8 2001/03/26 21:16:52 coryan Exp")

static const char *iorfile = "ior";
static const char *filename = "big.txt";
static const int NUM_THREADS = 10;
static CORBA::ORB_var orb;
static int loops=100;

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

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

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

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

static void *
MTTEST (void *args)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_CString& ior = *(ACE_CString*)args;
  ACE_TRY
    {
    CORBA::Object_var object = orb->string_to_object (ior.c_str (),
                                                      ACE_TRY_ENV);
    ACE_TRY_CHECK;

    // Narrow the object reference to a File::System
    File::System_var file_system = File::System::_narrow (object.in (),
                                                          ACE_TRY_ENV);
    ACE_TRY_CHECK;

    // Creat the file filename i.e "test"
    File::Descriptor_var fd = file_system->open (filename,
                                                 O_RDONLY,
                                                 ACE_TRY_ENV);
    ACE_TRY_CHECK;

    for( int i=0; i<loops; ++i)
    {
      //seek to the beginning of the file
      ACE_DEBUG((LM_DEBUG,"Making request number %d\n",i));
      fd->lseek (0, SEEK_SET, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Read back the written message
      File::Descriptor::DataBuffer_var data_received = fd->read (1024*1024,
                                                                 ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }

    // close the file
    fd->destroy (ACE_TRY_ENV);
    ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Exception caught in main");
      return 0;
    }
  ACE_ENDTRY;

  return 0;
}

int
main (int argc, char **argv)
{
  ACE_DECLARE_NEW_CORBA_ENV;

  ACE_TRY
    {
      // Initialize the ORB
      orb = CORBA::ORB_init (argc, argv, 0, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Parse the command-line arguments to get the IOR
      parse_args (argc, argv);

      // parse args should catch this, but just in case...
      if (iorfile == 0)
        return 0;

      // Read the file, and get the IOR
      ACE_HANDLE input_file = ACE_OS::open (iorfile, 0);
      if (input_file == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open input file for reading IOR: %s\n",
                           iorfile),
                          -1);
      ACE_Read_Buffer ior_buffer (input_file);
      char *data = ior_buffer.read ();
      if (data == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to read ior\n"),
                          -1);

      ACE_CString ior = data;
      ior_buffer.alloc ()-> free (data);
      ACE_OS::close (input_file);

      if (ACE_Thread_Manager::instance ()->spawn_n (NUM_THREADS,
                                                ACE_THR_FUNC (MTTEST),
                                                (void *)&ior,
                                                THR_NEW_LWP | THR_DETACHED) == -1)
        {
         ACE_ERROR ((LM_ERROR,
                     ACE_TEXT ("%p\n%a"),
                     ACE_TEXT ("thread create failed")));
        }
        ACE_Thread_Manager::instance()->wait();
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Exception caught in main");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}