carnivore-unixsocket 0.1.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0690f9cf11c543bfc6f51202a972f3b6852228b
4
- data.tar.gz: 0b450308e838d001f603b90ea15f1b491a6b1506
3
+ metadata.gz: ac593dce0b201d4ef9408cdf9ae84a1384a974c3
4
+ data.tar.gz: b739dd496994e3877119dbe855ac662b85b65071
5
5
  SHA512:
6
- metadata.gz: 504228019293064bd168444a5018c73c09efd6e8077c1b438eac9bb30c479095f2949bf36a609d4d904dd5067c2e32503e1b1e06cad7289a0aecffc7ae1bc53d
7
- data.tar.gz: 4559e2340b4861cb4e133dc880857241eb61b038bd7603c86a1af7ea0f5331e686414a23bb6746fbf57700ed386bd5b152888f2e7ff5dc7b3c63a1296cd4cff0
6
+ metadata.gz: 75adbc8989a62869040b2f488cda19013d5102088a5190e4dc0464d4835c00c9195ce68778e1957a74db2312408eacca1958a04a3e866814fe3ec5caa1fbd2e0
7
+ data.tar.gz: bbf897e05ba4e2ffa71563c3e663e453f061f0f4b1dbeaf5d99d3219bdd56b6292bc238acff7cce1269862834bf4f5fc6cb810359c3d4876ab47bc6735424df7
data/CHANGELOG.md CHANGED
@@ -1,2 +1,5 @@
1
+ # v0.2.0
2
+ * Remove celluloid dependency
3
+
1
4
  # v0.1.0
2
5
  * Initial release
data/README.md CHANGED
@@ -10,7 +10,7 @@ require 'carnivore-unixsocket'
10
10
 
11
11
  Carnivore.configure do
12
12
  source = Carnivore::Source.build(
13
- :type => :socket, :args => {:path => '/var/run/my.sock'}
13
+ :type => :unix_socket, :args => {:path => '/var/run/my.sock'}
14
14
  )
15
15
  end
16
16
  ```
@@ -10,7 +10,6 @@ Gem::Specification.new do |s|
10
10
  s.description = 'Carnivore Unix Socket Source'
11
11
  s.license = 'Apache 2.0'
12
12
  s.require_path = 'lib'
13
- s.add_dependency 'carnivore', '>= 0.1.8'
14
- s.add_dependency 'celluloid-io'
15
- s.files = Dir['**/*']
13
+ s.add_dependency 'carnivore', '>= 1.0'
14
+ s.files = Dir['lib/**/*'] + %w(carnivore-unixsocket.gemspec README.md CHANGELOG.md)
16
15
  end
@@ -5,8 +5,7 @@ module Carnivore
5
5
  # Unix socket based carnivore source
6
6
  class UnixSocket < Source
7
7
 
8
- # max time for unix socket to setup
9
- INIT_SRV_TIMEOUT = 2.0
8
+ option :cache_signals
10
9
 
11
10
  # @return [String] path to socket
12
11
  attr_reader :socket
@@ -19,6 +18,7 @@ module Carnivore
19
18
  #
20
19
  # @param args [Hash]
21
20
  def setup(args={})
21
+ @write_lock = Mutex.new
22
22
  @socket = ::File.expand_path(args[:path])
23
23
  @srv_name = "socket_srv_#{name}".to_sym
24
24
  @connection = nil
@@ -27,40 +27,34 @@ module Carnivore
27
27
 
28
28
  # @return [Util::Server]
29
29
  def server
30
- callback_supervisor[srv_name]
30
+ @server
31
31
  end
32
32
 
33
33
  # @return [Celluloid::IO::UNIXSocket]
34
34
  def connection
35
35
  unless(@connection)
36
- @connection = Celluloid::IO::UNIXSocket.new(socket)
36
+ @connection = UNIXSocket.new(socket)
37
37
  end
38
38
  @connection
39
39
  end
40
40
 
41
41
  # Initialize the server
42
42
  def init_srv
43
- callback_supervisor.supervise_as(srv_name,
44
- Carnivore::UnixSocket::Util::Server,
45
- init_args.merge(:notify_actor => current_actor)
43
+ @server = Util::Server.new(
44
+ :path => socket,
45
+ :source => current_self
46
46
  )
47
- waited = 0.0
48
- until(server || waited > INIT_SRV_TIMEOUT)
49
- sleep(0.01)
50
- waited += 0.01
51
- end
52
- server.async.start
47
+ @server.async.start_collector!
48
+ @server.async.start_server!
53
49
  end
54
50
 
55
51
  # Receive messages
56
52
  def receive(*args)
57
- wait(:new_socket_lines)
58
- server.return_lines.map do |line|
59
- begin
60
- MultiJson.load(line)
61
- rescue MultiJson::ParseError
62
- line
63
- end
53
+ line = wait(:message)
54
+ begin
55
+ MultiJson.load(line)
56
+ rescue MultiJson::ParseError
57
+ line
64
58
  end
65
59
  end
66
60
 
@@ -70,8 +64,12 @@ module Carnivore
70
64
  # @param original_message [Carnivore::Message]
71
65
  def transmit(payload, original_message=nil)
72
66
  output = payload.is_a?(String) ? payload : MultiJson.dump(payload)
73
- connection.puts(output)
74
- connection.flush
67
+ @write_lock.synchronize do
68
+ defer do
69
+ connection.puts(output)
70
+ connection.flush
71
+ end
72
+ end
75
73
  end
76
74
 
77
75
  # Override processing to enable server only if required
@@ -1,60 +1,97 @@
1
+ require 'socket'
1
2
  require 'carnivore-unixsocket'
2
- require 'celluloid/io'
3
3
 
4
4
  module Carnivore
5
5
  module UnixSocket
6
6
  module Util
7
7
  class Server
8
8
 
9
- include Celluloid
9
+ include Zoidberg::SoftShell
10
+ include Zoidberg::Supervise
10
11
  include Carnivore::Utils::Logging
11
12
 
12
- attr_reader :path, :notify_actor, :server, :supervisor
13
+ # @return [String] socket path
14
+ attr_reader :path
15
+ # @return [UNIXServer]
16
+ attr_reader :server
17
+ # @return [Source]
18
+ attr_reader :source
19
+ # @return [IO]
20
+ attr_reader :waker
13
21
 
22
+ # Create a new server
23
+ #
24
+ # @param args [Hash]
25
+ # @option args [String] :path socket path
26
+ # @return [self]
14
27
  def initialize(args={})
15
28
  @path = ::File.expand_path(args[:path])
16
- @notify_actor = args[:notify_actor]
17
- @supervisor = Celluloid::SupervisionGroup.run!
18
- @messages = []
29
+ @source = args[:source]
30
+ @connections = []
31
+ @waker = IO.pipe.last
19
32
  end
20
33
 
21
- def start
22
- srv_actor = current_actor
23
- defer do
24
- loop do
25
- setup_server!
26
- debug 'Waiting for new connection to server'
27
- connection = server.accept
28
- debug 'Received new connection to server. Setting up connection...'
29
- supervisor.supervise_as("con_#{connection.hash}", Connection,
30
- :io => connection, :server => srv_actor, :auto_consume => true
31
- )
32
- debug 'Connection setup complete and active'
34
+ # Add a new connection
35
+ #
36
+ # @param con [UNIXSocket]
37
+ # @return [NilClass]
38
+ def add_connection(con)
39
+ @connections.push(con)
40
+ waker.write '-'
41
+ nil
42
+ end
43
+
44
+ # Remove a connection
45
+ #
46
+ # @param con [UNIXSocket]
47
+ # @return [NilClass]
48
+ def remove_connection(con)
49
+ @connections.delete(con)
50
+ waker.write '-'
51
+ nil
52
+ end
53
+
54
+ # Start the server listener loop
55
+ def start_server!
56
+ setup_server!
57
+ loop do
58
+ debug 'Waiting for new connection to socket'
59
+ connection = server.accept
60
+ debug 'Received new connection to socket, loading in...'
61
+ add_connection(connection)
62
+ end
63
+ end
64
+
65
+ # Start the message collector loop
66
+ def start_collector!
67
+ loop do
68
+ IO.select(current_self.connections + [waker]).flatten.compact.each do |sock|
69
+ if(sock == waker)
70
+ sock.read
71
+ else
72
+ line = sock.gets
73
+ if(line)
74
+ source.signal(:message, line.strip)
75
+ else
76
+ sock.close
77
+ end
78
+ end
33
79
  end
34
80
  end
35
81
  end
36
82
 
83
+ # Setup the path for the server and create new server
84
+ #
85
+ # @return [UNIXServer]
37
86
  def setup_server!
38
87
  unless(@server)
39
88
  if(::File.exists?(path))
40
89
  ::File.delete(path)
41
90
  end
42
- @server = Celluloid::IO::UNIXServer.new(path)
91
+ @server = UNIXServer.new(path)
43
92
  end
44
93
  end
45
94
 
46
- def add_lines(lines)
47
- lines = [lines] unless lines.is_a?(Array)
48
- @messages += lines
49
- notify_actor.signal(:new_socket_lines)
50
- end
51
-
52
- def return_lines
53
- msgs = @messages.dup
54
- @messages.clear
55
- msgs
56
- end
57
-
58
95
  end
59
96
  end
60
97
  end
@@ -3,10 +3,7 @@ require 'carnivore-unixsocket'
3
3
  module Carnivore
4
4
  module UnixSocket
5
5
  module Util
6
-
7
- autoload :Connection, 'carnivore-unixsocket/util/socket_connection'
8
6
  autoload :Server, 'carnivore-unixsocket/util/socket_server'
9
-
10
7
  end
11
8
  end
12
9
  end
@@ -1,6 +1,6 @@
1
1
  module Carnivore
2
2
  module UnixSocket
3
3
  # current library version
4
- VERSION = Gem::Version.new('0.1.0')
4
+ VERSION = Gem::Version.new('0.2.0')
5
5
  end
6
6
  end
@@ -1,4 +1,3 @@
1
- require 'celluloid/io'
2
1
  require 'carnivore'
3
2
 
4
3
  module Carnivore
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carnivore-unixsocket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-14 00:00:00.000000000 Z
11
+ date: 2017-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carnivore
@@ -16,28 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.8
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.8
27
- - !ruby/object:Gem::Dependency
28
- name: celluloid-io
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
26
+ version: '1.0'
41
27
  description: Carnivore Unix Socket Source
42
28
  email: chrisroberts.code@gmail.com
43
29
  executables: []
@@ -45,14 +31,11 @@ extensions: []
45
31
  extra_rdoc_files: []
46
32
  files:
47
33
  - CHANGELOG.md
48
- - CONTRIBUTING.md
49
- - LICENSE
50
34
  - README.md
51
35
  - carnivore-unixsocket.gemspec
52
36
  - lib/carnivore-unixsocket.rb
53
37
  - lib/carnivore-unixsocket/unixsocket.rb
54
38
  - lib/carnivore-unixsocket/util.rb
55
- - lib/carnivore-unixsocket/util/socket_connection.rb
56
39
  - lib/carnivore-unixsocket/util/socket_server.rb
57
40
  - lib/carnivore-unixsocket/version.rb
58
41
  homepage: https://github.com/carnivore-rb/carnivore-unixsocket
@@ -75,9 +58,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
58
  version: '0'
76
59
  requirements: []
77
60
  rubyforge_project:
78
- rubygems_version: 2.2.2
61
+ rubygems_version: 2.4.8
79
62
  signing_key:
80
63
  specification_version: 4
81
64
  summary: Message processing helper
82
65
  test_files: []
83
- has_rdoc:
data/CONTRIBUTING.md DELETED
@@ -1,25 +0,0 @@
1
- # Contributing
2
-
3
- ## Branches
4
-
5
- ### `master` branch
6
-
7
- The master branch is the current stable released version.
8
-
9
- ### `develop` branch
10
-
11
- The develop branch is the current edge of development.
12
-
13
- ## Pull requests
14
-
15
- * https://github.com/carnivore-rb/carnivore-unixsocket/pulls
16
-
17
- Please base all pull requests of the `develop` branch. Merges to
18
- `master` only occur through the `develop` branch. Pull requests
19
- based on `master` will likely be cherry picked.
20
-
21
- ## Issues
22
-
23
- Need to report an issue? Use the github issues:
24
-
25
- * https://github.com/carnivore-rb/carnivore-unixsocket/issues
data/LICENSE DELETED
@@ -1,13 +0,0 @@
1
- Copyright 2014 Chris Roberts
2
-
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
@@ -1,48 +0,0 @@
1
- require 'carnivore-unixsocket'
2
-
3
- module Carnivore
4
- module UnixSocket
5
- module Util
6
- class Connection
7
-
8
- include Celluloid
9
-
10
- attr_reader :srv, :io
11
-
12
- def initialize(args={})
13
- @io = args[:io]
14
- @io.sync
15
- @srv = args[:server]
16
- async.consume! if args[:auto_consume]
17
- end
18
-
19
- def consume!
20
- defer do
21
- loop do
22
- line = receive
23
- unless(line.empty?)
24
- srv.add_lines([line])
25
- end
26
- end
27
- end
28
- end
29
-
30
- def write_line(s)
31
- io.puts s
32
- io.flush
33
- end
34
-
35
- def receive
36
- line = io.gets
37
- if(line)
38
- line.strip
39
- else
40
- io.close
41
- terminate
42
- end
43
- end
44
-
45
- end
46
- end
47
- end
48
- end