json-socket 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/json-socket.rb +25 -25
  3. metadata +22 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46a865cdc1b4ab82e552adcb126791787d5fefe87d27563deeeef81b07d24dab
4
- data.tar.gz: edf79c31433a6962db8272dbd463b0f60056c129a892b93423ac6333b8e2d169
3
+ metadata.gz: 875091899544dfd73d4014d91052776f04ef92c62acd78d233701baf82a2f01c
4
+ data.tar.gz: db3aedc20dabe6e640d83fe682cde75dbde60ddc601eda29269099e6f2fca52f
5
5
  SHA512:
6
- metadata.gz: 98c75257eb925c267d1ec530a9602d44078c4d21f7e05eafec04531f33a58118f2962aaf7bd84b0cea87b23fbc0ffac9b2c9c23382aff36b87f50d78e89f3574
7
- data.tar.gz: 8d3f6bbc130ef056f8bc91bcdebe6784a0f2865c68fc3a6f75b5ef82891dc9839e1099dfc53ee3fefd58256ea41f8defe6989f3b0a1235ad3731ebc1674a87a3
6
+ metadata.gz: bc524ff94905b6f97ab70d6bbc6c0ee067737cededacb4eec6c2bfc5c2dd74ed76d2455fcc494c2abaa3c8ef2f4b74016941893e2f747715d3d3646f25b1d67e
7
+ data.tar.gz: 7a56e89c0320e7b494dafee28037567214b4663168d82eb08536e8c90c74a15074a35958d939b9cfe100b11c7f83e544eaf35287594ce891b2e2a7d2120679f4
data/lib/json-socket.rb CHANGED
@@ -1,6 +1,8 @@
1
- require "socket"
1
+ # require "socket"
2
2
  require "fileutils"
3
3
  require "oj"
4
+ require "async/io"
5
+ require 'async/io/unix_endpoint'
4
6
 
5
7
  Oj.default_options = { :mode => :strict }
6
8
 
@@ -25,35 +27,37 @@ module JSONSocket
25
27
  def initialize(host: "127.0.0.1", port: 1234, delimeter: "#", unix_socket: nil, oj_options: nil)
26
28
  Oj.default_options = oj_options if oj_options
27
29
  @delimeter = delimeter
28
- @stop = false
29
30
  @server = if unix_socket
30
31
  FileUtils.rm_f(unix_socket)
31
- UNIXServer.new unix_socket
32
+ Async::IO::Endpoint.unix(unix_socket)
32
33
  else
33
- TCPServer.new host, port
34
+ Async::IO::Endpoint.tcp(host, port)
34
35
  end
35
36
  end
36
37
 
37
- def stop
38
- @stop = true
39
- end
40
-
41
38
  def listen
42
- loop do
43
- Thread.start(@server.accept) do |client|
44
- begin
45
- # https://stackoverflow.com/questions/25303943/how-to-send-a-utf-8-encoded-strings-via-tcpsocket-in-ruby/25305256
46
- client.set_encoding 'UTF-8'
47
- message_length = client.gets(@delimeter).to_i
39
+ Async do |task|
40
+ begin
41
+ @server.accept do |client|
42
+ client = client.set_encoding 'UTF-8'
43
+ # https://stackoverflow.com/questions/25303943/how-to-send-a-utf-8-encoded-strings-via-tcpsocket-in-ruby/25305256
44
+ message_length = client.gets(@delimeter).to_i
45
+ unless message_length == 0
48
46
  on_message(parse_json(client.read(message_length)), client)
49
- rescue Exception => e
50
- STDERR.puts e.message
47
+ else
48
+ client.close
51
49
  end
52
50
  end
53
- break if @stop
51
+ rescue Exception => e
52
+ on_error e
53
+ end
54
54
  end
55
55
  end
56
56
 
57
+ def on_error e
58
+ STDERR.puts e.message
59
+ end
60
+
57
61
  def on_message(message, client)
58
62
  puts "Default on_message method - please override like this: \n" \
59
63
  " \n" \
@@ -100,14 +104,10 @@ module JSONSocket
100
104
  end
101
105
 
102
106
  def send(message)
103
- begin
104
- if @unix_socket
105
- UNIXSocket.open(@unix_socket) {|socket| handle_send_receive(socket, message) }
106
- else
107
- TCPSocket.open(@host, @port) {|socket| handle_send_receive(socket, message) }
108
- end
109
- rescue Exception => e
110
- STDERR.puts e
107
+ if @unix_socket
108
+ UNIXSocket.open(@unix_socket) {|socket| handle_send_receive(socket, message) }
109
+ else
110
+ TCPSocket.open(@host, @port) {|socket| handle_send_receive(socket, message) }
111
111
  end
112
112
  end
113
113
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-socket
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - foi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-03 00:00:00.000000000 Z
11
+ date: 2023-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-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'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,8 +52,8 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
- description: JSON-socket client & server implementation. Inspired by and compatible
42
- with sebastianseilund/node-json-socket
55
+ description: JSON-socket client & server implementation based on async-io. Inspired
56
+ by and compatible with sebastianseilund/node-json-socket
43
57
  email: foi@live.ru
44
58
  executables: []
45
59
  extensions: []
@@ -50,7 +64,7 @@ homepage: https://github.com/foi/ruby-json-socket
50
64
  licenses:
51
65
  - MIT
52
66
  metadata: {}
53
- post_install_message:
67
+ post_install_message:
54
68
  rdoc_options: []
55
69
  require_paths:
56
70
  - lib
@@ -65,9 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
79
  - !ruby/object:Gem::Version
66
80
  version: '0'
67
81
  requirements: []
68
- rubyforge_project:
69
- rubygems_version: 2.7.6
70
- signing_key:
82
+ rubygems_version: 3.4.1
83
+ signing_key:
71
84
  specification_version: 4
72
85
  summary: json-socket protocol implementation
73
86
  test_files: []