carnivore-unixsocket 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +2 -0
- data/CONTRIBUTING.md +25 -0
- data/LICENSE +13 -0
- data/README.md +21 -0
- data/carnivore-unixsocket.gemspec +16 -0
- data/lib/carnivore-unixsocket/unixsocket.rb +85 -0
- data/lib/carnivore-unixsocket/util/socket_connection.rb +48 -0
- data/lib/carnivore-unixsocket/util/socket_server.rb +61 -0
- data/lib/carnivore-unixsocket/util.rb +12 -0
- data/lib/carnivore-unixsocket/version.rb +6 -0
- data/lib/carnivore-unixsocket.rb +13 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e0690f9cf11c543bfc6f51202a972f3b6852228b
|
4
|
+
data.tar.gz: 0b450308e838d001f603b90ea15f1b491a6b1506
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 504228019293064bd168444a5018c73c09efd6e8077c1b438eac9bb30c479095f2949bf36a609d4d904dd5067c2e32503e1b1e06cad7289a0aecffc7ae1bc53d
|
7
|
+
data.tar.gz: 4559e2340b4861cb4e133dc880857241eb61b038bd7603c86a1af7ea0f5331e686414a23bb6746fbf57700ed386bd5b152888f2e7ff5dc7b3c63a1296cd4cff0
|
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,25 @@
|
|
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
ADDED
@@ -0,0 +1,13 @@
|
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Carnivore Unix Socket
|
2
|
+
|
3
|
+
Provides Unix Socket `Carnivore::Source`
|
4
|
+
|
5
|
+
# Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'carnivore'
|
9
|
+
require 'carnivore-unixsocket'
|
10
|
+
|
11
|
+
Carnivore.configure do
|
12
|
+
source = Carnivore::Source.build(
|
13
|
+
:type => :socket, :args => {:path => '/var/run/my.sock'}
|
14
|
+
)
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
# Info
|
19
|
+
* Carnivore: https://github.com/carnivore-rb/carnivore
|
20
|
+
* Repository: https://github.com/carnivore-rb/carnivore-unixsocket
|
21
|
+
* IRC: Freenode @ #carnivore
|
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__)) + '/lib/'
|
2
|
+
require 'carnivore-unixsocket/version'
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'carnivore-unixsocket'
|
5
|
+
s.version = Carnivore::UnixSocket::VERSION.version
|
6
|
+
s.summary = 'Message processing helper'
|
7
|
+
s.author = 'Chris Roberts'
|
8
|
+
s.email = 'chrisroberts.code@gmail.com'
|
9
|
+
s.homepage = 'https://github.com/carnivore-rb/carnivore-unixsocket'
|
10
|
+
s.description = 'Carnivore Unix Socket Source'
|
11
|
+
s.license = 'Apache 2.0'
|
12
|
+
s.require_path = 'lib'
|
13
|
+
s.add_dependency 'carnivore', '>= 0.1.8'
|
14
|
+
s.add_dependency 'celluloid-io'
|
15
|
+
s.files = Dir['**/*']
|
16
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'carnivore-unixsocket'
|
2
|
+
|
3
|
+
module Carnivore
|
4
|
+
class Source
|
5
|
+
# Unix socket based carnivore source
|
6
|
+
class UnixSocket < Source
|
7
|
+
|
8
|
+
# max time for unix socket to setup
|
9
|
+
INIT_SRV_TIMEOUT = 2.0
|
10
|
+
|
11
|
+
# @return [String] path to socket
|
12
|
+
attr_reader :socket
|
13
|
+
# @return [String]
|
14
|
+
attr_reader :srv_name
|
15
|
+
# @return [Hash]
|
16
|
+
attr_reader :init_args
|
17
|
+
|
18
|
+
# Setup the unix socket
|
19
|
+
#
|
20
|
+
# @param args [Hash]
|
21
|
+
def setup(args={})
|
22
|
+
@socket = ::File.expand_path(args[:path])
|
23
|
+
@srv_name = "socket_srv_#{name}".to_sym
|
24
|
+
@connection = nil
|
25
|
+
@init_args = args
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [Util::Server]
|
29
|
+
def server
|
30
|
+
callback_supervisor[srv_name]
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Celluloid::IO::UNIXSocket]
|
34
|
+
def connection
|
35
|
+
unless(@connection)
|
36
|
+
@connection = Celluloid::IO::UNIXSocket.new(socket)
|
37
|
+
end
|
38
|
+
@connection
|
39
|
+
end
|
40
|
+
|
41
|
+
# Initialize the server
|
42
|
+
def init_srv
|
43
|
+
callback_supervisor.supervise_as(srv_name,
|
44
|
+
Carnivore::UnixSocket::Util::Server,
|
45
|
+
init_args.merge(:notify_actor => current_actor)
|
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
|
53
|
+
end
|
54
|
+
|
55
|
+
# Receive messages
|
56
|
+
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
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Send message
|
68
|
+
#
|
69
|
+
# @param payload [Object]
|
70
|
+
# @param original_message [Carnivore::Message]
|
71
|
+
def transmit(payload, original_message=nil)
|
72
|
+
output = payload.is_a?(String) ? payload : MultiJson.dump(payload)
|
73
|
+
connection.puts(output)
|
74
|
+
connection.flush
|
75
|
+
end
|
76
|
+
|
77
|
+
# Override processing to enable server only if required
|
78
|
+
def process(*args)
|
79
|
+
init_srv
|
80
|
+
super
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,48 @@
|
|
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
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'carnivore-unixsocket'
|
2
|
+
require 'celluloid/io'
|
3
|
+
|
4
|
+
module Carnivore
|
5
|
+
module UnixSocket
|
6
|
+
module Util
|
7
|
+
class Server
|
8
|
+
|
9
|
+
include Celluloid
|
10
|
+
include Carnivore::Utils::Logging
|
11
|
+
|
12
|
+
attr_reader :path, :notify_actor, :server, :supervisor
|
13
|
+
|
14
|
+
def initialize(args={})
|
15
|
+
@path = ::File.expand_path(args[:path])
|
16
|
+
@notify_actor = args[:notify_actor]
|
17
|
+
@supervisor = Celluloid::SupervisionGroup.run!
|
18
|
+
@messages = []
|
19
|
+
end
|
20
|
+
|
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'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def setup_server!
|
38
|
+
unless(@server)
|
39
|
+
if(::File.exists?(path))
|
40
|
+
::File.delete(path)
|
41
|
+
end
|
42
|
+
@server = Celluloid::IO::UNIXServer.new(path)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
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
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'celluloid/io'
|
2
|
+
require 'carnivore'
|
3
|
+
|
4
|
+
module Carnivore
|
5
|
+
module UnixSocket
|
6
|
+
|
7
|
+
autoload :Util, 'carnivore-unixsocket/util'
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'carnivore-unixsocket/version'
|
13
|
+
Carnivore::Source.provide(:unix_socket, 'carnivore-unixsocket/unixsocket')
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carnivore-unixsocket
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Roberts
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: carnivore
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.8
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
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'
|
41
|
+
description: Carnivore Unix Socket Source
|
42
|
+
email: chrisroberts.code@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- CHANGELOG.md
|
48
|
+
- CONTRIBUTING.md
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- carnivore-unixsocket.gemspec
|
52
|
+
- lib/carnivore-unixsocket.rb
|
53
|
+
- lib/carnivore-unixsocket/unixsocket.rb
|
54
|
+
- lib/carnivore-unixsocket/util.rb
|
55
|
+
- lib/carnivore-unixsocket/util/socket_connection.rb
|
56
|
+
- lib/carnivore-unixsocket/util/socket_server.rb
|
57
|
+
- lib/carnivore-unixsocket/version.rb
|
58
|
+
homepage: https://github.com/carnivore-rb/carnivore-unixsocket
|
59
|
+
licenses:
|
60
|
+
- Apache 2.0
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.2.2
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Message processing helper
|
82
|
+
test_files: []
|
83
|
+
has_rdoc:
|