summaryrefslogtreecommitdiff
path: root/TAO/DevGuideExamples/Multithreading/GracefulShutdown/MessengerServer.h
blob: 4674176a963f4cee539b0b251756696e4eaaf014 (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
/* -*- C++ -*-  */

#ifndef MESSENGERSERVER_H_
#define MESSENGERSERVER_H_

#include "MessengerS.h"
#include "ace/Task.h"
#include <iostream>

//Class MessengerServer
class MessengerServer
{
public:
  //Constructor
  MessengerServer (CORBA::ORB_ptr orb);

  //Destructor
  virtual ~MessengerServer (void);

  // parse arguments
  int parse_args (int argc, ACE_TCHAR* argv[]);

  // run the ORB's event loop continuously
  void run ();

  // run the ORB's event loop for some number of seconds
  void run (int seconds);

  // handle ORB events in a polling loop for some number of iterations
  void poll (int iterations);

  // schedule a shutdown timer with the ORB's reactor to timeout
  // after some seconds
  void schedule_shutdown_timer (int seconds);

  // spawn thread to monitor console and shutdown on console input
  void shutdown_on_console_input ();

  enum ShutdownMethod {
    s_client_call,     // shutdown on client invocation
    s_polling_loop,    // shutdown after some iterations through loop
    s_timer,           // schedule a timer to shutdown
    s_console_input,   // shutdown on console input
    s_run_time_value   // use CORBA::ORB::run() with time value
  };

  // Task to monitor console input.
  class ConsoleMonitor : public ACE_Task_Base
  {
  public:
    ConsoleMonitor (CORBA::ORB_ptr orb)
      : orb_(CORBA::ORB::_duplicate(orb))
      {
      }

    virtual int svc()
      {
        char c;
        std::cin.get (c);
        orb_->shutdown (true);
        return 0;
      }
  private:
    CORBA::ORB_var orb_;
  };

private:
  CORBA::ORB_var orb_;
  ConsoleMonitor * monitor_;
};

#endif /* MESSENGERSERVER_H_  */