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
|
//=============================================================================
/**
* @file blobby.cpp
*
* $Id: blobby.cpp 93652 2011-03-28 08:55:39Z johnnyw $
*
* Simple client application to illustrate the use of the ACE_Blob class
*
* It reads "length" number of bytes, after skipping offset "offset"
* from hostname, port and filename as specified. (if -r specified)
*
* It writes "length" number of bytes, after skipping offset "offset"
* to hostname, port and filename as specified (if -w specified)
*
*
* @author Prashant Jain and Sumedh Mungee
*/
//=============================================================================
#include "Options.h"
#include "ace/OS_main.h"
#include "ace/OS_NS_fcntl.h"
#include "ace/OS_NS_unistd.h"
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
// Options is a singleton
Options *options = Options::instance ();
options->parse_args (argc, argv);
// Explain what is going to happen
if (options->debug_)
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("hostname = %C, port = %d, filename = %s, ")
ACE_TEXT ("length = %d, offset = %d, operation = %c\n"),
options->hostname_,
options->port_,
options->filename_,
options->length_,
options->offset_,
options->operation_));
// Create a blob
ACE_Blob blob;
// User requested a read
if (options->operation_ == 'r')
{
ACE_Message_Block mb (0, options->length_);
// Open the blob
if (blob.open (options->filename_,
options->hostname_,
options->port_) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("open error")),
-1);
// Read from it
if (blob.read (&mb, options->length_, options->offset_) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("read error")),
-1);
// Write to STDOUT
if (ACE_OS::write (ACE_STDOUT, mb.rd_ptr(), mb.length()) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("write error")),
-1);
}
else
{
int total = options->length_ + options->offset_;
ACE_Message_Block mb (total);
// Open the file to be sent
ACE_HANDLE h = ACE_OS::open (options->filename_, O_RDONLY);
if (h == ACE_INVALID_HANDLE)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("file open error")),
-1);
// Open the blob
if (blob.open (options->filename_, options->hostname_, options->port_) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("connection open error")),
-1);
// Read from the file
if (ACE_OS::read (h, mb.wr_ptr (), total) != total)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("file read error")),
-1);
// Close the file
ACE_OS::close (h);
// Adjust the offset
mb.wr_ptr (mb.size ());
// Write to the blob
if (blob.write (&mb, options->length_, options->offset_) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("network write error")),
-1);
}
blob.close ();
return 0;
}
|