chatr 0.3.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -2
- data/bin/chatr_host +13 -9
- data/chatr_version +1 -1
- data/lib/chatr/host.rb +15 -16
- data/spec/lib/host_spec.rb +18 -6
- data/spec/spec_helper.rb +0 -4
- metadata +4 -6
data/README.md
CHANGED
@@ -1,2 +1,20 @@
|
|
1
|
-
chatr
|
2
|
-
|
1
|
+
# chatr
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/benwoody/chatr.png)](https://travis-ci.org/benwoody/chatr)
|
4
|
+
|
5
|
+
## Chatr Library
|
6
|
+
Chatr helps facilitate messages across socket connections. When a new Chatr::Host object is created, it creates a TCPServer to marshal messaging to and from the connecting clients.
|
7
|
+
|
8
|
+
## chatr_host
|
9
|
+
This binary sets up a Chatr host connection. Clients may then open connections to that hosts IP and Chatr port (default 4242)
|
10
|
+
|
11
|
+
Run host:
|
12
|
+
|
13
|
+
chatr_host 3333
|
14
|
+
|
15
|
+
Connect to host:
|
16
|
+
|
17
|
+
telnet 192.168.1.1 3333
|
18
|
+
|
19
|
+
The Host will accept the new connection and route incoming socket messages to the Host and other connected clients.
|
20
|
+
Clients are designated by their machines IP Address and the incoming Socket ID.
|
data/bin/chatr_host
CHANGED
@@ -1,32 +1,36 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
3
|
+
require_relative '../lib/chatr.rb'
|
4
4
|
|
5
5
|
# grab the first arguement from chatr_host
|
6
6
|
port = ARGV[0]
|
7
7
|
|
8
|
-
chatr = Chatr::Host.new
|
8
|
+
chatr = Chatr::Host.new(port=4242)
|
9
9
|
|
10
10
|
puts "Chat server initiated at #{chatr.local_ip} on port #{port}"
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
while true
|
16
|
-
session = select(connections,nil, nil,nil)
|
12
|
+
loop do
|
13
|
+
session = select(chatr.connections)
|
17
14
|
if session != nil
|
18
15
|
session[0].each do |socket|
|
19
|
-
if socket == host
|
16
|
+
if socket == chatr.host
|
20
17
|
chatr.accept_new_connection
|
18
|
+
new_socket = chatr.connections.last
|
19
|
+
join_msg = sprintf "Client #{new_socket.peeraddr[2]}:#{new_socket.peeraddr[1]} joined.\n"
|
20
|
+
chatr.broadcast_string(join_msg,new_socket)
|
21
21
|
else
|
22
22
|
input = socket.gets
|
23
23
|
if input.chomp == "QUIT"
|
24
|
+
quit_msg = sprintf "Client #{socket.peeraddr[2]}:#{socket.peeraddr[1]} disconnected\n"
|
25
|
+
chatr.broadcast_string(quit_msg,socket)
|
24
26
|
chatr.client_quit socket
|
25
27
|
else
|
26
|
-
|
28
|
+
output = sprintf "[#{socket.peeraddr[2]}|#{socket.peeraddr[1]}]: #{input}"
|
29
|
+
chatr.broadcast_string(output,socket)
|
27
30
|
end
|
28
31
|
end
|
29
32
|
end
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
36
|
+
|
data/chatr_version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.6
|
data/lib/chatr/host.rb
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
module Chatr
|
2
2
|
class Host
|
3
3
|
|
4
|
+
attr_reader :host, :connections
|
5
|
+
|
4
6
|
def initialize(port=4242)
|
5
7
|
@host = build_connection local_ip, port
|
6
8
|
@connections = grab_connections
|
7
9
|
end
|
8
10
|
|
9
11
|
# Find local ip
|
12
|
+
# This attempts an open connection to google.com and checks the local address used to attempt the request
|
10
13
|
def local_ip
|
11
|
-
|
14
|
+
UDPSocket.open {|s| s.connect("64.233.187.99", 1); s.addr.last}
|
12
15
|
end
|
13
16
|
|
14
17
|
# Start Socket connection
|
@@ -22,28 +25,24 @@ module Chatr
|
|
22
25
|
connections << @host
|
23
26
|
end
|
24
27
|
|
28
|
+
# Gives the number of connected clients
|
29
|
+
# Subtracts 1 to account for the host
|
30
|
+
def room_size
|
31
|
+
@connections.size - 1
|
32
|
+
end
|
33
|
+
|
25
34
|
# Take new socket from remote connection.
|
26
35
|
# This will write out new information to the client and output this to the host as well
|
27
36
|
def accept_new_connection
|
28
37
|
new_socket = @host.accept
|
29
|
-
@connections
|
30
|
-
|
31
|
-
str = sprintf "Client #{newsock.peeraddr[2]}:#{newsock.peeraddr[1]} joined.\n"
|
32
|
-
broadcast_string(str,newsock)
|
33
|
-
end
|
34
|
-
|
35
|
-
# Writes out the clients information and chat string to both host and connected clients
|
36
|
-
def write_out(string,sock)
|
37
|
-
str = sprintf "[#{sock.peeraddr[2]}|#{sock.peeraddr[1]}]: #{string}"
|
38
|
-
broadcast_string(str,sock)
|
38
|
+
@connections << new_socket
|
39
|
+
new_socket.write("Write QUIT to disconnect\n")
|
39
40
|
end
|
40
41
|
|
41
42
|
# Writes out when a client writes EOF and then outputs that information to host and clients
|
42
|
-
def client_quit(
|
43
|
-
|
44
|
-
|
45
|
-
sock.close
|
46
|
-
@connections.delete(sock)
|
43
|
+
def client_quit(socket)
|
44
|
+
@connections.delete(socket)
|
45
|
+
socket.close
|
47
46
|
end
|
48
47
|
|
49
48
|
# Print the string to the open connected sockets
|
data/spec/lib/host_spec.rb
CHANGED
@@ -7,23 +7,35 @@ describe 'Host' do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'should find local ip address' do
|
10
|
-
|
11
|
-
@chatr.local_ip.should
|
10
|
+
ip_regex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
|
11
|
+
@chatr.local_ip.should match ip_regex
|
12
12
|
end
|
13
13
|
|
14
|
-
it 'should build
|
14
|
+
it 'should build an Array of socket connections' do
|
15
|
+
@chatr.grab_connections.class.should be Array
|
15
16
|
end
|
16
17
|
|
17
|
-
it 'should
|
18
|
+
it 'should show the number of clients in chat' do
|
19
|
+
@chatr.room_size.class.should be Fixnum
|
18
20
|
end
|
19
21
|
|
20
|
-
|
22
|
+
# Tests that a TCPServer connection is built
|
23
|
+
it 'should build a Socket connection' do
|
24
|
+
@chatr.host.class.should be TCPServer
|
21
25
|
end
|
22
26
|
|
23
|
-
it 'should
|
27
|
+
it 'should accept new connections' do
|
28
|
+
client = TCPSocket.new(@chatr.local_ip,'4242')
|
29
|
+
@chatr.accept_new_connection
|
30
|
+
@chatr.room_size.should eq(1)
|
31
|
+
@chatr.client_quit @chatr.connections.last
|
24
32
|
end
|
25
33
|
|
26
34
|
it 'should close a connection when a client quits' do
|
35
|
+
client = TCPSocket.new(@chatr.local_ip,'4242')
|
36
|
+
@chatr.accept_new_connection
|
37
|
+
@chatr.client_quit @chatr.connections.last
|
38
|
+
@chatr.room_size.should eq(0)
|
27
39
|
end
|
28
40
|
|
29
41
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: This gem is for my final project for UW Ruby Certificate course
|
15
15
|
email: mail@benwoodall.com
|
@@ -52,11 +52,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
54
|
rubyforge_project:
|
55
|
-
rubygems_version: 1.8.
|
55
|
+
rubygems_version: 1.8.11
|
56
56
|
signing_key:
|
57
57
|
specification_version: 3
|
58
58
|
summary: Simple chat interface using Sockets
|
59
|
-
test_files:
|
60
|
-
- spec/lib/host_spec.rb
|
61
|
-
- spec/spec_helper.rb
|
59
|
+
test_files: []
|
62
60
|
has_rdoc:
|