Skip to content

Instantly share code, notes, and snippets.

@spllr
Created February 7, 2011 22:21
Show Gist options
  • Save spllr/815368 to your computer and use it in GitHub Desktop.
Save spllr/815368 to your computer and use it in GitHub Desktop.
Little POC for Distributed Object in MacRuby
#
# MacRuby distributed objects poc.
#
# Inspired by the Distributed Object chapter in "Advanced Mac Os X Programming" by Dalrymple and Hillegass
#
# Klaas Speller
# http://www.github.com/spllr
#
# Use at own risk (that's what the lisence is for).
# Its still buggy and I would love some input on good usage of the NSRunLoop with MacRuby
# An actual implementation would not run from two threads like this.
#
# And yes, I know about the puts from threads :-)
#
# ######################
#
# Copyright (C) 2011 by Klaas Speller
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
#
framework "Foundation"
module Chatter
class Server
class Monitor
def connection(ancestor, shouldMakeNewConnection: conn)
puts "creating new connection"
true
end
def connectionDidDie(note)
puts "connection did die"
end
end
def initialize
@clients = []
end
def subscribe(client)
puts "Adding client <#{client}>"
say("SERVER", "#{client} joined in on the fun")
@clients << client
true
end
def say(who, what)
@clients.each { |c| c.notify("<#{who}> #{what}") }
end
end
class Client
def initialize(name)
@name = name
end
def notify(msg)
puts "[#{@name}] #{msg}"
end
def to_s
@name
end
end
end
port = 8080
# server thread
Thread.new do
# setup the server
server = Chatter::Server.alloc.init
monitor = Chatter::Server::Monitor.alloc.init
# get a socket
begin
receive_port = NSSocketPort.alloc.initWithTCPPort(port)
rescue
puts "unable to get port #{port}"
exit(-1)
end
# setup the connection
connection = NSConnection.connectionWithReceivePort(receive_port, sendPort: nil)
# vend the server
connection.setRootObject(server)
# setup notification
connection.setDelegate(monitor)
NSNotificationCenter.defaultCenter.addObserver(monitor,
selector: :connectionDidDie,
name: NSConnectionDidDieNotification,
object: nil)
puts "server listening on #{port}"
NSRunLoop.currentRunLoop.run
end
# client thread
["Pipo", "John", "Ellen"].each_with_index do |name, i|
Thread.new do
sleep 1 + i # wait a bit
# get the remote port
send_port = NSSocketPort.alloc.initRemoteWithTCPPort(port, host: "127.0.0.1")
# serup the connection
connection = NSConnection.connectionWithReceivePort(nil, sendPort: send_port)
connection.setRequestTimeout(10.0)
connection.setReplyTimeout(10.0)
# setup client
client = Chatter::Client.new(name)
begin
# setup the connection
proxy = connection.rootProxy
if proxy.subscribe(client)
proxy.say(client, "Hi, my name is #{client}")
else
puts "Could not register client"
exit(-1)
end
rescue
puts "something went bad"
exit(-1)
end
NSRunLoop.currentRunLoop.run
end
end
NSRunLoop.currentRunLoop.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment