> Now, a small feature quest:
> 
> I would like a flag to xorpsh to make it die immediately if it cannot 
> connect to
> the specified router-mgr (based on the port specified in the environment 
> variable).
> This will make it a lot easier to script.

Please try the following patch and let me know whether it works for
you.

The new xorpsh flag is "-e" to exit immediately if the connect
failed:

xorpsh -e


Thanks,
Pavlin

Index: libxipc/finder_tcp_messenger.cc
===================================================================
RCS file: /usr/local/share/doc/apache/cvs/xorp/libxipc/finder_tcp_messenger.cc,v
retrieving revision 1.31
diff -u -p -r1.31 finder_tcp_messenger.cc
--- libxipc/finder_tcp_messenger.cc	1 Aug 2007 19:27:49 -0000	1.31
+++ libxipc/finder_tcp_messenger.cc	11 Oct 2007 01:13:03 -0000
@@ -304,8 +304,8 @@ FinderTcpAutoConnector::FinderTcpAutoCon
 				uint32_t 		give_up_ms
 				)
     : FinderTcpConnector(e, *this, cmds, host, port),
-      _real_manager(real_manager), _connected(false), _enabled(en),
-      _once_active(false), _last_error(0), _consec_error(0)
+      _real_manager(real_manager), _connected(false), _connect_failed(false),
+      _enabled(en), _once_active(false), _last_error(0), _consec_error(0)
 {
     if (en) {
 	start_timer();
@@ -357,11 +357,19 @@ FinderTcpAutoConnector::connected() cons
     return _connected;
 }
 
+bool
+FinderTcpAutoConnector::connect_failed() const
+{
+    return _connect_failed;
+}
+
 void
 FinderTcpAutoConnector::do_auto_connect()
 {
     XLOG_ASSERT(false == _connected);
 
+    _connect_failed = false;
+
     FinderTcpMessenger* fm;
     int r = connect(fm);
     if (r == 0) {
@@ -370,6 +378,7 @@ FinderTcpAutoConnector::do_auto_connect(
 	_connected = true;
     } else {
 	XLOG_ASSERT(fm == 0);
+	_connect_failed = true;
 	if (r != _last_error) {
 	    XLOG_ERROR("Failed to connect to %s/%u: %s",
 		       _host.str().c_str(), _port, strerror(r));
Index: libxipc/finder_tcp_messenger.hh
===================================================================
RCS file: /usr/local/share/doc/apache/cvs/xorp/libxipc/finder_tcp_messenger.hh,v
retrieving revision 1.19
diff -u -p -r1.19 finder_tcp_messenger.hh
--- libxipc/finder_tcp_messenger.hh	1 Aug 2007 19:27:49 -0000	1.19
+++ libxipc/finder_tcp_messenger.hh	11 Oct 2007 01:13:03 -0000
@@ -153,6 +153,7 @@ public:
     void set_enabled(bool en);
     bool enabled() const;
     bool connected() const;
+    bool connect_failed() const;
 
 protected:
     void do_auto_connect();
@@ -175,6 +176,7 @@ protected:
 protected:
     FinderMessengerManager& _real_manager;
     bool		    _connected;
+    bool		    _connect_failed;
     bool		    _enabled;
     bool		    _once_active;
     XorpTimer		    _retry_timer;
Index: libxipc/xrl_router.cc
===================================================================
RCS file: /usr/local/share/doc/apache/cvs/xorp/libxipc/xrl_router.cc,v
retrieving revision 1.54
diff -u -p -r1.54 xrl_router.cc
--- libxipc/xrl_router.cc	23 May 2007 12:12:40 -0000	1.54
+++ libxipc/xrl_router.cc	11 Oct 2007 01:13:03 -0000
@@ -290,6 +290,12 @@ XrlRouter::connected() const
 }
 
 bool
+XrlRouter::connect_failed() const
+{
+    return _fac && _fac->connect_failed();
+}
+
+bool
 XrlRouter::ready() const
 {
     return _fc && _fc->ready();
Index: libxipc/xrl_router.hh
===================================================================
RCS file: /usr/local/share/doc/apache/cvs/xorp/libxipc/xrl_router.hh,v
retrieving revision 1.38
diff -u -p -r1.38 xrl_router.hh
--- libxipc/xrl_router.hh	23 May 2007 12:12:40 -0000	1.38
+++ libxipc/xrl_router.hh	11 Oct 2007 01:13:03 -0000
@@ -84,6 +84,12 @@ public:
     bool connected() const;
 
     /**
+     * @return true if instance has encountered a connection error to the
+     * Finder.
+     */
+    bool connect_failed() const;
+
+    /**
      * @return true if instance has established a connection to the Finder,
      * registered own XRLs, and should be considered operational.
      */
Index: rtrmgr/xorpsh_main.cc
===================================================================
RCS file: /usr/local/share/doc/apache/cvs/xorp/rtrmgr/xorpsh_main.cc,v
retrieving revision 1.68
diff -u -p -r1.68 xorpsh_main.cc
--- rtrmgr/xorpsh_main.cc	30 Sep 2007 03:55:15 -0000	1.68
+++ rtrmgr/xorpsh_main.cc	11 Oct 2007 01:13:03 -0000
@@ -79,7 +79,8 @@ announce_waiting()
 }
 
 static bool
-wait_for_xrl_router_ready(EventLoop& eventloop, XrlRouter& xrl_router)
+wait_for_xrl_router_ready(EventLoop& eventloop, XrlRouter& xrl_router,
+			  bool exit_on_error)
 {
     XorpTimer announcer = eventloop.new_oneoff_after_ms(
 				3 * 1000, callback(&announce_waiting)
@@ -91,6 +92,8 @@ wait_for_xrl_router_ready(EventLoop& eve
 	    return false;
 	    break;
 	}
+	if (xrl_router.connect_failed() && exit_on_error)
+	    return false;
     }
     return true;
 }
@@ -195,7 +198,7 @@ XorpShell::~XorpShell()
 }
 
 void
-XorpShell::run(const string& commands)
+XorpShell::run(const string& commands, bool exit_on_error)
 {
     bool success;
     string error_msg;
@@ -238,7 +241,8 @@ XorpShell::run(const string& commands)
     _cli_node.set_cli_client_delete_callback(callback(exit_handler));
 
     _is_connected_to_finder = false;
-    if (wait_for_xrl_router_ready(_eventloop, _xrl_router) == false) {
+    if (wait_for_xrl_router_ready(_eventloop, _xrl_router, exit_on_error)
+	== false) {
 	// RtrMgr contains finder
 	error_msg = c_format("Failed to connect to the router manager");
 	xorp_throw(InitError, error_msg);
@@ -797,6 +801,7 @@ usage(const char *argv0)
     fprintf(stderr, "Usage: %s [options]\n", xorp_basename(argv0));
     fprintf(stderr, "Options:\n");
     fprintf(stderr, "  -c        Specify command(s) to execute\n");
+    fprintf(stderr, "  -e        Exit immediately if cannot connect to the rtrmgr\n");
     fprintf(stderr, "  -h        Display this information\n");
     fprintf(stderr, "  -v        Print verbose information\n");
     fprintf(stderr, "  -t <dir>  Specify templates directory\n");
@@ -820,6 +825,7 @@ main(int argc, char *argv[])
 {
     int errcode = 0;
     string commands;
+    bool exit_on_error = false;
 
     //
     // Initialize and start xlog
@@ -848,13 +854,16 @@ main(int argc, char *argv[])
     string xrl_targets_dir	= xorp_xrl_targets_dir();
 
     int c;
-    while ((c = getopt(argc, argv, "c:t:x:vh")) != EOF) {
+    while ((c = getopt(argc, argv, "c:et:x:vh")) != EOF) {
 	switch(c) {
 	case 'c':
 	    // XXX: Append the arguments to allow multiple "-c cmd" commands
 	    commands += optarg;
 	    commands += "\n";
 	    break;
+	case 'e':
+	    exit_on_error = true;
+	    break;
 	case 't':
 	    template_dir = optarg;
 	    break;
@@ -894,7 +903,7 @@ main(int argc, char *argv[])
 					   hostname);
 	XorpShell xorpsh(eventloop, xname, xorp_binary_root_dir(),
 			 template_dir, xrl_targets_dir, verbose);
-	xorpsh.run(commands);
+	xorpsh.run(commands, exit_on_error);
     } catch (const InitError& e) {
 	XLOG_ERROR("xorpsh exiting due to an init error: %s", e.why().c_str());
 	errcode = 1;
Index: rtrmgr/xorpsh_main.hh
===================================================================
RCS file: /usr/local/share/doc/apache/cvs/xorp/rtrmgr/xorpsh_main.hh,v
retrieving revision 1.33
diff -u -p -r1.33 xorpsh_main.hh
--- rtrmgr/xorpsh_main.hh	16 Feb 2007 22:47:27 -0000	1.33
+++ rtrmgr/xorpsh_main.hh	11 Oct 2007 01:13:03 -0000
@@ -47,7 +47,7 @@ public:
 	      bool verbose) throw (InitError);
     ~XorpShell();
 
-    void run(const string& command);
+    void run(const string& command, bool exit_on_error);
     bool done() const;
 
 
_______________________________________________
Xorp-hackers mailing list
[email protected]
http://mailman.ICSI.Berkeley.EDU/mailman/listinfo/xorp-hackers

Reply via email to