crimson 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: a5da65ea222585d02e2af5148b3e4f3b51d1bb1b1a119879484e320717e88674
4
- data.tar.gz: 9b11f71db1c618a53df6ac15028d3d7eb76d1fb8773484be30349f65ae754813
3
+ metadata.gz: d0089dfdf0858ceff35c98db0799998355463be523a13420425294bee18c4a46
4
+ data.tar.gz: 681a505d387641e482379284548cd6a5055017347792112f179a4c28a0637b8f
5
5
  SHA512:
6
- metadata.gz: 46eacb8d903a59a88a8375791ced6686000e273914355b10950c48e77b777cf43ccc05fd0f081590a1ace4275d0b74c9df8eaf23275e8ba5cbe0b67cda50970e
7
- data.tar.gz: 5ec837a1e8871c71a8c3a09091c263da16414e9b8c878ae83fa12df455f1c0201a2060cc6266e8512a72a0a4da65df19c45ae340ef3a9fe16a11faac0c392b20
6
+ metadata.gz: 40046a67b7b174c74d585715271278dae16ce786e3ed92c8458cb60ea4c05012cca716f18acf7998ced6a6b1d09ab25810e5747b5f6f95e495f371eb5cbbc8b6
7
+ data.tar.gz: '0253249eeb2d990a0d88d154647566e4b69b480a8ca77bc37701f889ab9b7edf1b6be5f9c6a363ea64e6d034cae12ff4a587b4bbdef55f3fc21214dce7216e3d'
data/Gemfile CHANGED
@@ -4,8 +4,5 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'hashie'
7
- gem 'thin'
8
- gem 'sinatra'
9
7
  gem 'rubytree'
10
- gem 'eventmachine'
11
- gem 'websocket-eventmachine-server'
8
+ gem 'async-websocket'
@@ -6,8 +6,6 @@ require 'crimson/widgets/window'
6
6
  require 'crimson/widgets/form'
7
7
  require 'crimson/widgets/input'
8
8
 
9
- server = Crimson::Server.new
10
-
11
9
  desktop = Crimson::Desktop.new
12
10
  desktop.style.backgroundColor = "white"
13
11
 
@@ -53,6 +51,8 @@ login.content = form
53
51
 
54
52
  desktop.commit_tree!
55
53
 
54
+ server = Crimson::Server.new
55
+
56
56
  server.on_connect do |client|
57
57
  puts "#{client.id} connected"
58
58
 
@@ -63,4 +63,4 @@ server.on_disconnect do |client|
63
63
  puts "#{client.id} disconnected"
64
64
  end
65
65
 
66
- server.run
66
+ run server
@@ -0,0 +1,16 @@
1
+ require 'faye/websocket'
2
+
3
+ module Crimson
4
+ module Adapters
5
+ class Faye < SimpleDelegator
6
+ def initialize(ws)
7
+ @ws = ws
8
+ super(@ws)
9
+ end
10
+
11
+ def write(message)
12
+ @ws.send(message)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -9,15 +9,12 @@ module Crimson
9
9
 
10
10
  def initialize(id, connection)
11
11
  @id = id
12
-
13
12
  @connection = connection
14
- @connection.onmessage(&method(:on_message))
15
-
16
13
  @notification_bus = NotificationBus.new
17
14
  end
18
15
 
19
- def on_message(message, type)
20
- message = Hashie::Mash.new(JSON.parse(message))
16
+ def on_message(message)
17
+ message = Hashie::Mash.new(message)
21
18
 
22
19
  begin
23
20
  case message.action
@@ -30,7 +27,7 @@ module Crimson
30
27
  end
31
28
 
32
29
  def write(message = {})
33
- connection.send(message.to_json)
30
+ connection.write(message)
34
31
  end
35
32
 
36
33
  def on_commit(object, changes)
@@ -1,24 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thin'
4
- require 'websocket-eventmachine-server'
3
+ require 'async/websocket/adapters/rack'
4
+ require_relative 'adapters/faye'
5
5
  require_relative 'client'
6
- require_relative 'webserver'
7
6
  require_relative 'utilities'
8
7
 
9
8
  module Crimson
10
9
  class Server
11
10
  attr_reader :clients
12
11
 
13
- def initialize(opts = {})
14
- @opts = opts || {}
12
+ def initialize
15
13
  @clients = {}
16
14
  end
17
15
 
18
- def host
19
- @opts[:host] || '0.0.0.0'
20
- end
21
-
22
16
  def on_connect(&block)
23
17
  @on_connect = block if block_given?
24
18
  end
@@ -27,43 +21,76 @@ module Crimson
27
21
  @on_disconnect = block if block_given?
28
22
  end
29
23
 
30
- def port
31
- @opts[:port] || 10_000
24
+ def self.template_html_path
25
+ File.expand_path("#{__dir__}/../html/template.html")
32
26
  end
33
27
 
34
- def run(websocket_enabled: true, webserver_enabled: true)
35
- EM.run do
36
- start_websocket if websocket_enabled
37
- start_webserver if webserver_enabled
38
- end
28
+ def self.static
29
+ { :urls => [File.expand_path("#{__dir__}/.."), File.expand_path("#{__dir__}/../javascript")] }
30
+ end
31
+
32
+ def content(port, path = Server.template_html_path)
33
+ template = File.read(path)
34
+ template.sub!("{PORT}", port)
35
+
36
+ [template]
39
37
  end
40
38
 
41
- def start_webserver
42
- template = File.read("#{__dir__}/../html/template.html")
43
- template.sub!("{PORT}", websocket_port.to_s)
39
+ def call(env)
40
+ call_async(env) or call_faye(env) or serve_template(env)
41
+ end
42
+
43
+ def call_async(env)
44
+ Async::WebSocket::Adapters::Rack.open(env, protocols: ['ws']) do |connection|
45
+ id = :"client_#{Utilities.generate_id}"
46
+ client = Client.new(id, connection)
47
+ clients[connection] = client
48
+
49
+ @on_connect&.call(client)
44
50
 
45
- Thin::Server.start(WebServer.new(template), host, port, signals: false)
51
+ begin
52
+ while message = connection.read
53
+ client.on_message(message)
54
+ end
55
+ rescue Protocol::WebSocket::ClosedError
56
+
57
+ end
58
+ ensure
59
+ @on_disconnect&.call(client)
60
+ clients.delete(connection)
61
+ connection.close
62
+ end
46
63
  end
47
64
 
48
- def start_websocket
49
- WebSocket::EventMachine::Server.start(host: host, port: websocket_port) do |ws|
65
+ def call_faye(env)
66
+ if Faye::WebSocket.websocket?(env)
67
+ connection = Crimson::Adapters::Faye.new(Faye::WebSocket.new(env))
50
68
  id = :"client_#{Utilities.generate_id}"
51
- client = Client.new(id, ws)
69
+ client = Client.new(id, connection)
70
+ clients[id] = client
52
71
 
53
- ws.onopen {
54
- clients[id] = client
55
- @on_connect.call(client)
56
- }
72
+ @on_connect&.call(client)
57
73
 
58
- ws.onclose {
59
- @on_disconnect.call(client)
74
+ connection.on :message do |event|
75
+ client.on_message(JSON.parse(event.data))
76
+ end
77
+
78
+ connection.on :close do |event|
79
+ @on_disconnect&.call(client)
60
80
  clients.delete(id)
61
- }
81
+ connection = nil
82
+ end
83
+
84
+ return connection.rack_response
62
85
  end
63
86
  end
64
87
 
65
- def websocket_port
66
- port + 1
88
+ def serve_template(env)
89
+ if env['REQUEST_PATH'] != '/'
90
+ return Rack::Directory.new(File.expand_path("#{__dir__}/..")).call(env)
91
+ else
92
+ return [200, {"Content-Type" => "text/html"}, content(env['SERVER_PORT'])]
93
+ end
67
94
  end
68
95
  end
69
96
  end
@@ -1,3 +1,3 @@
1
1
  module Crimson
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -2,12 +2,17 @@ import MessageHandler from './MessageHandler.js'
2
2
 
3
3
  export default class ServerInteractor {
4
4
  constructor(uri, objectManager, logger) {
5
- let me = this;
5
+ this.uri = uri;
6
+ this.messageHandler = new MessageHandler(this, objectManager, logger);
7
+ this.logger = logger;
6
8
 
7
- me.messageHandler = new MessageHandler(me, objectManager, logger);
8
- me.logger = logger;
9
+ this.connect();
10
+ }
11
+
12
+ connect() {
13
+ const me = this;
9
14
 
10
- me.socket = new WebSocket("ws://" + uri);
15
+ me.socket = new WebSocket("ws://" + me.uri);
11
16
  me.socket.onopen = function (event) { me.onOpen(event); };
12
17
  me.socket.onmessage = function (event) { me.onMessage(event); };
13
18
  me.socket.onclose = function (event) { me.onClose(event); };
@@ -29,11 +34,18 @@ export default class ServerInteractor {
29
34
  }
30
35
 
31
36
  onClose(event) {
37
+ const me = this;
38
+
32
39
  if (event.wasClean) {
33
- this.logger.log(`[ServerInteractor] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
40
+ me.logger.log(`[ServerInteractor] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
34
41
  } else {
35
- this.logger.log("[ServerInteractor] Connection died");
42
+ me.logger.log("[ServerInteractor] Connection died.");
36
43
  }
44
+
45
+ me.logger.log("[ServerInteractor] Attempting to reestablish connection...")
46
+ setTimeout(function() {
47
+ me.connect();
48
+ }, 5000);
37
49
  }
38
50
 
39
51
  onError(error) {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crimson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rizwan Qureshi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-02 00:00:00.000000000 Z
11
+ date: 2020-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,10 +59,8 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".github/workflows/gempush.yml"
63
62
  - ".gitignore"
64
63
  - ".travis.yml"
65
- - ".vscode/launch.json"
66
64
  - CODE_OF_CONDUCT.md
67
65
  - Gemfile
68
66
  - LICENSE.txt
@@ -73,10 +71,10 @@ files:
73
71
  - crimson.gemspec
74
72
  - doc/images/prelim-ruby-js-comms.png
75
73
  - doc/images/temperature-readme-example.PNG
76
- - example/ets.rb
77
- - example/example.rb
74
+ - example/config.ru
78
75
  - lib/crimson.js
79
76
  - lib/crimson.rb
77
+ - lib/crimson/adapters/faye.rb
80
78
  - lib/crimson/client.rb
81
79
  - lib/crimson/icons/close.png
82
80
  - lib/crimson/icons/hide.png
@@ -89,7 +87,6 @@ files:
89
87
  - lib/crimson/server.rb
90
88
  - lib/crimson/utilities.rb
91
89
  - lib/crimson/version.rb
92
- - lib/crimson/webserver.rb
93
90
  - lib/crimson/widgets/bottom_resizer.rb
94
91
  - lib/crimson/widgets/desktop.rb
95
92
  - lib/crimson/widgets/form.rb
@@ -1,44 +0,0 @@
1
- name: Ruby Gem
2
-
3
- on:
4
- pull_request:
5
- branches:
6
- - master
7
- push:
8
- branches:
9
- - master
10
-
11
- jobs:
12
- build:
13
- name: Build + Publish
14
- runs-on: ubuntu-latest
15
-
16
- steps:
17
- - uses: actions/checkout@master
18
- - name: Set up Ruby 2.6
19
- uses: actions/setup-ruby@v1
20
- with:
21
- version: 2.6.x
22
-
23
- - name: Publish to GPR
24
- run: |
25
- mkdir -p $HOME/.gem
26
- touch $HOME/.gem/credentials
27
- chmod 0600 $HOME/.gem/credentials
28
- printf -- "---\n:github: Bearer ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
29
- gem build *.gemspec
30
- gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
31
- env:
32
- GEM_HOST_API_KEY: ${{secrets.GPR_AUTH_TOKEN}}
33
- OWNER: username
34
-
35
- - name: Publish to RubyGems
36
- run: |
37
- mkdir -p $HOME/.gem
38
- touch $HOME/.gem/credentials
39
- chmod 0600 $HOME/.gem/credentials
40
- printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
41
- gem build *.gemspec
42
- gem push *.gem
43
- env:
44
- GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -1,14 +0,0 @@
1
- {
2
- // Use IntelliSense to learn about possible attributes.
3
- // Hover to view descriptions of existing attributes.
4
- // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
- "version": "0.2.0",
6
- "configurations": [
7
- {
8
- "name": "Debug Local File",
9
- "type": "Ruby",
10
- "request": "launch",
11
- "program": "${workspaceRoot}/example/ets.rb"
12
- }
13
- ]
14
- }
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../crimson'
4
- require_relative '../lib/ruby/widgets/desktop'
5
-
6
- # server = Crimson::Server.new
7
-
8
- # desktop = Crimson::Desktop.new
9
- # desktop.style.backgroundColor = 'black'
10
- # desktop.commit_tree!
11
-
12
- # server.on_connect do |client|
13
- # puts "#{client.id} connected"
14
-
15
- # client.observe(desktop)
16
- # end
17
-
18
- # server.on_disconnect do |client|
19
- # puts "#{client.id} disconnected"
20
- # end
21
-
22
- # server.run
@@ -1,17 +0,0 @@
1
- require 'sinatra/base'
2
-
3
- module Crimson
4
- class WebServer < Sinatra::Base
5
- set :public_folder, "#{__dir__}/../"
6
-
7
- def initialize(template)
8
- super()
9
-
10
- @template = template
11
- end
12
-
13
- get '/' do
14
- @template
15
- end
16
- end
17
- end