Adam Lindsay avatar Adam Lindsay committed 3206a00

Simplification and service_type differentiation through find_matching_service()

Comments (0)

Files changed (6)

 
 DEFAULTPORT = 9900
 WAITPERIOD = 0.01
+SERVICENAME = "Locator"
+
 usage = '''
   python %prog [options]
   
             loc.port += 1
     raise NodeNotFound(loc)
 
+def find_matching_service(location, service, maximum=10):
+    loc = Location(location.address, location.port)
+    for a in range(maximum):
+        try:
+            if service == remote_call(loc, 'service_type'):
+                return loc
+        except NodeNotFound:
+            pass
+        loc.port += 1
+    print 'No peer autodiscovered.'
+    return None
+
 def ping_until_not_found(location, maximum=10):
     loc = Location(location.address, location.port)
     for a in range(maximum):
     
     @classmethod
     def service_type(cls):
-        return "Locator"
+        return SERVICENAME
     
     @classmethod
     def service_types(cls):
     if options.peer:
         options.peer = str2loc(options.peer)
     else:
-        try:
-            options.peer = ping_until_found(Location('localhost', DEFAULTPORT))
-        except NodeNotFound:
-            print 'No peer autodiscovered.'
+        options.peer = find_matching_service(Location('localhost', DEFAULTPORT), SERVICENAME)
     main(options.__dict__)
 
 
 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT, parser
-from location import ping_until_found, str2loc, NodeNotFound
+from storeserver import remote_call, parser, DEFAULTPORT, SERVICENAME
+from location import find_matching_service, str2loc, NodeNotFound
 
 usage = '''
   python %prog [options] <key>
     if options.peer:
         loc = str2loc(options.peer)
     else:
-        try:
-            loc = ping_until_found(Location('localhost', DEFAULTPORT))
-        except NodeNotFound:
-            print 'No peer autodiscovered.'
-            sys.exit()
+        loc = find_matching_service(Location('localhost', DEFAULTPORT), SERVICENAME) or sys.exit()
     print remote_call(loc, 'get', key)
 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT, parser
-from location import ping_until_found, str2loc, NodeNotFound
+from storeserver import remote_call, parser, DEFAULTPORT, SERVICENAME
+from location import find_matching_service, str2loc, NodeNotFound
 
 usage = '''
   python %prog
     if options.peer:
         loc = str2loc(options.peer)
     else:
-        try:
-            loc = ping_until_found(Location('localhost', DEFAULTPORT))
-        except NodeNotFound:
-            print 'No peer autodiscovered.'
-            sys.exit()
+        loc = find_matching_service(Location('localhost', DEFAULTPORT), SERVICENAME) or sys.exit()
     for key, value in DICTIONARY.items():
         remote_call(loc, 'put', key, value)
 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT, parser
-from location import ping_until_found, str2loc, NodeNotFound
+from storeserver import remote_call, parser, DEFAULTPORT, SERVICENAME
+from location import find_matching_service, str2loc, NodeNotFound
 
 usage = '''
   python %prog [options] <key> <value>
     if options.peer:
         loc = str2loc(options.peer)
     else:
-        try:
-            loc = ping_until_found(Location('localhost', DEFAULTPORT))
-        except NodeNotFound:
-            print 'No peer autodiscovered.'
-            sys.exit()
+        loc = find_matching_service(Location('localhost', DEFAULTPORT), SERVICENAME) or sys.exit()
     remote_call(loc, 'put', key, value)
 
 DEFAULTPORT = 9900
 WAITPERIOD = 0.01
+SERVICENAME = "Diststore"
 
 usage = '''
   python %prog [options]
     
     @classmethod
     def service_type(cls):
-        return "Diststore"
+        return SERVICENAME
     
     def get(self, key):
         """
     if options.peer:
         options.peer = location.str2loc(options.peer)
     else:
-        try:
-            options.peer = location.ping_until_found(Location('localhost', DEFAULTPORT))
-        except location.NodeNotFound:
-            print 'No peer autodiscovered.'
+        options.peer = location.find_matching_service(Location('localhost', DEFAULTPORT), SERVICENAME)
     main(options.__dict__)
 
 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT, parser
-from location import ping_until_found, str2loc, NodeNotFound
+from storeserver import remote_call, parser, DEFAULTPORT, SERVICENAME
+from location import find_matching_service, str2loc, NodeNotFound
 
 usage = '''
   python %prog
     if options.peer:
         loc = str2loc(options.peer)
     else:
-        try:
-            loc = ping_until_found(Location('localhost', DEFAULTPORT))
-        except NodeNotFound:
-            print 'No peer autodiscovered.'
-            sys.exit()
+        loc = find_matching_service(Location('localhost', DEFAULTPORT), SERVICENAME) or sys.exit()
     for key in KEYS:
         value = remote_call(loc, 'get', key)
         if value:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.