OmniEvents
eventf.cc
Go to the documentation of this file.
1 // Package : omniEvents
2 // eventf.cc Created : 2004-05-30
3 // Author : Alex Tingle
4 //
5 // Copyright (C) 2004 Alex Tingle
6 //
7 // This file is part of the omniEvents application.
8 //
9 // omniEvents is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // omniEvents is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // Description:
24 // Destroys the named EventChannel.
25 //
26 
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #ifdef HAVE_GETOPT
32 # include <unistd.h>
33 extern char* optarg;
34 extern int optind;
35 #else
36 # include "getopt.h"
37 #endif
38 
39 #ifdef HAVE_STDLIB_H
40 # include <stdlib.h> // exit()
41 #endif
42 
43 #ifdef HAVE_IOSTREAM
44 # include <iostream>
45 #else
46 # include <iostream.h>
47 #endif
48 
49 #include <cstdio>
50 
51 #ifdef HAVE_STD_IOSTREAM
52 using namespace std;
53 #endif
54 
55 #include "CosEventChannelAdmin.hh"
56 
57 static void usage(int argc, char **argv);
58 static CosEventChannelAdmin::EventChannel_ptr getChannel(const char* sior);
59 
60 CORBA::ORB_ptr orb;
61 
62 int
63 main(int argc, char **argv)
64 {
65  int result =1;
66 
67  //
68  // Start orb.
69 #if defined(HAVE_OMNIORB4)
70  orb=CORBA::ORB_init(argc,argv,"omniORB4");
71 #else
72  orb=CORBA::ORB_init(argc,argv,"omniORB3");
73 #endif
74 
75  // Process Options
76  int c;
77 
78  while((c = getopt(argc,argv,"h")) != EOF)
79  {
80  switch (c)
81  {
82  case 'h': usage(argc,argv);
83  exit(0);
84 
85  default : usage(argc,argv);
86  exit(-1);
87  }
88  }
89 
90  if(optind!=argc-2)
91  {
92  usage(argc,argv);
93  exit(-1);
94  }
95 
96  //
97  // Use one big try...catch block.
98  // 'action' variable keeps track of what we're doing.
99  const char* action ="start";
100  try
101  {
102  using namespace CosEventChannelAdmin;
103 
104  action="convert URI into reference to source channel";
105  EventChannel_var from_channel =getChannel(argv[optind]);
106 
107  action="convert URI into reference to destination channel";
108  EventChannel_var to_channel =getChannel(argv[optind+1]);
109 
110  action="obtain ConsumerAdmin";
111  ConsumerAdmin_var cadmin =from_channel->for_consumers();
112 
113  action="obtain ProxyPushSupplier";
114  ProxyPushSupplier_var supplier =cadmin->obtain_push_supplier();
115 
116  action="obtain SupplierAdmin";
117  SupplierAdmin_var sadmin =to_channel->for_suppliers();
118 
119  action="obtain ProxyPushConsumer";
120  ProxyPushConsumer_var consumer =sadmin->obtain_push_consumer();
121 
122  action="connect PushConsumer";
123  consumer->connect_push_supplier(supplier.in());
124 
125  action="connect PushSupplier";
126  supplier->connect_push_consumer(consumer.in());
127 
128  //
129  // Clean up nicely.
130  action="destroy orb";
131  orb->destroy();
132 
133  //
134  // If we get here, then everything has worked OK.
135  result=0;
136 
137  }
138  catch(CORBA::TRANSIENT& ex) { // _narrow()
139  cerr<<"Failed to "<<action<<". TRANSIENT"<<endl;
140  }
141  catch(CORBA::OBJECT_NOT_EXIST& ex) { // _narrow()
142  cerr<<"Failed to "<<action<<". OBJECT_NOT_EXIST"<<endl;
143  }
144  catch(CORBA::SystemException& ex) {
145  cerr<<"Failed to "<<action<<".";
146 #if defined(HAVE_OMNIORB4)
147  cerr<<" "<<ex._name();
148  if(ex.NP_minorString())
149  cerr<<" ("<<ex.NP_minorString()<<")";
150 #endif
151  cerr<<endl;
152  }
153  catch(CORBA::Exception& ex) {
154  cerr<<"Failed to "<<action<<"."
155 #if defined(HAVE_OMNIORB4)
156  " "<<ex._name()
157 #endif
158  <<endl;
159  }
160 
161  return result;
162 }
163 
164 
165 static void
166 usage(int argc, char **argv)
167 {
168  cerr<<
169 "\nConnect (federate) two event channels.\n"
170 "syntax: "<<(argc?argv[0]:"eventf")<<" OPTIONS [FROM_CHANNEL] [TO_CHANNEL]\n"
171 "\n"
172 "FROM/TO_CHANNEL: The event channels must be specified as a URI.\n"
173 " This may be an IOR, or a corbaloc::: or corbaname::: URI.\n"
174 "\n"
175 "OPTIONS:\n"
176 " -h display this help text\n" << endl;
177 }
178 
179 
180 //
181 // Obtain object reference to EventChannel
182 static CosEventChannelAdmin::EventChannel_ptr
183 getChannel(const char* sior)
184 {
185  // convert URI from command line into object reference";
186  CORBA::Object_var obj =orb->string_to_object(sior);
187 
188  // narrow object reference to event channel";
189  CosEventChannelAdmin::EventChannel_var channel =
190  CosEventChannelAdmin::EventChannel::_narrow(obj);
191  if(CORBA::is_nil(channel))
192  throw CORBA::OBJECT_NOT_EXIST();
193 
194  return channel._retn();
195 }
int optind
Definition: getopt.cc:82
char * optarg
Definition: getopt.cc:83
int getopt(int argc, char *argv[], const char *optionS)
Definition: getopt.cc:88
static CosEventChannelAdmin::EventChannel_ptr getChannel(const char *sior)
Definition: eventf.cc:183
int main(int argc, char **argv)
The main process entry point.
Definition: eventf.cc:63
CORBA::ORB_ptr orb
Definition: eventf.cc:60
static void usage(int argc, char **argv)
Definition: eventf.cc:166