Created
June 8, 2012 12:02
-
-
Save MagerValp/2895285 to your computer and use it in GitHub Desktop.
Cocoa Distributed Objects in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import optparse | |
from Foundation import * | |
SRV_NAME = u"HelloServer" | |
def main(argv): | |
p = optparse.OptionParser() | |
p.set_usage("""Usage: %prog [options] name""") | |
p.add_option("-v", "--verbose", action="store_true", | |
help="Verbose output.") | |
options, argv = p.parse_args(argv) | |
if len(argv) != 2: | |
print >>sys.stderr, p.get_usage() | |
return 1 | |
name = argv[1].decode("utf-8") | |
srv_proxy = NSConnection.rootProxyForConnectionWithRegisteredName_host_(SRV_NAME, None) | |
if not srv_proxy: | |
sys.exit(u"Couldn't establish connection with '%s'" % SRV_NAME) | |
print u"Server says: '%s'" % srv_proxy.hello(name) | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import optparse | |
from Foundation import * | |
SRV_NAME = u"HelloServer" | |
class MyService(NSObject): | |
keep_running = True | |
def hello(self, name): | |
if name == u"quit": | |
self.keep_running = False | |
return u"Quitting" | |
else: | |
print u"Client says: hello(%s)" % repr(name) | |
return u"Hello, %s!" % name | |
def main(argv): | |
p = optparse.OptionParser() | |
p.set_usage("""Usage: %prog [options]""") | |
p.add_option("-v", "--verbose", action="store_true", | |
help="Verbose output.") | |
options, argv = p.parse_args(argv) | |
if len(argv) != 1: | |
print >>sys.stderr, p.get_usage() | |
return 1 | |
conn = NSConnection.defaultConnection() | |
myservice = MyService.alloc().init() | |
conn.setRootObject_(myservice) | |
if conn.registerName_(SRV_NAME) == False: | |
sys.exit(u"Unable to register '%s'" % SRV_NAME) | |
runloop = NSRunLoop.currentRunLoop() | |
while myservice.keep_running and runloop.runMode_beforeDate_(NSDefaultRunLoopMode, NSDate.distantFuture()): | |
pass | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment