From 5ac3cde2bdeec58156f2d34a5ea4de710662b77e Mon Sep 17 00:00:00 2001
From: Cody Burkard <cody@onlab.us>
Date: Tue, 15 Jul 2014 15:48:18 -0700
Subject: [PATCH] restructured defaultController into a function

---
 bin/mn          |  2 +-
 mininet/node.py | 27 +++++++++++++++------------
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/bin/mn b/bin/mn
index 3d01a583..04761daf 100755
--- a/bin/mn
+++ b/bin/mn
@@ -25,7 +25,7 @@ from mininet.cli import CLI
 from mininet.log import lg, LEVELS, info, debug, error
 from mininet.net import Mininet, MininetWithControlNet, VERSION
 from mininet.node import ( Host, CPULimitedHost, Controller, OVSController,
-                           NOX, RemoteController, DefaultController, UserSwitch, OVSKernelSwitch,
+                           NOX, DefaultController, RemoteController, UserSwitch, OVSKernelSwitch,
                            OVSLegacyKernelSwitch, IVSSwitch )
 from mininet.link import Link, TCLink
 from mininet.topo import SingleSwitchTopo, LinearTopo, SingleSwitchReversedTopo
diff --git a/mininet/node.py b/mininet/node.py
index 26802656..41b2d45b 100644
--- a/mininet/node.py
+++ b/mininet/node.py
@@ -1277,23 +1277,19 @@ def __repr__( self ):
         return '<%s %s: %s:%s pid=%s> ' % (
             self.__class__.__name__, self.name,
             self.IP(), self.port, self.pid )
-
-class DefaultController( Controller ):
-    "find any controller that is available and run it"
-    def __init__( self, name, **kwargs ):
-        "search for any installed controller"
-        controllers = [ 'controller', 'ovs-controller',
-                        'test-controller' ]
-        for c in controllers:
-            if quietRun( "which " + c ):
-                Controller.__init__( self, name, command=c, **kwargs )
-                break
+    @classmethod
+    def isAvailable( self ):
+        return quietRun( 'which controller' )
 
 class OVSController( Controller ):
     "Open vSwitch controller"
     def __init__( self, name, command='ovs-controller', **kwargs ):
+        if quietRun( 'which test-controller' ):
+            command = 'test-controller'
         Controller.__init__( self, name, command=command, **kwargs )
-
+    @classmethod
+    def isAvailable( self ):
+        return quietRun( 'which ovs-controller' ) or quietRun( 'which test-controller' )
 
 class NOX( Controller ):
     "Controller to run a NOX application."
@@ -1348,3 +1344,10 @@ def checkListening( self ):
         if 'Connected' not in listening:
             warn( "Unable to contact the remote controller"
                   " at %s:%d\n" % ( self.ip, self.port ) )
+
+
+def DefaultController( name, order=[ Controller, OVSController ], **kwargs ):
+    "find a default controller for mininet"
+    for controller in order:
+        if controller.isAvailable():
+            return controller( name, **kwargs )
-- 
GitLab