-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_budget_chat.rb
executable file
·195 lines (167 loc) · 3.91 KB
/
3_budget_chat.rb
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env ruby --yjit
require 'async'
require 'async/queue'
require 'socket'
require_relative 'lib/config'
JOINED = [
"* %{username} has sauntered in",
"* %{username} has entered the room",
"* %{username} has joined, quick stop talking about them",
"* everybody welcome %{username}",
]
class ChatRoom
attr_reader :users
def initialize
@q = Async::Queue.new
@users = {}
end
def <<(msg) = @q << msg
def run
@q.each do |msg|
case msg
in [:subscribe, username, subscriber]
join_msg = format(JOINED.sample, username:)
@users.each { _2.(join_msg) }
subscriber.("* already here: #{@users.keys.join(", ")}")
@users[username] = subscriber
in [:send, username, message]
message = "[#{username}] #{message}"
@users.each { |u,cb| cb.(message) unless u == username }
in [:leave, username]
prev = @users.delete(username)
next unless prev
leave_msg = "* #{username} has left the room"
@users.each { |u,cb| cb.(leave_msg) }
end
end
end
end
def handle conn, room
conn.puts 'heyo. what should we call you?'
username = conn.gets&.chomp
unless username =~ /^[a-zA-Z0-9]{1,32}$/
conn.puts 'what kind of name is that? bye'
return
end
conn.got_username(username)
# TODO: race condition
if room.users.key?(username)
conn.puts "nuh uh, #{username} is already here!"
return
end
q = Async::Queue.new
cb = ->(msg) { q << [room, msg] }
room << [:subscribe, username, cb]
# read from socket in separate task
Async { gets_to_queue conn, q }
q.each do |from, msg|
case from
in ^room
conn.puts msg
in ^conn
break if !msg # connection closed
room << [:send, username, msg]
end
end
ensure
room << [:leave, username] if cb
conn.close
end
def gets_to_queue(sock, q)
loop do
msg = sock.gets
break if !msg
q << [sock, msg]
end
ensure
q << [sock, nil]
end
def server
Async do
room = ChatRoom.new
Async { room.run }
svr = TCPServer.new CONFIG['bind-port']
puts "listening on #{CONFIG['bind-port']}"
loop do
Async(svr.accept) do |_,sock|
conn = $mon.connection
conn.sock = sock
handle conn, room
end
end
end
end
Connection = Struct.new(:id,:username,:status,:messages,:sock) do
M = Struct.new(:message)
def initialize(*)
super
self.status = 'waiting for username'
self.messages = []
end
def puts(msg)
add_message msg
sock.puts msg
end
def gets
sock.gets.tap { add_message("> #{_1}") }
end
def add_message(msg)
self.messages << M[msg]
end
def message_count = messages.size
def got_username(username)
self.username = username
self.status = 'active'
end
def close
self.status = 'closed'
sock&.close
sock = nil
end
end
require 'glimmer-dsl-libui'
class Monitoring
include Glimmer
attr_accessor :conns, :dummy
def initialize
@lock = Mutex.new
@conns = []
@dummy = []
Glimmer::LibUI.timer(0.1) { @conn_table&.cell_rows = self.conns }
end
def sync(&) = @lock.synchronize(&)
def connection = sync { @conns[@conns.size] = Connection[@conns.size+1] }
def launch
window('Prime Time', 1200, 600) {
margined true
vertical_box {
@conn_table = table {
text_column 'id'
text_column 'username'
text_column 'message count'
text_column 'status'
cell_rows self.conns
on_selection_changed { |t,s|
if s
@messages_table.cell_rows = @conns[s].messages
else
@messages_table.cell_rows = []
end
}
}
@messages_table = table {
text_column 'message'
cell_rows <= [self, :dummy]
}
}
}.show
end
end
def monitor
$mon = Monitoring.new
$mon.launch
end
if $0 == __FILE__
Thread.new { server }
monitor
end