xfiredb 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/xfiredb/client.rb +107 -0
- data/lib/xfiredb/result.rb +90 -0
- data/lib/xfiredb/socket.rb +48 -0
- data/lib/xfiredb/version.rb +4 -0
- data/lib/xfiredb.rb +58 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9bd830b09b250371533632ecf4aa70b9f9d40a7d
|
4
|
+
data.tar.gz: 63c8d424676a7b44b615a7be898c66e5248ffed7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ac853af16c4d8341f9d7e8076adc4e9f3c836ac8cfd1b7270c65ebea89ea08706e1f183bfb12673da199a1080d4d69fd942c02afd2bbb08a28d904a01032897
|
7
|
+
data.tar.gz: baef7b08a54e305e05040b95f14f9ce118ab85c958086e05b7a3af8063d9fcf44027aca048ee5a98537e6401c35dfb408e3676c5a8476d4b6b6b787017345cfa
|
@@ -0,0 +1,107 @@
|
|
1
|
+
#
|
2
|
+
# XFireDB client library
|
3
|
+
# Copyright (C) 2016 Michel Megens <dev@michelmegens.net>
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
#
|
18
|
+
|
19
|
+
module XFireDB
|
20
|
+
# Representation of the connecting client.
|
21
|
+
class Client
|
22
|
+
# Create a new client.
|
23
|
+
def initialize
|
24
|
+
@connected = false
|
25
|
+
@ssl = false
|
26
|
+
@authenticated = false
|
27
|
+
end
|
28
|
+
|
29
|
+
# Connect to an XFireDB server
|
30
|
+
#
|
31
|
+
# @param [String] host Server host.
|
32
|
+
# @param [Fixnum] port Server port.
|
33
|
+
# @param [Fixnum] flags Connection flags.
|
34
|
+
# @param [String] user Username.
|
35
|
+
# @param [String] pass Password.
|
36
|
+
# @return [Boolean] True if the connection was succesful, false otherwise.
|
37
|
+
def connect(host, port, flags = nil, user = nil, pass = nil)
|
38
|
+
@socket = XFireDB::Socket.connect(host, port, flags & XFireDB::SSL)
|
39
|
+
@ssl = true if @socket.class == XFireDB::SSLSocket
|
40
|
+
@stream = true if flags & XFireDB::STREAM == XFireDB::STREAM
|
41
|
+
|
42
|
+
if flags & XFireDB::AUTH == XFireDB::AUTH
|
43
|
+
@socket.puts "AUTH #{user} #{pass}"
|
44
|
+
result = @socket.gets.chomp
|
45
|
+
|
46
|
+
if result == "OK"
|
47
|
+
@authenticated = true
|
48
|
+
else
|
49
|
+
return false
|
50
|
+
end
|
51
|
+
|
52
|
+
@socket.puts "STREAM" if @stream
|
53
|
+
elsif @stream
|
54
|
+
@socket.puts "STREAM"
|
55
|
+
end
|
56
|
+
|
57
|
+
@connected = true
|
58
|
+
return true
|
59
|
+
end
|
60
|
+
|
61
|
+
# Query a client.
|
62
|
+
# @param [String] q Query to send to the server.
|
63
|
+
# @return [true]
|
64
|
+
def query(q)
|
65
|
+
return false unless @connected
|
66
|
+
|
67
|
+
@socket.puts q
|
68
|
+
reply = @socket.gets
|
69
|
+
reply.chomp! if reply
|
70
|
+
num = reply.scan(/\ /).count + 1
|
71
|
+
|
72
|
+
puts num
|
73
|
+
|
74
|
+
num.times do
|
75
|
+
data = @socket.gets
|
76
|
+
data.chomp! if data
|
77
|
+
puts data
|
78
|
+
res = XFireDB::Result.new data
|
79
|
+
res.process
|
80
|
+
yield res
|
81
|
+
end
|
82
|
+
|
83
|
+
true
|
84
|
+
end
|
85
|
+
|
86
|
+
# Close a connection.
|
87
|
+
def close
|
88
|
+
@connected = false
|
89
|
+
@authenticated = false
|
90
|
+
@stream = false
|
91
|
+
|
92
|
+
@socket.puts "QUIT" if @connected
|
93
|
+
@socket.close
|
94
|
+
end
|
95
|
+
|
96
|
+
# Check if a client is connected.
|
97
|
+
def connected?
|
98
|
+
@connected
|
99
|
+
end
|
100
|
+
|
101
|
+
# Check if the client is using SSL.
|
102
|
+
def ssl?
|
103
|
+
@ssl
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
#
|
2
|
+
# XFireDB client library
|
3
|
+
# Copyright (C) 2016 Michel Megens <dev@michelmegens.net>
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
#
|
18
|
+
|
19
|
+
module XFireDB
|
20
|
+
# Query result class
|
21
|
+
class Result
|
22
|
+
attr_reader :data, :status
|
23
|
+
|
24
|
+
# Status OK
|
25
|
+
OK = 0x1
|
26
|
+
# Status SUCCESS
|
27
|
+
SUCCESS = 0x2
|
28
|
+
# Status NULL
|
29
|
+
NULL = 0x4
|
30
|
+
# Status MESSAGE
|
31
|
+
MSG = 0x8
|
32
|
+
|
33
|
+
# Query success status codes
|
34
|
+
ACK = XFireDB::Result::OK | XFireDB::Result::SUCCESS
|
35
|
+
|
36
|
+
# Create a new result object
|
37
|
+
#
|
38
|
+
# @param [String] dat Raw query data.
|
39
|
+
def initialize(dat)
|
40
|
+
@data = dat
|
41
|
+
@status = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
# Process the data.
|
45
|
+
def process
|
46
|
+
case @data[0,1]
|
47
|
+
when '+' # String reply
|
48
|
+
@data.slice!(0)
|
49
|
+
@status = XFireDB::Result::SUCCESS
|
50
|
+
when '&' # Boolean result
|
51
|
+
@status = XFireDB::Result::SUCCESS
|
52
|
+
@data = true if @data == "&true"
|
53
|
+
@data = false if @data == "&false"
|
54
|
+
when '%' # Numeral result
|
55
|
+
@status = XFireDB::Result::SUCCESS
|
56
|
+
@data.slice!(0)
|
57
|
+
@data = @data.to_i
|
58
|
+
when '-'
|
59
|
+
process_default
|
60
|
+
else
|
61
|
+
process_default
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Check whether the query was succesfull or not.
|
66
|
+
def success?
|
67
|
+
@status & XFireDB::Result::SUCCESS == XFireDB::Result::SUCCESS
|
68
|
+
end
|
69
|
+
|
70
|
+
# Check if the result is NULL.
|
71
|
+
def null?
|
72
|
+
@status & XFireDB::Result::NULL == XFireDB::Result::NULL
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
# Process status codes.
|
77
|
+
def process_default
|
78
|
+
@data.slice!(0) if @data[0,1] == '-'
|
79
|
+
|
80
|
+
if @data == "OK"
|
81
|
+
@status = ACK
|
82
|
+
elsif @data == "nil"
|
83
|
+
@status = NULL
|
84
|
+
else
|
85
|
+
@status = MSG
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#
|
2
|
+
# XFireDB client library
|
3
|
+
# Copyright (C) 2016 Michel Megens <dev@michelmegens.net>
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
#
|
18
|
+
|
19
|
+
module XFireDB
|
20
|
+
# Socket helper class
|
21
|
+
class Socket
|
22
|
+
# Connect a socket.
|
23
|
+
# @param [String] addr Server address.
|
24
|
+
# @param [Fixnum] port Server port.
|
25
|
+
# @param [Boolean] ssl Set to true to enable SSL.
|
26
|
+
def self.connect(addr, port, ssl)
|
27
|
+
sock = TCPSocket.new addr, port
|
28
|
+
|
29
|
+
return sock unless ssl
|
30
|
+
return XFireDB::SSLSocket.new sock
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Connect SSL version of the TCPSocket class.
|
35
|
+
class SSLSocket < OpenSSL::SSL::SSLSocket
|
36
|
+
@sock = nil
|
37
|
+
|
38
|
+
# Create a new SSL socket.
|
39
|
+
# @param [TCPSocket] sock Base socket.
|
40
|
+
def initialize(sock)
|
41
|
+
super(sock)
|
42
|
+
|
43
|
+
@sock = sock
|
44
|
+
self.sync_close = true
|
45
|
+
self.connect
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/xfiredb.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#
|
2
|
+
# XFireDB client library
|
3
|
+
# Copyright (C) 2016 Michel Megens <dev@michelmegens.net>
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'socket'
|
20
|
+
require 'openssl'
|
21
|
+
|
22
|
+
require 'xfiredb/socket'
|
23
|
+
require 'xfiredb/version'
|
24
|
+
require 'xfiredb/client'
|
25
|
+
require 'xfiredb/result'
|
26
|
+
|
27
|
+
# XFireDB client module.
|
28
|
+
module XFireDB
|
29
|
+
# SSL connection flag.
|
30
|
+
SSL = 0x1
|
31
|
+
# Authentication required flag.
|
32
|
+
AUTH = 0x2
|
33
|
+
# Command stream flag.
|
34
|
+
STREAM = 0x4
|
35
|
+
|
36
|
+
class << self
|
37
|
+
# Create a new client.
|
38
|
+
# @return [Client] A new client object.
|
39
|
+
def new
|
40
|
+
Client.new
|
41
|
+
end
|
42
|
+
|
43
|
+
# Connect to an XFireDB server
|
44
|
+
#
|
45
|
+
# @param [String] host Server host.
|
46
|
+
# @param [Fixnum] port Server port.
|
47
|
+
# @param [Fixnum] flags Connection flags.
|
48
|
+
# @param [String] user Username.
|
49
|
+
# @param [String] pass Password.
|
50
|
+
# @return [Client] A client object.
|
51
|
+
def connect(host, port, flags = nil, user = nil, pass = nil)
|
52
|
+
client = Client.new
|
53
|
+
client.connect(host, port, flags, user, pass)
|
54
|
+
return client
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xfiredb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michel Megens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: XFireDB ruby client library
|
56
|
+
email:
|
57
|
+
- dev@michelmegens.net
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/xfiredb.rb
|
63
|
+
- lib/xfiredb/client.rb
|
64
|
+
- lib/xfiredb/result.rb
|
65
|
+
- lib/xfiredb/socket.rb
|
66
|
+
- lib/xfiredb/version.rb
|
67
|
+
homepage: http://xfiredb.bietje.net
|
68
|
+
licenses: []
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.4.8
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: XFireDB connector
|
90
|
+
test_files: []
|