chatr 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ - ruby-head
8
+ - jruby-head
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ gem 'rake'
5
+ gem 'rspec'
6
+ gem 'yard'
File without changes
@@ -0,0 +1,2 @@
1
+ chatr
2
+ =====
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ task default: :spec
4
+
5
+ desc "Run Specs"
6
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'chatr'
4
+
5
+ # grab the first arguement from chatr_host
6
+ port = ARGV[0]
7
+
8
+ chatr = Chatr::Host.new port
9
+
10
+ puts "Chat server initiated at #{chatr.local_ip} on port #{port}"
11
+
12
+ host = chatr.instance_variable_get(:@host)
13
+ connections = chatr.instance_variable_get(:@connections)
14
+
15
+ while true
16
+ session = select(connections,nil, nil,nil)
17
+ if session != nil
18
+ session[0].each do |socket|
19
+ if socket == host
20
+ chatr.accept_new_connection
21
+ else
22
+ input = socket.gets
23
+ if input.chomp == "QUIT"
24
+ chatr.client_quit socket
25
+ else
26
+ chatr.write_out input, socket
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,21 @@
1
+ version = File.read(File.expand_path("../chatr_version",__FILE__)).strip
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'chatr'
5
+ s.version = version
6
+
7
+ s.summary = "Simple chat interface using Sockets"
8
+ s.description = "This gem is for my final project for UW Ruby Certificate course"
9
+
10
+ s.authors = ["Ben Woodall"]
11
+ s.email = 'mail@benwoodall.com'
12
+ s.homepage = 'http://github.com/benwoody/chatr'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.require_paths = ["lib"]
18
+
19
+ s.extra_rdoc_files = ["README.md"]
20
+
21
+ end
@@ -0,0 +1 @@
1
+ 0.3.5
@@ -0,0 +1,7 @@
1
+ require 'socket'
2
+
3
+ module Chatr
4
+
5
+ require 'chatr/host'
6
+
7
+ end
@@ -0,0 +1,60 @@
1
+ module Chatr
2
+ class Host
3
+
4
+ def initialize(port=4242)
5
+ @host = build_connection local_ip, port
6
+ @connections = grab_connections
7
+ end
8
+
9
+ # Find local ip
10
+ def local_ip
11
+ Socket.ip_address_list.last.ip_address
12
+ end
13
+
14
+ # Start Socket connection
15
+ def build_connection(ip,port)
16
+ TCPServer.new(ip,port)
17
+ end
18
+
19
+ # Builds an Array to store connections, adds current socket in 0
20
+ def grab_connections
21
+ connections = []
22
+ connections << @host
23
+ end
24
+
25
+ # Take new socket from remote connection.
26
+ # This will write out new information to the client and output this to the host as well
27
+ def accept_new_connection
28
+ new_socket = @host.accept
29
+ @connections.push(new_socket)
30
+ newsock.write("Write QUIT to disconnect\n")
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)
39
+ end
40
+
41
+ # Writes out when a client writes EOF and then outputs that information to host and clients
42
+ def client_quit(sock)
43
+ str = sprintf "Client #{sock.peeraddr[2]}:#{sock.peeraddr[1]} disconnected\n"
44
+ broadcast_string(str,sock)
45
+ sock.close
46
+ @connections.delete(sock)
47
+ end
48
+
49
+ # Print the string to the open connected sockets
50
+ def broadcast_string(string,origin)
51
+ @connections.each do |client|
52
+ if client != @host && client != origin
53
+ client.write(string)
54
+ end
55
+ end
56
+ print(string)
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Host' do
4
+
5
+ before :all do
6
+ @chatr = Chatr::Host.new
7
+ end
8
+
9
+ it 'should find local ip address' do
10
+ ip_from_google = UDPSocket.open {|s| s.connect("64.233.187.99", 1); s.addr.last}
11
+ @chatr.local_ip.should == ip_from_google
12
+ end
13
+
14
+ it 'should build a Socket connection' do
15
+ end
16
+
17
+ it 'should accept new connections' do
18
+ end
19
+
20
+ it 'should broadcast information accross connections' do
21
+ end
22
+
23
+ it 'should write chat lines accross connections' do
24
+ end
25
+
26
+ it 'should close a connection when a client quits' do
27
+ end
28
+
29
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ RSpec.configure do |config|
5
+ config.order = 'random'
6
+ end
7
+
8
+ require File.expand_path("../../lib/chatr", __FILE__)
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chatr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Woodall
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This gem is for my final project for UW Ruby Certificate course
15
+ email: mail@benwoodall.com
16
+ executables:
17
+ - chatr_host
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.md
21
+ files:
22
+ - .gitignore
23
+ - .travis.yml
24
+ - Gemfile
25
+ - Manifest.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/chatr_host
29
+ - chatr.gemspec
30
+ - chatr_version
31
+ - lib/chatr.rb
32
+ - lib/chatr/host.rb
33
+ - spec/lib/host_spec.rb
34
+ - spec/spec_helper.rb
35
+ homepage: http://github.com/benwoody/chatr
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.21
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Simple chat interface using Sockets
59
+ test_files:
60
+ - spec/lib/host_spec.rb
61
+ - spec/spec_helper.rb
62
+ has_rdoc: