summaryrefslogtreecommitdiff
path: root/ACE/examples/C++NPv2/TP_Logging_Server.cpp
blob: 7342cf4c757302201e52c0136c86f3fedeb993a6 (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
/*
** $Id$
**
** Copyright 2002 Addison Wesley. All Rights Reserved.
*/

#include "ace/OS_Memory.h"
#include "ace/Guard_T.h"
#include "ace/Message_Block.h"
#include "TP_Logging_Server.h"

int TP_Logging_Handler::handle_input (ACE_HANDLE) {
  ACE_Message_Block *mblk = 0;
  if (logging_handler_.recv_log_record (mblk) != -1) {
    ACE_Message_Block *log_blk = 0;
    ACE_NEW_RETURN
      (log_blk, ACE_Message_Block
                  (reinterpret_cast<char *> (this)), -1);
    log_blk->cont (mblk);
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, lock_, -1);
    if (TP_LOGGING_TASK::instance ()->put (log_blk) == -1)
    { log_blk->release (); return -1; }
    ++queued_count_;
    return 0;
  } else return -1;
}


int
TP_Logging_Handler::handle_close (ACE_HANDLE handle,
                                  ACE_Reactor_Mask) {
  int close_now = 0;
  if (handle != ACE_INVALID_HANDLE) {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, lock_, -1);
    if (queued_count_ == 0)
      close_now = 1;
    else
      deferred_close_ = 1;
  } else {
    ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, lock_, -1);
    queued_count_--;
    if (queued_count_ == 0) close_now = deferred_close_;
  }

  if (close_now)
    return Logging_Event_Handler::handle_close ();
  return 0;
}


int TP_Logging_Task::svc () {
  for (ACE_Message_Block *log_blk; getq (log_blk) != -1; ) {
    TP_Logging_Handler *tp_handler = reinterpret_cast<TP_Logging_Handler *> (log_blk->rd_ptr ());
    Logging_Handler logging_handler (tp_handler->log_file ());
    logging_handler.write_log_record (log_blk->cont ());

    log_blk->release ();
    tp_handler->handle_close (ACE_INVALID_HANDLE, 0);
  }
  return 0;
}

ACE_FACTORY_DEFINE (TPLS, TP_Logging_Server)

#if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
template ACE_Singleton<TP_Logging_Task, ACE_Null_Mutex> *
  ACE_Singleton<TP_Logging_Task, ACE_Null_Mutex>::singleton_;
template ACE_Unmanaged_Singleton<TP_Logging_Task, ACE_Null_Mutex> *
  ACE_Unmanaged_Singleton<TP_Logging_Task, ACE_Null_Mutex>::singleton_;
#endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */