예제 #1
0
void *
read_file (void *fd)
{
  ACE_TLI_Stream stream;
  char buf[BUFSIZ];
  int flags = 0;
  ssize_t n;

  // Cast the arg to a long, first, because a pointer is the same size
  // as a long on all current ACE platforms.
  stream.set_handle ((int) (long) fd);

  ACE_DEBUG((LM_DEBUG, "start (tid = %t, fd = %d)\n",
             stream.get_handle ()));

  while ((n = stream.recv (buf, sizeof buf, &flags)) > 0)
    continue;

  ACE_UNUSED_ARG (n);

  ACE_DEBUG ((LM_DEBUG,"finish (tid = %t, fd = %d)\n",
              stream.get_handle ()));

  if (stream.close () == -1)
    ACE_OS::t_error ("stream.close error");

  return 0;
}
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  parse_args (argc, argv);
  void *cp;
  char buf[BUFSIZ];
  ACE_TLI_Stream sc;
  ACE_TLI_Connector con;

  if (con.connect (sc,
                   ACE_INET_Addr (port_number,
                                  host_name)) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "open"),
                      -1);

  ACE_Mem_Map mmap (file_name, PROT_READ);

  if (mmap (cp) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",

                       "mmap"), -1);

  // Next, send the file's contents.

  if (sc.send_n (cp, mmap.size ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",

                       "send_n"), -1);

  if (sc.sndrel () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close_writer"),
                      -1);

  for (int n; (n = sc.recv (buf, sizeof buf)) > 0; )
    if (ACE_OS::write (ACE_STDOUT, buf, n) != n)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "%p\n",
                         "write"),
                        -1);

  if (sc.close () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "close"),
                      -1);
  return 0;
}
예제 #3
0
void *
lookup_name (ACE_HANDLE handle)
{
  enum
  {
    MAXLINE = 255,
    EMPNAMELEN = 512
  };

  static struct
  {
    int emp_id;
    const char *emp_name;
  } employee_db[] =
    {
      {123, "John Wayne Bobbit"},
      {124, "Woody Allen"},
      {125, "O. J. Simpson"},
      {126, "Bill Clinton"},
      {127, "Rush Limbaugh"},
      {128, "Michael Jackson"},
      {129, "Kenneth Starr"},
      {130, "Paula Jones"},
      {131, "Monica Lewinsky"},
      {132, "Marv Albert"},
      {0, ""}
    };

  int flags;
  int employee_id;
  int index;
  int found;
  ACE_TLI_Stream stream;
  char recvline[MAXLINE];
  char sendline[MAXLINE];

  ACE_DEBUG ((LM_DEBUG,
              "stream handle = %d, thread id = %t\n",
              handle));
  stream.set_handle (handle);

  ssize_t n = stream.recv (recvline, MAXLINE, &flags);

  if (n == -1)
    ACE_OS::t_error ("stream.recv error");

  employee_id = ACE_OS::atoi (recvline);
  found = 0;

  for (index = 0; found == 0 && employee_db[index].emp_id; index++)
    if (employee_id == employee_db[index].emp_id)
      {
        found = 1;
        n = ACE_OS::sprintf (sendline,
                             "%s", employee_db[index].emp_name);
      }

  if (found == 0)
    n = ACE_OS::sprintf (sendline, "%s", "ERROR");

  if (stream.send (sendline, n + 1, 0) == -1)
    ACE_OS::t_error ("stream.send error");

  if (stream.sndrel () == -1)
    ACE_OS::t_error ("stream.send error");

  if (stream.close () == -1)
    ACE_OS::t_error ("stream.close error");

  return 0;
}