-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDRuby_server.rb
More file actions
48 lines (42 loc) · 1004 Bytes
/
DRuby_server.rb
File metadata and controls
48 lines (42 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'drb/drb'
require 'ostruct'
class Connection
include DRbUndumped
attr_reader :name
def initialize(server,remote)
@remote=remote
@server=server
@name=remote.name
end
def to_server(msg)
@server.message(@remote,msg)
end
def to_client(msg)
@remote.message(msg)
end
end
class Server
attr_accessor :connections
def initialize
@connections=[]
end
def add_client(remote)
puts "new client"
c=Connection.new(self,remote)
@connections << c
c.to_client("users=>"+ (@connections.length != 0 ? usernames : "NO USER LOOGED IN ") )
c.to_client("welcome #{remote.name} from server")
return c
end
def usernames
@connections.map {|user| user.name}.join("\n")
end
def message(remote,msg)
msg="#{remote.name} : #{msg}"
connections.each do |connection|
connection.to_client(msg) if connection.name != remote.name
end
end
end
DRb.start_service("druby://localhost:4200",Server.new)
DRb.thread.join