appborg 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/appborg.gemspec +19 -0
- data/src/appborg.rb +85 -0
- data/test/example.rb +23 -0
- data/test/subproc.coffee +7 -0
- metadata +7 -4
data/appborg.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
|
4
|
+
s.name = "appborg"
|
5
|
+
s.version = "0.0.2"
|
6
|
+
s.homepage = "http://github.com/andrewschaaf/appborg"
|
7
|
+
s.authors = ["Andrew Schaaf"]
|
8
|
+
s.email = "andrew@andrewschaaf.com"
|
9
|
+
s.summary = "appborg helps you build hybrid apps: native + webkit* + subprocess*"
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split
|
12
|
+
s.platform = Gem::Platform::RUBY
|
13
|
+
s.require_paths = ["src"]
|
14
|
+
s.required_rubygems_version = ">= 1.3.5"
|
15
|
+
s.required_ruby_version = ">= 1.9.2"
|
16
|
+
|
17
|
+
s.add_runtime_dependency 'multi_json', '~> 1.0', '>= 1.0.3'
|
18
|
+
|
19
|
+
end
|
data/src/appborg.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
|
2
|
+
require 'multi_json'
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
|
6
|
+
def appborg_decodeLine(line)
|
7
|
+
MultiJson.decode Base64::decode64 line
|
8
|
+
end
|
9
|
+
|
10
|
+
def appborg_encodeLine(x)
|
11
|
+
json = MultiJson.encode x
|
12
|
+
json64 = Base64::strict_encode64 json
|
13
|
+
json64 + "\n"
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
module Appborg
|
18
|
+
|
19
|
+
DEFAULT_WRAPPER_NAME = 'subprocess'
|
20
|
+
DEFAULT_ROUTER_NAME = 'router'
|
21
|
+
|
22
|
+
class PTYWrapper
|
23
|
+
def initialize(output, input, pid)
|
24
|
+
@output = output
|
25
|
+
@input = input
|
26
|
+
@pid = pid
|
27
|
+
end
|
28
|
+
|
29
|
+
def killDashNine()
|
30
|
+
# "Kill dash nine / no more CPU time!"
|
31
|
+
`kill -p #{@pid}`
|
32
|
+
end
|
33
|
+
|
34
|
+
def sendEventSync(e)
|
35
|
+
@input.write appborg_encodeLine e
|
36
|
+
buffer = ""
|
37
|
+
2.times do
|
38
|
+
buffer = ""
|
39
|
+
@output.readpartial(250000, buffer) until buffer =~ /\n/
|
40
|
+
end
|
41
|
+
appborg_decodeLine buffer
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
class Router
|
47
|
+
def initialize()
|
48
|
+
@names = [DEFAULT_ROUTER_NAME]
|
49
|
+
@wrappers = {}
|
50
|
+
@counter = 0
|
51
|
+
end
|
52
|
+
|
53
|
+
def _nextId()
|
54
|
+
@counter += 1
|
55
|
+
@counter
|
56
|
+
end
|
57
|
+
|
58
|
+
def addPTY(output, input, pid, name=DEFAULT_WRAPPER_NAME)
|
59
|
+
@wrappers[name] = PTYWrapper.new(output, input, pid)
|
60
|
+
end
|
61
|
+
|
62
|
+
def sendSync(method, info={})
|
63
|
+
dest = DEFAULT_WRAPPER_NAME
|
64
|
+
request = {
|
65
|
+
:id => _nextId(),
|
66
|
+
:from => @names[0],
|
67
|
+
:to => dest,
|
68
|
+
:method => method,
|
69
|
+
:info => info
|
70
|
+
}
|
71
|
+
response = @wrappers[dest].sendEventSync request
|
72
|
+
if not response['error'].nil?
|
73
|
+
throw response['error']
|
74
|
+
else
|
75
|
+
return response['result']
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def killDashNine()
|
80
|
+
@wrappers.each do |k, wrapper|
|
81
|
+
wrapper.killDashNine()
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/test/example.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
require '../src/appborg'
|
3
|
+
require 'pty'
|
4
|
+
|
5
|
+
|
6
|
+
router = Appborg::Router.new
|
7
|
+
|
8
|
+
r, w, pid = PTY.spawn 'coffee', 'subproc.coffee'
|
9
|
+
router.addPTY r, w, pid
|
10
|
+
|
11
|
+
|
12
|
+
result = router.sendSync 'foo'
|
13
|
+
puts "."
|
14
|
+
|
15
|
+
|
16
|
+
begin
|
17
|
+
result = router.sendSync 'sample-exception'
|
18
|
+
rescue
|
19
|
+
puts "."
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
puts "OK"
|
data/test/subproc.coffee
ADDED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: appborg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Andrew Schaaf
|
@@ -35,8 +35,11 @@ extensions: []
|
|
35
35
|
|
36
36
|
extra_rdoc_files: []
|
37
37
|
|
38
|
-
files:
|
39
|
-
|
38
|
+
files:
|
39
|
+
- appborg.gemspec
|
40
|
+
- src/appborg.rb
|
41
|
+
- test/example.rb
|
42
|
+
- test/subproc.coffee
|
40
43
|
has_rdoc: true
|
41
44
|
homepage: http://github.com/andrewschaaf/appborg
|
42
45
|
licenses: []
|
@@ -45,7 +48,7 @@ post_install_message:
|
|
45
48
|
rdoc_options: []
|
46
49
|
|
47
50
|
require_paths:
|
48
|
-
-
|
51
|
+
- src
|
49
52
|
required_ruby_version: !ruby/object:Gem::Requirement
|
50
53
|
none: false
|
51
54
|
requirements:
|