summaryrefslogtreecommitdiff
path: root/TAO/DevGuideExamples/BiDirectionalGIOP/server.cpp
blob: 405b3d0637d105a4ac9196652bae49a4c84ae753 (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
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
// $Id$

#include "bidir_giop_pch.h"

#include "simple_i.h"
#include "callbackC.h"

#include "ace/Get_Opt.h"
#include "tao/BiDir_GIOP/BiDirGIOP.h"
#include <iostream>
#include <fstream>

ACE_TString ior_output_file;
int callback_count = 10;

int
parse_args(int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("o:i:"));
  int c;

  while((c = get_opts()) != -1)
    switch(c)
  {
      case 'o':
        ior_output_file = get_opts.optarg;
        break;
      case 'i':
        callback_count = ACE_OS::atoi(get_opts.optarg);
        break;
      case '?':
      default:
        std::cerr << "usage: " << argv[0] << "-o <iorfile> -i <no_iterations>" << std::endl;
        return -1;
        break;
  }
  // Indicates sucessful parsing of the command line
  return 0;
}

int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  try
  {
    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

    if (parse_args(argc, argv) != 0) {
      return 1;
    }

    // Create a bidirectional POA
    CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
    PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj.in());
    PortableServer::POAManager_var poa_manager = root_poa->the_POAManager();
    // Policies for the childPOA to be created.
    CORBA::PolicyList policies(1);
    policies.length(1);
    CORBA::Any pol;
    pol <<= BiDirPolicy::BOTH;
    policies[0] =
      orb->create_policy(BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE, pol);
    // Create POA as child of RootPOA with the above policies.  This POA
    // will receive request in the same connection in which it sent
    // the request
    PortableServer::POA_var poa = root_poa->create_POA("bidirPOA", poa_manager.in(), policies);
    // Creation of bidirPOA is over. Destroy the Policy objects.
    for (CORBA::ULong i = 0; i < policies.length (); ++i) {
      policies[i]->destroy ();
    }
    poa_manager->activate ();

    Simple_i svt(orb.in(), callback_count);

    // Register and activate Simple servant
    PortableServer::ObjectId_var id = poa->activate_object(&svt);
    obj = poa->id_to_reference(id.in());
    Simple_var server = Simple::_narrow(obj.in());

    CORBA::String_var ior = orb->object_to_string(server.in());
    if (ior_output_file != ACE_TEXT("")) {
      std::ofstream outfile(ACE_TEXT_ALWAYS_CHAR(ior_output_file.c_str()));
      outfile << ior.in();
    }
    std::cout << "Activated as " << ior.in() << std::endl;

    // Our own special orb->run() that knows how to callback clients
    while (true) {

      // returns 1 as soon as it has successfully called back.
      if (svt.call_client()) {
        break;
      }

      // We don't want to check for work pending, because we really want
      // to simulate a normal orb->run() while adding the ability to call
      // our routine which calls back to the client.
      orb->perform_work();
    }

    std::cout << "Event loop finished." << std::endl;

    int etherealize = 1, wait = 1;
    poa->destroy(etherealize, wait);
    orb->destroy();

    return 0;
  }
  catch(const CORBA::Exception& ex) {
    std::cerr << "Caught CORBA::Exception: " << std::endl << ex << std::endl;
  }

  return 1;
}