Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / apps / JAWS / clients / Blobby / blobby.cpp
blob43ef3ef10ccb91b878937b028809cf2491469bb5
2 //=============================================================================
3 /**
4 * @file blobby.cpp
6 * Simple client application to illustrate the use of the ACE_Blob class
8 * It reads "length" number of bytes, after skipping offset "offset"
9 * from hostname, port and filename as specified. (if -r specified)
11 * It writes "length" number of bytes, after skipping offset "offset"
12 * to hostname, port and filename as specified (if -w specified)
14 * @author Prashant Jain and Sumedh Mungee
16 //=============================================================================
19 #include "Options.h"
20 #include "ace/OS_main.h"
21 #include "ace/OS_NS_fcntl.h"
22 #include "ace/OS_NS_unistd.h"
24 int
25 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
27 // Options is a singleton
28 Options *options = Options::instance ();
29 options->parse_args (argc, argv);
31 // Explain what is going to happen
32 if (options->debug_)
33 ACE_DEBUG ((LM_DEBUG,
34 ACE_TEXT ("hostname = %C, port = %d, filename = %s, ")
35 ACE_TEXT ("length = %d, offset = %d, operation = %c\n"),
36 options->hostname_,
37 options->port_,
38 options->filename_,
39 options->length_,
40 options->offset_,
41 options->operation_));
43 // Create a blob
44 ACE_Blob blob;
46 // User requested a read
47 if (options->operation_ == 'r')
49 ACE_Message_Block mb (0, options->length_);
51 // Open the blob
52 if (blob.open (options->filename_,
53 options->hostname_,
54 options->port_) == -1)
55 ACE_ERROR_RETURN ((LM_ERROR,
56 ACE_TEXT ("%p\n"),
57 ACE_TEXT ("open error")),
58 -1);
60 // Read from it
61 if (blob.read (&mb, options->length_, options->offset_) == -1)
62 ACE_ERROR_RETURN ((LM_ERROR,
63 ACE_TEXT ("%p\n"),
64 ACE_TEXT ("read error")),
65 -1);
67 // Write to STDOUT
68 if (ACE_OS::write (ACE_STDOUT, mb.rd_ptr(), mb.length()) == -1)
69 ACE_ERROR_RETURN ((LM_ERROR,
70 ACE_TEXT ("%p\n"),
71 ACE_TEXT ("write error")),
72 -1);
74 else
76 int total = options->length_ + options->offset_;
77 ACE_Message_Block mb (total);
79 // Open the file to be sent
80 ACE_HANDLE h = ACE_OS::open (options->filename_, O_RDONLY);
81 if (h == ACE_INVALID_HANDLE)
82 ACE_ERROR_RETURN ((LM_ERROR,
83 ACE_TEXT ("%p\n"),
84 ACE_TEXT ("file open error")),
85 -1);
87 // Open the blob
88 if (blob.open (options->filename_, options->hostname_, options->port_) == -1)
89 ACE_ERROR_RETURN ((LM_ERROR,
90 ACE_TEXT ("%p\n"),
91 ACE_TEXT ("connection open error")),
92 -1);
94 // Read from the file
95 if (ACE_OS::read (h, mb.wr_ptr (), total) != total)
96 ACE_ERROR_RETURN ((LM_ERROR,
97 ACE_TEXT ("%p\n"),
98 ACE_TEXT ("file read error")),
99 -1);
101 // Close the file
102 ACE_OS::close (h);
104 // Adjust the offset
105 mb.wr_ptr (mb.size ());
107 // Write to the blob
108 if (blob.write (&mb, options->length_, options->offset_) == -1)
109 ACE_ERROR_RETURN ((LM_ERROR,
110 ACE_TEXT ("%p\n"),
111 ACE_TEXT ("network write error")),
112 -1);
115 blob.close ();
116 return 0;