sappho-socket 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
|
|
1
|
+
# See https://github.com/sappho/sappho-heatmiser-proxy/wiki for project documentation.
|
2
|
+
# This software is licensed under the GNU Affero General Public License, version 3.
|
3
|
+
# See http://www.gnu.org/licenses/agpl.html for full details of the license terms.
|
4
|
+
# Copyright 2012 Andrew Heald.
|
5
|
+
|
6
|
+
module Sappho
|
7
|
+
module Socket
|
8
|
+
|
9
|
+
require 'singleton'
|
10
|
+
require 'thread'
|
11
|
+
require 'logger'
|
12
|
+
|
13
|
+
class AutoFlushLog
|
14
|
+
|
15
|
+
include Singleton
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@mutex = Mutex.new
|
19
|
+
@log = Logger.new $stdout
|
20
|
+
@log.level = ENV['application.log.level'] == 'debug' ? Logger::DEBUG : Logger::INFO
|
21
|
+
end
|
22
|
+
|
23
|
+
def info message
|
24
|
+
@mutex.synchronize do
|
25
|
+
@log.info message
|
26
|
+
$stdout.flush
|
27
|
+
end if @log.info?
|
28
|
+
end
|
29
|
+
|
30
|
+
def debug message
|
31
|
+
@mutex.synchronize do
|
32
|
+
@log.debug message
|
33
|
+
$stdout.flush
|
34
|
+
end if @log.debug?
|
35
|
+
end
|
36
|
+
|
37
|
+
def error error
|
38
|
+
@mutex.synchronize do
|
39
|
+
@log.error "error! #{error.message}"
|
40
|
+
error.backtrace.each { |error| @log.error error }
|
41
|
+
$stdout.flush
|
42
|
+
end if @log.error?
|
43
|
+
end
|
44
|
+
|
45
|
+
def debug?
|
46
|
+
@log.debug?
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
module LogUtilities
|
52
|
+
|
53
|
+
def hexString bytes
|
54
|
+
(bytes.collect {|byte| "%02x " % (byte & 0xFF)}).join
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# See https://github.com/sappho/sappho-socket/wiki for project documentation.
|
2
|
+
# This software is licensed under the GNU Affero General Public License, version 3.
|
3
|
+
# See http://www.gnu.org/licenses/agpl.html for full details of the license terms.
|
4
|
+
# Copyright 2012 Andrew Heald.
|
5
|
+
|
6
|
+
module Sappho
|
7
|
+
module Socket
|
8
|
+
|
9
|
+
require 'sappho-socket/auto_flush_log'
|
10
|
+
require 'thread'
|
11
|
+
require 'socket'
|
12
|
+
|
13
|
+
class SafeServer
|
14
|
+
|
15
|
+
def initialize name, port, maxClients = 10
|
16
|
+
@name = name
|
17
|
+
@port = port
|
18
|
+
@maxClients = maxClients
|
19
|
+
@clients = {}
|
20
|
+
@mutex = Mutex.new
|
21
|
+
@log = AutoFlushLog.instance
|
22
|
+
end
|
23
|
+
|
24
|
+
def serve
|
25
|
+
Thread.new do
|
26
|
+
begin
|
27
|
+
@log.info "opening #{@name} server port #{@port}"
|
28
|
+
@server = TCPServer.open @port
|
29
|
+
@log.info "#{@name} server port #{@port} is now open"
|
30
|
+
clientCount = 0
|
31
|
+
loop do
|
32
|
+
@mutex.synchronize do
|
33
|
+
clientCount = @clients.size
|
34
|
+
end
|
35
|
+
if clientCount >= @maxClients
|
36
|
+
sleep 1
|
37
|
+
else
|
38
|
+
@log.info "listening for new clients on #{@name} server port #{@port}"
|
39
|
+
client = @server.accept
|
40
|
+
ip = client.getpeername
|
41
|
+
ip = (4 ... 8).map{|pos|ip[pos]}.join('.')
|
42
|
+
@mutex.synchronize do
|
43
|
+
@clients[client] = ip
|
44
|
+
log ip, 'connected'
|
45
|
+
end
|
46
|
+
Thread.new client, ip do | client, ip |
|
47
|
+
socket = SafeSocket.new 30
|
48
|
+
socket.attach client
|
49
|
+
begin
|
50
|
+
yield socket, ip, @name, @port
|
51
|
+
rescue => error
|
52
|
+
@log.error error
|
53
|
+
end
|
54
|
+
socket.close
|
55
|
+
@mutex.synchronize do
|
56
|
+
@clients.delete client
|
57
|
+
log ip, 'disconnected'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def log ip, status
|
69
|
+
@log.info "client #{ip} #{status}"
|
70
|
+
@log.info "clients: #{@clients.size > 0 ? (@clients.collect{|client, ip| ip}).join(', ') : 'none'}"
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -3,13 +3,13 @@
|
|
3
3
|
# See http://www.gnu.org/licenses/agpl.html for full details of the license terms.
|
4
4
|
# Copyright 2012 Andrew Heald.
|
5
5
|
|
6
|
-
require 'timeout'
|
7
|
-
require 'socket'
|
8
|
-
require 'sappho-socket/connected_socket'
|
9
|
-
|
10
6
|
module Sappho
|
11
7
|
module Socket
|
12
8
|
|
9
|
+
require 'timeout'
|
10
|
+
require 'socket'
|
11
|
+
require 'sappho-socket/connected_socket'
|
12
|
+
|
13
13
|
class SafeSocket
|
14
14
|
|
15
15
|
def SafeSocket.mock session, timeout = 10
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sappho-socket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Heald
|
@@ -45,7 +45,9 @@ extra_rdoc_files: []
|
|
45
45
|
|
46
46
|
files:
|
47
47
|
- lib/sappho-socket/version.rb
|
48
|
+
- lib/sappho-socket/auto_flush_log.rb
|
48
49
|
- lib/sappho-socket/connected_socket.rb
|
50
|
+
- lib/sappho-socket/safe_server.rb
|
49
51
|
- lib/sappho-socket/mock_socket.rb
|
50
52
|
- lib/sappho-socket/safe_socket.rb
|
51
53
|
- test/ruby/socket_test.rb
|