summaryrefslogtreecommitdiff
path: root/TAO/examples/RTScheduling/Starter.cpp
blob: 8abff46a9f08d6d9ee0bee5418516da9f5399176 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//$Id$

#include "Starter.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/Argv_Type_Converter.h"


Starter::Starter (CORBA::ORB_ptr orb)
{
  // Initialize the naming service
  if (this->naming_client_.init (orb) != 0)
    ACE_ERROR ((LM_ERROR,
		" (%P|%t) Unable to initialize "
		"the TAO_Naming_Client. \n"));
}

void
Starter::init (ACE_ENV_SINGLE_ARG_DECL)
{
  this->resolve_synch_objs (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->fire ();
}

void
Starter::fire (void)
{
  ACE_Time_Value base_time = ACE_OS::gettimeofday ();
  for (Synchs::iterator iterator = this->synchs_.begin ();
       iterator != this->synchs_.end ();
       ++iterator)
    {
      (*iterator).int_id_.in ()->go (base_time.sec ());
    }
}

void
Starter::resolve_synch_objs (ACE_ENV_SINGLE_ARG_DECL)
{
  CosNaming::Name name (1);
  name.length (1);

  // Get the sender context.
  name [0].id =
    CORBA::string_dup ("Synch");

  CORBA::Object_var object =
    this->naming_client_->resolve (name
				   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  this->synch_context_ =
    CosNaming::NamingContext::_narrow (object.in ());


  CosNaming::BindingIterator_var iterator;
  CosNaming::BindingList_var binding_list;
  const CORBA::ULong chunk = 100;

  // Get the list of synchs registered for this sender.
  this->synch_context_->list (chunk,
			      binding_list,
			      iterator
			      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  // Add the receivers found in the bindinglist to the <receivers>.
  this->add_to_synchs (binding_list
		       ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (!CORBA::is_nil (iterator.in ()))
    {
      CORBA::Boolean more = 1;

      // Check to see if there are more receivers listed.
      while (more)
        {
          more = iterator->next_n (chunk,
                                   binding_list
                                   ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;

          this->add_to_synchs (binding_list
			       ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;
        }
    }

}


void
Starter::add_to_synchs (CosNaming::BindingList &binding_list
			ACE_ENV_ARG_DECL)
{
  ACE_Time_Value base_time = ACE_OS::gettimeofday ();
  for (CORBA::ULong i = 0;
       i < binding_list.length ();
       i++)
    {
      // Get the receiver name from the binding list.
      ACE_CString synch_name =
        binding_list [i].binding_name [0].id.in ();

      ACE_DEBUG ((LM_DEBUG,
		  "Synch Name %s\n",
		  synch_name.c_str ()));

      CosNaming::Name name (1);
      name.length (1);
      name [0].id =
        CORBA::string_dup (synch_name.c_str ());

      // Resolve the reference of the receiver from the receiver
      // context.
      CORBA::Object_var obj =
        this->synch_context_->resolve (name
				       ACE_ENV_ARG_PARAMETER);

      Synch_var synch_obj =
        Synch::_narrow (obj.in ());


      synch_obj->go (base_time.sec ());

//        // Add this receiver to the receiver map.
//        this->synchs_.bind (synch_name,
//  			  synch_obj);
    }
}


int
ACE_TMAIN (int argc, ACE_TCHAR** argv)
{
  ACE_Argv_Type_Converter convert (argc, argv);
  ACE_TRY_NEW_ENV
    {
      CORBA::ORB_var orb = CORBA::ORB_init (convert.get_argc(),
                                            convert.get_ASCII_argv(),
					    ""
					    ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      Starter starter (orb.in ());

      starter.init (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Caught exception:");
      return 1;
    }
  ACE_ENDTRY;
return 0;
}