fargo 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/fargo/server.rb DELETED
@@ -1,52 +0,0 @@
1
- module Fargo
2
- class Server
3
-
4
- include Fargo::Publisher
5
-
6
- def initialize options = {}
7
- @options = options
8
- @options[:address] = '0.0.0.0'
9
- @peers = []
10
- end
11
-
12
- def connected?
13
- !@server.nil?
14
- end
15
-
16
- def connect
17
- return if connected?
18
-
19
- Fargo.logger.info "#{self}: Starting server on #{@options[:address]}:#{@options[:port]}"
20
-
21
- @server = TCPServer.new @options[:address], @options[:port]
22
-
23
- @active_thread = Thread.start { loop {
24
-
25
- connection = @options[:connection].new @options.merge(:first => false)
26
-
27
- connection_type = self.class.name.split("::").last.downcase
28
- disconnect_symbol = :"#{connection_type}_disconnected"
29
-
30
- connection.subscribe{ |type, hash|
31
- @peers.delete connection if type == disconnect_symbol
32
- }
33
-
34
- connection.socket = @server.accept
35
- connection.listen
36
- @peers << connection
37
- } }
38
- end
39
-
40
- def disconnect
41
- Fargo.logger.info "#{self}: disconnecting..."
42
- @active_thread.exit if @active_thread
43
-
44
- @server.close if @server rescue nil
45
- @server = nil
46
-
47
- @peers.each{ |p| p.disconnect }
48
- @peers.clear
49
- end
50
-
51
- end
52
- end