Fixing bug with xslt processing and adding basic routines to run autoconfiguration

Change-Id: I5764360e82ce329d6951db7298fd2a04df3f2b96
diff --git a/osx/Resources/status-to-fib.xslt b/osx/Resources/status-to-fib.xslt
index e4ad841..58f2fc7 100644
--- a/osx/Resources/status-to-fib.xslt
+++ b/osx/Resources/status-to-fib.xslt
@@ -3,14 +3,14 @@
 
 <xsl:template match="/ndnd">
 <fibs>
-<xsl:apply-templates select="forwarding/fentry" />
+<xsl:apply-templates select="forwarding/fentry/dest" />
 </fibs>
 </xsl:template>
 
-<xsl:template match="fentry">
+<xsl:template match="dest">
 <fib>
-<xsl:apply-templates select="dest/faceid" />
-<prefix><xsl:value-of select="prefix"/></prefix>
+<xsl:apply-templates select="faceid" />
+<prefix><xsl:value-of select="../prefix"/></prefix>
 </fib>
 </xsl:template>
 
diff --git a/osx/menu-delegate.h b/osx/menu-delegate.h
index 5e11476..d6051cd 100644
--- a/osx/menu-delegate.h
+++ b/osx/menu-delegate.h
@@ -43,6 +43,8 @@
   IBOutlet NSTextField *connectionStatusText;
   
   IBOutlet PreferenceDelegate *preferencesDelegate;
+
+  bool m_autoconfInProgress;
 }
 
 -(void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
@@ -55,4 +57,6 @@
 -(void)statusUpdated:(NSXMLDocument*)document;
 -(void)statusUnavailable:(id)none;
 
+-(void)restartDaemon:(id)none;
+
 @end
diff --git a/osx/menu-delegate.mm b/osx/menu-delegate.mm
index 687f29c..b46a07d 100644
--- a/osx/menu-delegate.mm
+++ b/osx/menu-delegate.mm
@@ -18,6 +18,7 @@
     return nil;
   }
 
+  m_autoconfInProgress = false;
   m_operationQueue = [[NSOperationQueue alloc] init];
   return self;
 }
@@ -34,11 +35,11 @@
   m_statusXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status" ofType:@"xslt"]];
   m_statusToFibXslt = [NSData dataWithContentsOfFile:[bundle pathForResource:@"status-to-fib" ofType:@"xslt"]];
   
-  NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
-                target: self
-                selector:@selector(onTick:)
-                userInfo: nil
-                repeats:YES];
+  [NSTimer scheduledTimerWithTimeInterval: 1.0
+           target: self
+           selector:@selector(onTick:)
+           userInfo: nil
+           repeats:YES];
   [self updateStatus];
 }
 
@@ -154,6 +155,13 @@
   [daemonStatusHtml setAttributedStringValue:m_statusString];
 
   [preferencesDelegate updateFibStatus:statusFibXml];
+
+  NSArray *autoconf = [[statusFibXml rootElement] nodesForXPath:@"//fib/prefix[text()='ndn:/autoconf-route']" error:nil];
+  if ([autoconf count] == 0)
+    {
+      NSLog (@"No automatically detected route configured, trying to get one");
+      [self restartDaemon:nil];
+    }
 }
 
 - (void)statusUnavailable:(id)none
@@ -170,11 +178,43 @@
   [daemonStatusHtml setStringValue:@""];
   [preferencesDelegate updateFibStatus:nil];
 
-  [m_operationQueue addOperationWithBlock:^{
+  m_autoconfInProgress = true;
+  
+  NSOperation *startOp = [NSBlockOperation blockOperationWithBlock:^{
       NSTask *task = [[NSTask alloc] init];
       [task setLaunchPath: @NDND_START_COMMAND];
       [task launch];
     }];
+
+  NSOperation *autoconfOp = [NSBlockOperation blockOperationWithBlock:^{
+      NSTask *task = [[NSTask alloc] init];
+      [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND];
+      [task launch];
+      [task waitUntilExit];
+
+      m_autoconfInProgress = false;
+    }];
+
+  [autoconfOp addDependency:startOp];
+
+  [m_operationQueue addOperation:startOp];
+  [m_operationQueue addOperation:autoconfOp];
+}
+
+-(void)restartDaemon:(id)none
+{
+  if (m_autoconfInProgress)
+    return;
+
+  m_autoconfInProgress = true;
+  [m_operationQueue addOperationWithBlock:^{
+      NSTask *task = [[NSTask alloc] init];
+      [task setLaunchPath: @NDND_AUTOCONFIG_COMMAND];
+      [task launch];
+      [task waitUntilExit];
+
+      m_autoconfInProgress = false;
+    }];
 }
 
 @end
diff --git a/wscript b/wscript
index 3f1d6a6..ebcf01e 100644
--- a/wscript
+++ b/wscript
@@ -30,6 +30,7 @@
     conf.define('NDND_STOP_COMMAND', '%s/bin/ndndstop' % conf.options.ndnx_root)
     conf.define('NDND_STATUS_COMMAND', '%s/bin/ndndsmoketest' % conf.options.ndnx_root)
     conf.define('NDND_FIB_COMMAND',  '%s/bin/ndndc' % conf.options.ndnx_root)
+    conf.define('NDND_AUTOCONFIG_COMMAND', '%s/bin/ndnd-autoconfig' % conf.options.ndnx_root)
     
     if Utils.unversioned_sys_platform () == "darwin":
         conf.find_program('ibtool', var='IBTOOL', mandatory=False)