f_dot 0.0.2 → 0.0.3

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: b88e18072524eac7b3bcc2c89bc24575df4f8463bf430ca1572058c52823cad1
4
- data.tar.gz: c2d40369259cefaa3fc9f2b6676e9bcdc4b63d67def0f466daace4859e34b463
3
+ metadata.gz: 0e203f5541a0a89addcc7e258c9b508772a0c077c8f72e92ed956cdbdb858614
4
+ data.tar.gz: cbc6055801dd720dfe4c0d08ad135706e1cb7d832f6e8cb5eac0e6472cca2dc6
5
5
  SHA512:
6
- metadata.gz: f485a41b3e59a3a5173c71b025d286ee6260997827aee5c749824a2c98df0a05c459f2c7643279ceefa688f19b0462eb225b11d9db4891fafa286367eb903aff
7
- data.tar.gz: 5276e9c537d47f573c42a4476ed071abd6915a1bc00cd83a1fd2f1e28f31388d5627a09e71c0838c0cce9891455ea7b5ada46106f89333a8c64a1d58db7a4116
6
+ metadata.gz: 3140ccc8abae7b9153d2a3f6e88023f98fd6b5b64989415ea875b7de9913c21b50f6878ee909705e81a80586fcec6511adfe0134082d1faf2629196cc067259a
7
+ data.tar.gz: 52f344b1a46aafd808f6a111ae1fcb318f8ec55368a3a8e24cd60cb379c8cbf69b488e265c9a89f5fbd9ab33f4fc671773997b5fc4bdcf1705edd3458034b7f2
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg/*
2
2
  f_dot_local_storage*
3
- .f_dot
3
+ .f_dot
4
+ *.DS_Store
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- f_dot (0.0.2)
4
+ f_dot (0.0.3)
5
5
  tty-font (~> 0.5, >= 0.5.0)
6
6
  tty-prompt (~> 0.23, >= 0.23.1)
7
7
 
@@ -109,7 +109,7 @@ CHECKSUMS
109
109
  bundler (4.0.22) sha256=d8d5ec84c8555e0af71db63ed7aee4d1a8fb839ec46d84212d61979242a5d75a
110
110
  diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
111
111
  erb (6.0.7) sha256=c5ca6dc25b0ef974a44dc8f59fe847577122483b1968a38dec305c60bf91ee92
112
- f_dot (0.0.2)
112
+ f_dot (0.0.3)
113
113
  ffi (1.17.4) sha256=bcd1642e06f0d16fc9e09ac6d49c3a7298b9789bcb58127302f934e437d60acf
114
114
  ffi (1.17.4-aarch64-linux-gnu) sha256=b208f06f91ffd8f5e1193da3cae3d2ccfc27fc36fba577baf698d26d91c080df
115
115
  ffi (1.17.4-aarch64-linux-musl) sha256=9286b7a615f2676245283aef0a0a3b475ae3aae2bb5448baace630bb77b91f39
@@ -1 +1 @@
1
- 13d938dde631803c01e41fdb6ae0ce20fe84d12bbc6af7bd375940061942443c7ed0aefd6d524a7532905414dbf15052fd7204d909e28346e6dd40250d87e464
1
+ 4200c173de60db638e6201853796f183a91603c282240eaade91189c534a215776035268be08568a544d3dcfa9b2ba3c8c7b55c9a278fd078f1ef04061555cf6
@@ -0,0 +1 @@
1
+ 99164fd592df36c2b697937ead5d04421d6a760dbcef163e80834d48a0eb29529613fb465bd2d06abbde3b90a86b7ab03cd742430c561c475d5d19d123e83ea6
data/lib/f_dot/based.rb CHANGED
@@ -74,7 +74,7 @@ module FDot
74
74
  yield(new_value)
75
75
  }
76
76
  else
77
- return FDot::Based::PROMPT.ask(message)
77
+ yield(FDot::Based::PROMPT.ask(message))
78
78
  end
79
79
  end
80
80
 
data/lib/f_dot/cubicus.rb CHANGED
@@ -20,41 +20,59 @@ module FDot
20
20
  :display,
21
21
  :input,
22
22
  :f_dot_go,
23
- :"index.html"
23
+ :"index.html",
24
+ :javascript,
25
+ :stylesheets,
26
+ :projector,
27
+ :projector_input
24
28
  ]
25
29
 
26
30
  attr_accessor :ws_thread
27
31
  attr_accessor :controls
28
- attr_accessor :is_running
29
32
  attr_accessor :original_stdout
30
- attr_accessor :controls_delegate
31
-
32
- FOLDER_NAME = ".cubicus"
33
+ attr_accessor :projector_writeback
33
34
 
34
35
  GO_SERVER_BASENAME = "f_dot.go"
35
36
 
36
37
  def initialize
37
- self.is_running = false
38
38
  self.ws_thread = nil
39
39
  self.controls = {}
40
- self.controls_delegate = nil
40
+ self.projector_writeback = Proc.new{|data| puts "implement me"}
41
41
  end
42
42
 
43
43
  def start_ws_thread
44
- self.ws_thread&.exit
45
- self.ws_thread&.kill
44
+ self.class.run_exit_cleanup!
46
45
 
47
46
  prepare_f_dot_go!
48
47
 
49
- FDot::Based::Interface.instance.display_mode = FDot::DISPLAY_MODES[:websockets]
50
48
  go_server_path = self.class.path_for_file(:f_dot_go)
51
49
 
52
50
  self.original_stdout = $stdout
53
- $stdout.reopen(self.class.path_for_file(:display), "w")
54
- $stdout.sync = true
51
+ raise "missing ENV['LOCAL_THREEJS']!" if ENV["LOCAL_THREEJS"].nil? ||
52
+ !Dir.exist?(ENV["LOCAL_THREEJS"])
53
+
54
+ go_server_run_command = [
55
+ "LOCAL_THREEJS=#{ENV["LOCAL_THREEJS"]}",
56
+ "#{go_server_path} > #{go_server_path}.log 2>&1 &"
57
+ ].join(" ")
58
+
55
59
  self.ws_thread = Thread.new do
56
- system("#{go_server_path} > #{go_server_path}.log 2>&1")
60
+ puts "starting go server..."
61
+ puts go_server_run_command.gsub(File.expand_path(Dir.pwd), "")
62
+ FDot::Based::Interface.instance.display_mode = FDot::DISPLAY_MODES[:websockets]
63
+ system(go_server_run_command)
64
+ $stdout.reopen(self.class.path_for_file(:display), "w")
65
+ $stdout.sync = true
66
+ while FDot::Based::Cubicus.is_running? do
67
+ sleep(4)
68
+ unless FDot::Based::Cubicus.is_running?
69
+ puts "restarting in ws_thread..."
70
+ puts go_server_run_command.gsub(File.expand_path(Dir.pwd), "")
71
+ system(go_server_run_command)
72
+ end
73
+ end
57
74
  end
75
+
58
76
  end
59
77
 
60
78
  def self.run_exit_cleanup!
@@ -69,7 +87,7 @@ module FDot
69
87
  end
70
88
 
71
89
  self.write_open_system!
72
- system("pkill f_dot_go") # todo: daemonize
90
+ ps_aux_f_dot_pids.each{|i| puts `kill -3 #{i}`}
73
91
  self.instance.ws_thread&.exit
74
92
  self.instance.ws_thread&.kill
75
93
  self.instance.ws_thread = nil
@@ -78,7 +96,33 @@ module FDot
78
96
  end
79
97
  end
80
98
 
99
+ def self.is_running?
100
+ self.ps_aux_f_dot_pids.count > 1
101
+ end
102
+
103
+ def self.check_projector_input!
104
+ if File.exist?(self.path_for_file(:projector_input))
105
+ raise "missing projector_writeback!" if self.instance.projector_writeback.nil?
106
+ projector_input = File.read(self.path_for_file(:projector_input))
107
+ if !projector_input.nil? &&
108
+ !projector_input.empty? &&
109
+ projector_input.kind_of?(String)
110
+
111
+ self.instance.projector_writeback.call(
112
+ JSON.parse(projector_input)
113
+ )
114
+ FileUtils.rm_rf(self.path_for_file(:projector_input))
115
+ system("curl -s http://localhost:8080/ready")
116
+ end
117
+ end
118
+ end
119
+
81
120
  def self.read_input!
121
+ # clear last projector
122
+ FileUtils.rm_rf(self.path_for_file(:projector))
123
+
124
+ self.check_projector_input!
125
+
82
126
  unless File.exist?(self.path_for_file(:input))
83
127
  raise "invalid #{__method__} file does not exist at #{self.path_for_file(:input)}!"
84
128
  end
@@ -86,7 +130,19 @@ module FDot
86
130
  input = JSON.parse(File.read(self.path_for_file(:input)))
87
131
  raise "invalid input uuid!" unless self.instance.controls.keys.include?(input["uuid"])
88
132
 
89
- self.instance.controls[input["uuid"]][:value]
133
+ control = self.instance.controls[input["uuid"]]
134
+ raise "invalid control! #{input["uuid"]}" if control.nil? || !control.kind_of?(Hash)
135
+
136
+ if control[:type].nil?
137
+ # :name, :value hash
138
+ return self.instance.controls[input["uuid"]][:value]
139
+ else
140
+ # assert input contains "new_value"
141
+ raise "missing new_value for #{control[:name]} control!" if input["new_value"].nil? ||
142
+ !input["new_value"].kind_of?(String)
143
+
144
+ return input["new_value"]
145
+ end
90
146
  end
91
147
 
92
148
  def self.write_open_system!
@@ -109,6 +165,16 @@ module FDot
109
165
  $stdout.reopen(FDot::Based::Cubicus.path_for_file(:display), "w")
110
166
  end
111
167
 
168
+ def self.project_open_system(data, projector_writeback)
169
+ self.instance.projector_writeback = projector_writeback
170
+
171
+ # write current projection data
172
+ File.open(self.path_for_file(:projector), "w") do |f|
173
+ f.write(data.to_json)
174
+ f.fsync
175
+ end
176
+ end
177
+
112
178
  def self.update_with(controls)
113
179
  input_value = nil
114
180
  Thread.new{
@@ -123,6 +189,9 @@ module FDot
123
189
  # wait for user input
124
190
  while !File.exist?(FDot::Based::Cubicus.path_for_file(:input)) ||
125
191
  (File.read(FDot::Based::Cubicus.path_for_file(:input)) || "").empty?
192
+
193
+ FDot::Based::Cubicus.check_projector_input!
194
+ sleep(0.1)
126
195
  end
127
196
 
128
197
  input_value = FDot::Based::Cubicus.read_input!
@@ -133,6 +202,10 @@ module FDot
133
202
 
134
203
  private
135
204
 
205
+ def self.ps_aux_f_dot_pids
206
+ `ps aux | grep f_dot_go | awk '{print $2}'`.split("\n")
207
+ end
208
+
136
209
  def prepare_f_dot_go!
137
210
  go_directory = File.join(
138
211
  FDot.gem_directory, "lib/go"
@@ -140,19 +213,25 @@ module FDot
140
213
  go_file_path = File.join(
141
214
  go_directory, FDot::Based::Cubicus::GO_SERVER_BASENAME
142
215
  )
216
+ puts __method__
217
+ puts "go_directory: #{go_directory}"
218
+ puts "go_file_path: #{go_file_path}"
143
219
 
144
220
  raise "#{go_file_path} is not a valid file path!" unless File.exist?(go_file_path)
145
221
 
222
+ f_dot_space_path_logable = FDot::Based.f_dot_space_path.gsub(
223
+ File.expand_path(Dir.pwd), ""
224
+ )
146
225
  puts "ensure index.html exists in .f_dot directory:"
147
- puts "(#{File.join(go_directory, "index.html")}, #{FDot::Based.f_dot_space_path})"
226
+ puts "(File.join(go_directory, 'index.html'), #{f_dot_space_path_logable})"
148
227
  FileUtils.cp(File.join(go_directory, "index.html"), FDot::Based.f_dot_space_path)
149
228
 
150
229
  puts "ensure .f_dot/javascript directory exists:"
151
- puts "(#{File.join(go_directory, "javascript")}, #{FDot::Based.f_dot_space_path})"
230
+ puts "(File.join(go_directory, 'javascript'), #{f_dot_space_path_logable})"
152
231
  FileUtils.cp_r(File.join(go_directory, "javascript"), FDot::Based.f_dot_space_path)
153
232
 
154
233
  puts "ensure .f_dot/stylesheets directory exists:"
155
- puts "(#{File.join(go_directory, "stylesheets")}, #{FDot::Based.f_dot_space_path})"
234
+ puts "(File.join(go_directory, 'stylesheets'), #{f_dot_space_path_logable})"
156
235
  FileUtils.cp_r(File.join(go_directory, "stylesheets"), FDot::Based.f_dot_space_path)
157
236
 
158
237
  if File.exist?(self.class.path_for_file(:f_dot_go))
@@ -160,7 +239,8 @@ module FDot
160
239
  FileUtils.rm_rf("#{self.class.path_for_file(:f_dot_go)}.log")
161
240
  end
162
241
 
163
- puts "system(go build -o #{self.class.path_for_file(:f_dot_go)} #{go_file_path})"
242
+ puts "go build"
243
+ puts "binary output file: #{f_dot_space_path_logable}/f_dot_go"
164
244
  system("go build -C #{go_directory} -o #{self.class.path_for_file(:f_dot_go)} #{go_file_path}")
165
245
 
166
246
  raise "failed to build #{self.class.path_for_file(:f_dot_go)}!" unless File.exist?(self.class.path_for_file(:f_dot_go))
data/lib/f_dot/version.rb CHANGED
@@ -1,10 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FDot
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  RELEASE_NOTES = [
6
- :adds_cubicus,
7
- :adds_f_dot_go,
8
- :adds_display_modes
9
6
  ].join(" | ")
10
7
  end
data/lib/go/f_dot.go CHANGED
@@ -16,15 +16,11 @@ import (
16
16
  "strings"
17
17
  // "time"
18
18
  "path"
19
- "sync"
20
19
  "path/filepath"
20
+ "sync"
21
21
  "github.com/gorilla/websocket"
22
- "golang.org/x/net/http2"
23
- "golang.org/x/net/http2/h2c"
24
22
  )
25
23
 
26
- var port = "8080"
27
-
28
24
  var upgrader = websocket.Upgrader{
29
25
  CheckOrigin: func(*http.Request) bool {
30
26
  return true
@@ -35,6 +31,8 @@ var indexHtmlTemplate = template.Must(template.New("").Parse("<html></html>"))
35
31
 
36
32
  var displayFileName = "display"
37
33
  var controlsFileName = "controls"
34
+ var projectorFileName = "projector"
35
+ var projectorInputFileName = "projector_input"
38
36
  var inputFileName = "input"
39
37
  var connManager = newConnManager()
40
38
 
@@ -79,8 +77,8 @@ func writeDisplay(displayFileData []byte) {
79
77
  connManager.broadcast(displayFileData)
80
78
  }
81
79
 
82
- func render(w http.ResponseWriter, req *http.Request) {
83
- log.Print("starting render:", req)
80
+ func renderWebsockets(w http.ResponseWriter, req *http.Request) {
81
+ log.Print("starting renderWebsockets:", req)
84
82
  c, err := upgrader.Upgrade(w, req, nil)
85
83
  if err != nil {
86
84
  log.Print("upgrade:", err)
@@ -141,8 +139,14 @@ func render(w http.ResponseWriter, req *http.Request) {
141
139
  for {
142
140
  select {
143
141
  case msg := <-messageChan:
144
- log.Print("writing input ", msg.message)
145
- os.WriteFile(pathForFilename(inputFileName), []byte(msg.message), 0644)
142
+ var inputFileToWrite = pathForFilename(inputFileName)
143
+ log.Print("checking input type...")
144
+ log.Print("is for projector input? ", strings.Contains(string(msg.message), "\"position\":{"))
145
+ if strings.Contains(string(msg.message), "\"position\":{") {
146
+ inputFileToWrite = pathForFilename(projectorInputFileName)
147
+ }
148
+ log.Print("writing input to ", inputFileToWrite)
149
+ os.WriteFile(inputFileToWrite, []byte(msg.message), 0644)
146
150
  if err != nil {
147
151
  log.Fatal(err)
148
152
  }
@@ -151,32 +155,36 @@ func render(w http.ResponseWriter, req *http.Request) {
151
155
  }
152
156
 
153
157
  func html(w http.ResponseWriter, req *http.Request) {
154
- indexHtmlTemplate.Execute(w, struct{}{})
158
+ log.Print("serve html:", req.URL.Path)
159
+ indexHtmlTemplate.Execute(w, struct {}{})
155
160
  }
156
161
 
157
162
  func javascript(w http.ResponseWriter, req *http.Request) {
163
+ log.Print("serve javascript:", req.URL.Path)
158
164
  fileName := path.Base(req.URL.Path)
159
165
  w.Header().Set("Content-Type", "application/javascript; charset=UTF-8")
160
166
 
161
167
  http.ServeFile(w, req, pathForFilename("javascript/"+fileName))
162
- // log.Print("rendering javascript: ", fileName)
163
- // fileTemplate := template.Must(template.ParseFiles(pathForFilename("javascript/"+fileName)))
164
-
165
- // err := fileTemplate.Execute(w, struct{}{})
166
-
167
- // if err != nil {
168
- // log.Println("error:", err)
169
- // http.Error(w, "template parse error", http.StatusInternalServerError)
170
- // return
171
- // }
172
168
  }
173
169
 
174
170
  func stylesheets(w http.ResponseWriter, req *http.Request) {
171
+ log.Print("serve stylesheets:", req.URL.Path)
175
172
  fileName := path.Base(req.URL.Path)
176
173
  w.Header().Set("Content-Type", "text/css; charset=UTF-8")
177
174
 
178
175
  http.ServeFile(w, req, pathForFilename("stylesheets/"+fileName))
176
+ }
179
177
 
178
+ func canvas(w http.ResponseWriter, req *http.Request) {
179
+ log.Print("serve canvas:", req.URL.Path)
180
+ parts := strings.Split(req.URL.Path, "/canvas/")
181
+ if len(parts) > 0 {
182
+ last := parts[len(parts)-1]
183
+ w.Header().Set("Content-Type", "application/javascript; charset=UTF-8")
184
+ http.ServeFile(w, req, pathForCanvasRequest(last))
185
+ } else {
186
+ panic("invalid canvas request!")
187
+ }
180
188
  }
181
189
 
182
190
  func ready() {
@@ -196,13 +204,44 @@ func ready() {
196
204
  } else {
197
205
  connManager.broadcast([]byte(controlsFileData))
198
206
  }
207
+
208
+ // send projector file
209
+ projectorFileData, err := os.ReadFile(pathForFilename(projectorFileName))
210
+ if err != nil {
211
+ log.Print("failed to read:", projectorFileName)
212
+ } else {
213
+ connManager.broadcast([]byte(projectorFileData))
214
+ }
199
215
  }
200
216
 
201
- func handler(wr http.ResponseWriter, req *http.Request) {
217
+ func pathForFilename(f string) string {
218
+ path, err := os.Executable()
219
+ if err != nil {
220
+ log.Fatal(err)
221
+ }
222
+
223
+ return filepath.Join(filepath.Dir(path), f)
224
+ }
225
+
226
+ func pathForCanvasRequest(path string) string {
227
+ return filepath.Join(ensureLocalThreejs(), path)
228
+ }
229
+
230
+ func ensureLocalThreejs() string {
231
+ local_threejs, exists := os.LookupEnv("LOCAL_THREEJS")
232
+ if !exists {
233
+ panic("LOCAL_THREEJS is not set")
234
+ }
235
+
236
+ return local_threejs
237
+ }
238
+
239
+ // router
240
+ func router(wr http.ResponseWriter, req *http.Request) {
202
241
  defer req.Body.Close()
203
242
 
204
243
  if websocket.IsWebSocketUpgrade(req) {
205
- render(wr, req)
244
+ renderWebsockets(wr, req)
206
245
  } else if req.URL.Path == "/ready" {
207
246
  ready()
208
247
  } else if req.URL.Path == "/" {
@@ -211,33 +250,19 @@ func handler(wr http.ResponseWriter, req *http.Request) {
211
250
  javascript(wr, req)
212
251
  } else if strings.Contains(req.URL.Path, "/stylesheets/") {
213
252
  stylesheets(wr, req)
253
+ } else if strings.Contains(req.URL.Path, "/canvas/") {
254
+ canvas(wr, req)
214
255
  }
215
256
  }
216
257
 
217
- func pathForFilename(f string) string {
218
- path, err := os.Executable()
219
- if err != nil {
220
- log.Fatal(err)
221
- }
222
- log.Print("running in ", path)
223
-
224
- return filepath.Dir(path)+"/"+f
225
- }
226
-
227
- func serveJavascript() {
228
- mux := http.NewServeMux()
229
- // Serve everything inside ./javascript under the /javascript/ URL path
230
- fs := http.FileServer(http.Dir("./javascript"))
231
- mux.Handle("/javascript/", http.StripPrefix("/javascript/", fs))
232
- }
233
-
234
258
  func main() {
235
259
  indexHtmlTemplate = template.Must(template.ParseFiles(pathForFilename("index.html")))
236
- http.ListenAndServe(
237
- "0.0.0.0:"+port,
238
- h2c.NewHandler(
239
- http.HandlerFunc(handler),
240
- &http2.Server{},
241
- ),
242
- )
260
+ ensureLocalThreejs()
261
+ localhostOnly, exists := os.LookupEnv("LOCALHOST_ONLY")
262
+ var serveUrl = "127.0.0.1:8080"
263
+ if exists && localhostOnly != "true" {
264
+ serveUrl = ":8080"
265
+ }
266
+
267
+ log.Fatal(http.ListenAndServe(serveUrl, http.HandlerFunc(router)))
243
268
  }
data/lib/go/index.html CHANGED
@@ -1,18 +1,36 @@
1
1
  <!doctype html>
2
2
  <html lang="en">
3
-
4
3
  <head>
5
4
  <meta charset="UTF-8">
6
5
  <title>f_dot</title>
7
- <script type="module" src="javascript/entry.js"></script>
8
6
  <link rel="stylesheet" href="stylesheets/f_dot_go.css">
7
+ <script type="importmap">
8
+ {
9
+ "imports": {
10
+ "three": "/canvas/build/three.module.js",
11
+ "three/addons/": "/canvas/examples/jsm/"
12
+ }
13
+ }
14
+ </script>
15
+ <script type="module" src="javascript/entry.js"></script>
9
16
  </head>
10
-
11
17
  <body>
18
+ <div id="f_dot_menu_container" class="hidden">
19
+ <a id="f_dot_link" class="baton" style="display: none;">f_dot</a>
20
+ <a id="projector_link">projector</a>
21
+ <a id="editor_link">editor</a>
22
+ <a id="history_link">history</a>
23
+ </div>
12
24
  <div id="f_dot_menu_button">&int; ( &#120097; &#120108; &#120113; )</div>
13
- <canvas id="gl-canvas" width="640" height="480"></canvas>
14
- <pre id="display"></pre>
15
- <div id="controls"></div>
25
+ <div id="canvas" style="display: none;">
26
+ </div>
27
+ <div id="f_dot_contents">
28
+ <pre id="display"></pre>
29
+ <div id="browser_container" style="display: none;">
30
+ <iframe id="browser" sandbox="allow-scripts allow-same-origin allow-forms"></iframe>
31
+ </div>
32
+ <div id="controls"></div>
33
+ </div>
16
34
  </body>
17
35
 
18
36
  </html>