2004-02-14 05:28:02 +03:00
|
|
|
=begin
|
|
|
|
distributed Ruby --- chat server
|
2011-05-15 15:55:52 +04:00
|
|
|
Copyright (c) 1999-2000 Masatoshi SEKI
|
2004-02-14 05:28:02 +03:00
|
|
|
=end
|
|
|
|
require 'drb/drb'
|
|
|
|
|
|
|
|
class ChatEntry
|
|
|
|
include DRbUndumped
|
|
|
|
|
|
|
|
def initialize(server, there)
|
|
|
|
@server = server
|
|
|
|
@there = there
|
|
|
|
@name = there.name
|
|
|
|
@key = there.key = Time.now
|
|
|
|
end
|
|
|
|
attr :name, true
|
|
|
|
attr :there
|
|
|
|
|
|
|
|
def say(str)
|
|
|
|
@server.distribute(@there, str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def listen(str)
|
|
|
|
@there.message(@key, str)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class ChatServer
|
|
|
|
def initialize
|
|
|
|
@mutex = Mutex.new
|
|
|
|
@members = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_member(there)
|
|
|
|
client = ChatEntry.new(self, there)
|
|
|
|
@mutex.synchronize do
|
|
|
|
@members[there] = client
|
|
|
|
end
|
|
|
|
client
|
|
|
|
end
|
|
|
|
|
|
|
|
def distribute(there, str)
|
|
|
|
name = @members[there].name
|
|
|
|
msg = "<#{name}> #{str}"
|
|
|
|
msg2 = ">#{name}< #{str}"
|
|
|
|
@mutex.synchronize do
|
|
|
|
for m in @members.keys
|
2013-07-20 17:41:02 +04:00
|
|
|
begin
|
|
|
|
if m == there
|
|
|
|
@members[m].listen(msg2)
|
|
|
|
else
|
|
|
|
@members[m].listen(msg)
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
p $!
|
|
|
|
@members.delete(m)
|
|
|
|
end
|
2004-02-14 05:28:02 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
here = ARGV.shift
|
|
|
|
DRb.start_service(here, ChatServer.new)
|
|
|
|
puts DRb.uri
|
2004-06-27 19:36:32 +04:00
|
|
|
DRb.thread.join
|
2004-02-14 05:28:02 +03:00
|
|
|
end
|