procon_bypass_man 0.1.11 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/release.yml +33 -0
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +13 -0
  5. data/Gemfile +1 -1
  6. data/Gemfile.lock +14 -12
  7. data/README.md +11 -7
  8. data/bin/dev_api_server.rb +1 -1
  9. data/lib/procon_bypass_man/background/job_performer.rb +16 -0
  10. data/lib/procon_bypass_man/background/{report_thread.rb → job_runner.rb} +13 -10
  11. data/lib/procon_bypass_man/background/jobs/base_job.rb +7 -0
  12. data/lib/procon_bypass_man/background/jobs/concerns/has_external_api_setting.rb +5 -0
  13. data/lib/procon_bypass_man/background/jobs/concerns/has_internal_api_setting.rb +5 -0
  14. data/lib/procon_bypass_man/background/jobs/concerns/job_runnable.rb +16 -0
  15. data/lib/procon_bypass_man/background/jobs/fetch_and_run_remote_pbm_action_job.rb +29 -0
  16. data/lib/procon_bypass_man/background/jobs/report_boot_job.rb +12 -0
  17. data/lib/procon_bypass_man/background/jobs/report_error_job.rb +11 -0
  18. data/lib/procon_bypass_man/background/jobs/report_event_base_job.rb +5 -0
  19. data/lib/procon_bypass_man/background/jobs/report_load_config_job.rb +11 -0
  20. data/lib/procon_bypass_man/background/jobs/report_pressed_buttons_job.rb +15 -0
  21. data/lib/procon_bypass_man/background/jobs/report_reload_config_job.rb +11 -0
  22. data/lib/procon_bypass_man/background/jobs/sync_device_stats_job.rb +16 -0
  23. data/lib/procon_bypass_man/background.rb +14 -0
  24. data/lib/procon_bypass_man/boot_message.rb +16 -8
  25. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/layer.rb +34 -29
  26. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/loader.rb +5 -4
  27. data/lib/procon_bypass_man/{configuration → buttons_setting_configuration}/validator.rb +0 -0
  28. data/lib/procon_bypass_man/buttons_setting_configuration.rb +6 -7
  29. data/lib/procon_bypass_man/bypass/usb_hid_logger.rb +18 -7
  30. data/lib/procon_bypass_man/bypass.rb +15 -14
  31. data/lib/procon_bypass_man/commands/bypass_command.rb +86 -0
  32. data/lib/procon_bypass_man/commands/connect_device_command.rb +16 -0
  33. data/lib/procon_bypass_man/commands/print_boot_message_command.rb +9 -0
  34. data/lib/procon_bypass_man/commands/run_local_shell_command.rb +6 -0
  35. data/lib/procon_bypass_man/commands/run_remote_pbm_action_dispatch_command.rb +21 -0
  36. data/lib/procon_bypass_man/commands/send_error_command.rb +19 -0
  37. data/lib/procon_bypass_man/commands/send_reload_config_event_command.rb +11 -0
  38. data/lib/procon_bypass_man/commands/write_device_id_command.rb +12 -0
  39. data/lib/procon_bypass_man/commands/write_session_id_command.rb +7 -0
  40. data/lib/procon_bypass_man/commands.rb +8 -0
  41. data/lib/procon_bypass_man/configuration.rb +47 -6
  42. data/lib/procon_bypass_man/device_connector.rb +33 -26
  43. data/lib/procon_bypass_man/device_status.rb +44 -0
  44. data/lib/procon_bypass_man/io_monitor.rb +9 -4
  45. data/lib/procon_bypass_man/plugin/splatoon2/macro/fast_return.rb +17 -0
  46. data/lib/procon_bypass_man/plugin/splatoon2/macro/jump_to_left_key.rb +17 -0
  47. data/lib/procon_bypass_man/plugin/splatoon2/macro/jump_to_right_key.rb +17 -0
  48. data/lib/procon_bypass_man/plugin/splatoon2/macro/jump_to_up_key.rb +17 -0
  49. data/lib/procon_bypass_man/plugin/splatoon2/mode/guruguru.rb +59 -0
  50. data/lib/procon_bypass_man/plugin/splatoon2/version.rb +9 -0
  51. data/lib/procon_bypass_man/plugin.rb +11 -0
  52. data/lib/procon_bypass_man/procon/analog_stick_cap.rb +1 -1
  53. data/lib/procon_bypass_man/procon/{data.rb → consts.rb} +1 -1
  54. data/lib/procon_bypass_man/procon/layer_changer.rb +40 -0
  55. data/lib/procon_bypass_man/procon/user_operation.rb +11 -17
  56. data/lib/procon_bypass_man/procon.rb +6 -12
  57. data/lib/procon_bypass_man/{readonly_procon.rb → procon_reader.rb} +3 -4
  58. data/lib/procon_bypass_man/remote_pbm_action/base_action.rb +53 -0
  59. data/lib/procon_bypass_man/remote_pbm_action/change_pbm_version_action.rb +25 -0
  60. data/lib/procon_bypass_man/remote_pbm_action/lib/update_remote_pbm_action_status_command.rb +24 -0
  61. data/lib/procon_bypass_man/remote_pbm_action/reboot_os_action.rb +21 -0
  62. data/lib/procon_bypass_man/remote_pbm_action/restore_pbm_setting.rb +28 -0
  63. data/lib/procon_bypass_man/remote_pbm_action/stop_pbm_action.rb +21 -0
  64. data/lib/procon_bypass_man/remote_pbm_action.rb +32 -0
  65. data/lib/procon_bypass_man/runner.rb +14 -115
  66. data/lib/procon_bypass_man/scheduler.rb +92 -0
  67. data/lib/procon_bypass_man/{callbacks.rb → support/callbacks.rb} +0 -0
  68. data/lib/procon_bypass_man/support/compress_array.rb +56 -0
  69. data/lib/procon_bypass_man/support/http_client.rb +102 -0
  70. data/lib/procon_bypass_man/{on_memory_cache.rb → support/on_memory_cache.rb} +0 -0
  71. data/lib/procon_bypass_man/support/report_http_client.rb +19 -0
  72. data/lib/procon_bypass_man/{timer.rb → support/safe_timeout.rb} +1 -1
  73. data/lib/procon_bypass_man/support/send_device_stats_http_client.rb +9 -0
  74. data/lib/procon_bypass_man/support/server_pool.rb +42 -0
  75. data/lib/procon_bypass_man/support/signal_handler.rb +11 -0
  76. data/lib/procon_bypass_man/support/update_remote_pbm_action_status_http_client.rb +9 -0
  77. data/lib/procon_bypass_man/support/uptime.rb +25 -0
  78. data/lib/procon_bypass_man/value_objects/remote_pbm_action_object.rb +38 -0
  79. data/lib/procon_bypass_man/version.rb +1 -1
  80. data/lib/procon_bypass_man.rb +71 -36
  81. data/procon_bypass_man.gemspec +3 -3
  82. data/project_template/README.md +18 -11
  83. data/project_template/app.rb +2 -3
  84. data/project_template/setting.yml +8 -8
  85. data/sig/{README.rb → README.md} +0 -0
  86. data/sig/main.rbs +10 -11
  87. metadata +85 -26
  88. data/lib/procon_bypass_man/outbound/base.rb +0 -53
  89. data/lib/procon_bypass_man/outbound/error_reporter.rb +0 -13
  90. data/lib/procon_bypass_man/outbound/pressed_buttons_reporter.rb +0 -13
  91. data/lib/procon_bypass_man/outbound/reporter.rb +0 -12
  92. data/lib/procon_bypass_man/procon/layer_changeable.rb +0 -28
  93. data/lib/procon_bypass_man/procon/pressed_button_helper.rb +0 -15
  94. data/lib/procon_bypass_man/uptime.rb +0 -15
@@ -0,0 +1,53 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class ActionUnexpectedError < StandardError; end
4
+ class NeedPbmVersionError < ActionUnexpectedError; end
5
+
6
+ class BaseAction
7
+ attr_accessor :pbm_job_uuid
8
+
9
+ # @param [String] pbm_job_uuid
10
+ def initialize(pbm_job_uuid: )
11
+ self.pbm_job_uuid = pbm_job_uuid
12
+ end
13
+
14
+ # @return [void]
15
+ def action_content(_args)
16
+ raise NotImplementedError, nil
17
+ end
18
+
19
+ # @param [Hash] args
20
+ # @return [void]
21
+ def run!(job_args: )
22
+ before_action_callback
23
+ action_content(args: job_args)
24
+ after_action_callback
25
+ rescue => e
26
+ be_failed
27
+ ProconBypassMan::SendErrorCommand.execute(error: e)
28
+ end
29
+
30
+ private
31
+
32
+ # @return [void]
33
+ def before_action_callback; end
34
+ # @return [void]
35
+ def after_action_callback; end
36
+
37
+ # @return [void]
38
+ def be_failed
39
+ ProconBypassMan::UpdateRemotePbmActionStatusCommand.new(pbm_job_uuid: pbm_job_uuid).execute(to_status: :failed)
40
+ end
41
+
42
+ # @return [void]
43
+ def be_in_progress
44
+ ProconBypassMan::UpdateRemotePbmActionStatusCommand.new(pbm_job_uuid: pbm_job_uuid).execute(to_status: :in_progress)
45
+ end
46
+
47
+ # @return [void]
48
+ def be_processed
49
+ ProconBypassMan::UpdateRemotePbmActionStatusCommand.new(pbm_job_uuid: pbm_job_uuid).execute(to_status: :processed)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,25 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class ChangePbmVersionAction < BaseAction
4
+
5
+ def action_content(args: )
6
+ require "pbmenv"
7
+ ProconBypassMan.logger.info "execute ChangePbmVersionAction!"
8
+ pbm_version = args["pbm_version"] or raise(ProconBypassMan::RemotePbmAction::NeedPbmVersionError, "pbm_versionが必要です, #{args.inspect}")
9
+ Pbmenv.install(pbm_version)
10
+ Pbmenv.use(pbm_version)
11
+ `reboot` # symlinkの参照先が変わるのでrebootする必要がある
12
+ end
13
+
14
+ private
15
+
16
+ def before_action_callback
17
+ be_processed
18
+ end
19
+
20
+ def after_action_callback
21
+ # no-op
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module ProconBypassMan
2
+ class UpdateRemotePbmActionStatusCommand
3
+ # @param [String] pbm_job_uuid
4
+ def initialize(pbm_job_uuid: )
5
+ @pbm_job_uuid = pbm_job_uuid
6
+ end
7
+
8
+ # @param [String] to_status
9
+ # @return [void]
10
+ def execute(to_status: )
11
+ ProconBypassMan::UpdateRemotePbmActionStatusHttpClient.new(
12
+ path: path,
13
+ server_pool: ProconBypassMan.config.server_pool,
14
+ ).put(to_status: to_status)
15
+ end
16
+
17
+ private
18
+
19
+ # @return [String]
20
+ def path
21
+ "/api/devices/#{ProconBypassMan.device_id}/pbm_jobs/#{@pbm_job_uuid}"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class RebootOsAction < BaseAction
4
+
5
+ def action_content(_args)
6
+ ProconBypassMan.logger.info "execute RebootOsAction!"
7
+ `reboot`
8
+ end
9
+
10
+ private
11
+
12
+ def before_action_callback
13
+ be_processed
14
+ end
15
+
16
+ def after_action_callback
17
+ # no-op
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class RestorePbmSettingAction < BaseAction
4
+
5
+ def action_content(args: )
6
+ require "pbmenv"
7
+ ProconBypassMan.logger.info "execute RestorePbmSettingAction!"
8
+ setting = args["setting"] or raise(ProconBypassMan::RemotePbmAction::NeedPbmVersionError, "settingが必要です, #{args.inspect}")
9
+ File.write(
10
+ ProconBypassMan::ButtonsSettingConfiguration.instance.setting_path,
11
+ setting.to_yaml,
12
+ )
13
+ ProconBypassMan.hot_reload!
14
+ end
15
+
16
+ private
17
+
18
+ def before_action_callback
19
+ be_in_progress
20
+ end
21
+
22
+ def after_action_callback
23
+ be_processed
24
+ end
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,21 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ class StopPbmAction < BaseAction
4
+
5
+ def action_content(_args)
6
+ ProconBypassMan.logger.info "execute StopPbmAction!"
7
+ Process.kill("TERM", ProconBypassMan.pid)
8
+ end
9
+
10
+ private
11
+
12
+ def before_action_callback
13
+ be_processed
14
+ end
15
+
16
+ def after_action_callback
17
+ # no-op
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module ProconBypassMan
2
+ module RemotePbmAction
3
+ require "procon_bypass_man/remote_pbm_action/base_action"
4
+ require "procon_bypass_man/remote_pbm_action/change_pbm_version_action"
5
+ require "procon_bypass_man/remote_pbm_action/reboot_os_action"
6
+ require "procon_bypass_man/remote_pbm_action/stop_pbm_action"
7
+ require "procon_bypass_man/remote_pbm_action/restore_pbm_setting.rb"
8
+ require "procon_bypass_man/remote_pbm_action/lib/update_remote_pbm_action_status_command"
9
+
10
+ ACTION_CHANGE_PBM_VERSION = "change_pbm_version"
11
+ ACTION_REBOOT_OS = "reboot_os"
12
+ ACTION_STOP_PBM = "stop_pbm"
13
+ ACTION_RESTORE_SETTING = "restore_pbm_setting"
14
+
15
+ ACTIONS = [
16
+ ACTION_CHANGE_PBM_VERSION,
17
+ ACTION_REBOOT_OS,
18
+ ACTION_STOP_PBM,
19
+ ACTION_RESTORE_SETTING,
20
+ ]
21
+
22
+ STATUS_FAILED = :failed
23
+ STATUS_IN_PROGRESS = :in_progress
24
+ STATUS_PROCESSED = :processed
25
+
26
+ ACTION_STATUSES = [
27
+ STATUS_FAILED,
28
+ STATUS_IN_PROGRESS,
29
+ STATUS_PROCESSED,
30
+ ]
31
+ end
32
+ end
@@ -1,15 +1,19 @@
1
1
  require_relative "io_monitor"
2
- require_relative "uptime"
3
2
  require_relative "boot_message"
4
- require_relative "background/report_thread"
5
3
 
6
4
  class ProconBypassMan::Runner
7
5
  class InterruptForRestart < StandardError; end
8
6
 
9
- def run
10
- first_negotiation
11
- print_booted_message
7
+ include ProconBypassMan::SignalHandler
8
+
9
+ def initialize(gadget: , procon: )
10
+ @gadget = gadget
11
+ @procon = procon
12
12
 
13
+ ProconBypassMan::PrintBootMessageCommand.execute
14
+ end
15
+
16
+ def run
13
17
  self_read, self_write = IO.pipe
14
18
  %w(TERM INT USR1 USR2).each do |sig|
15
19
  begin
@@ -17,13 +21,14 @@ class ProconBypassMan::Runner
17
21
  self_write.puts(sig)
18
22
  end
19
23
  rescue ArgumentError
20
- ProconBypassMan.logger.error("Signal #{sig} not supported")
24
+ ProconBypassMan::SendErrorCommand.execute(error: "Signal #{sig} not supported")
21
25
  end
22
26
  end
23
27
 
24
28
  loop do
25
29
  $will_terminate_token = false
26
- main_loop_pid = fork { main_loop }
30
+ # NOTE メインプロセスではThreadをいくつか起動しているので念のためパフォーマンスを優先するためにforkしていく
31
+ main_loop_pid = Kernel.fork { ProconBypassMan::BypassCommand.new(gadget: @gadget, procon: @procon).execute }
27
32
 
28
33
  begin
29
34
  while(readable_io = IO.select([self_read]))
@@ -37,9 +42,9 @@ class ProconBypassMan::Runner
37
42
  ProconBypassMan.logger.info("Reloading config file")
38
43
  begin
39
44
  ProconBypassMan::ButtonsSettingConfiguration::Loader.reload_setting
40
- puts "設定ファイルの再読み込みができました"
45
+ ProconBypassMan::SendReloadConfigEventCommand.execute
41
46
  rescue ProconBypassMan::CouldNotLoadConfigError
42
- ProconBypassMan.logger.error "設定ファイルが不正です。再読み込みができませんでした"
47
+ ProconBypassMan::SendErrorCommand.execute(error: "設定ファイルが不正です。再読み込みができませんでした")
43
48
  end
44
49
  ProconBypassMan.logger.info("バイパス処理を再開します")
45
50
  rescue Interrupt
@@ -54,110 +59,4 @@ class ProconBypassMan::Runner
54
59
  end
55
60
  end
56
61
  end
57
-
58
- private
59
-
60
- def main_loop
61
- ProconBypassMan::IOMonitor.start!
62
- ProconBypassMan::Background::Reporter.start!
63
-
64
- # gadget => procon
65
- # 遅くていい
66
- monitor1 = ProconBypassMan::IOMonitor.new(label: "switch -> procon")
67
- monitor2 = ProconBypassMan::IOMonitor.new(label: "procon -> switch")
68
- ProconBypassMan.logger.info "Thread1を起動します"
69
- t1 = Thread.new do
70
- timer = ProconBypassMan::Timer.new(timeout: Time.now + 10)
71
- bypass = ProconBypassMan::Bypass.new(gadget: @gadget, procon: @procon, monitor: monitor1)
72
- loop do
73
- break if $will_terminate_token
74
- timer.throw_if_timeout!
75
- bypass.send_gadget_to_procon!
76
- sleep(0.005)
77
- rescue ProconBypassMan::Timer::Timeout
78
- ProconBypassMan.logger.info "10秒経過したのでThread1を終了します"
79
- puts "10秒経過したのでThread1を終了します"
80
- break
81
- rescue Errno::EIO, Errno::ENODEV, Errno::EPROTO, IOError => e
82
- ProconBypassMan.logger.error "Proconが切断されました.終了処理を開始します"
83
- Process.kill "TERM", Process.ppid
84
- rescue Errno::ETIMEDOUT => e
85
- # TODO まれにこれが発生する. 再接続したい
86
- ProconBypassMan::ErrorReporter.report(body: e)
87
- ProconBypassMan.logger.error "Switchとの切断されました.終了処理を開始します"
88
- Process.kill "TERM", Process.ppid
89
- end
90
- ProconBypassMan.logger.info "Thread1を終了します"
91
- end
92
-
93
- # procon => gadget
94
- # シビア
95
- ProconBypassMan.logger.info "Thread2を起動します"
96
- t2 = Thread.new do
97
- bypass = ProconBypassMan::Bypass.new(gadget: @gadget, procon: @procon, monitor: monitor2)
98
- loop do
99
- break if $will_terminate_token
100
- bypass.send_procon_to_gadget!
101
- rescue EOFError => e
102
- ProconBypassMan.logger.error "Proconと通信ができませんでした.終了処理を開始します"
103
- Process.kill "TERM", Process.ppid
104
- rescue Errno::EIO, Errno::ENODEV, Errno::EPROTO, IOError => e
105
- ProconBypassMan.logger.error "Proconが切断されました。終了処理を開始します"
106
- Process.kill "TERM", Process.ppid
107
- end
108
- ProconBypassMan.logger.info "Thread2を終了します"
109
- end
110
-
111
- self_read, self_write = IO.pipe
112
- %w(TERM INT).each do |sig|
113
- begin
114
- trap sig do
115
- self_write.puts(sig)
116
- end
117
- rescue ArgumentError
118
- puts "プロセスでSignal #{sig} not supported"
119
- end
120
- end
121
-
122
- ProconBypassMan.logger.info "子プロセスでgraceful shutdownの準備ができました"
123
- begin
124
- while(readable_io = IO.select([self_read]))
125
- signal = readable_io.first[0].gets.strip
126
- handle_signal(signal)
127
- end
128
- rescue Interrupt
129
- $will_terminate_token = true
130
- [t1, t2].each(&:join)
131
- @gadget&.close
132
- @procon&.close
133
- exit 1
134
- end
135
- end
136
-
137
- def first_negotiation
138
- @gadget, @procon = ProconBypassMan::DeviceConnector.connect
139
- rescue ProconBypassMan::Timer::Timeout
140
- ::ProconBypassMan.logger.error "デバイスとの通信でタイムアウトが起きて接続ができませんでした。"
141
- @gadget&.close
142
- @procon&.close
143
- raise ::ProconBypassMan::EternalConnectionError
144
- end
145
-
146
- def handle_signal(sig)
147
- ProconBypassMan.logger.info "#{$$}で#{sig}を受け取りました"
148
- case sig
149
- when 'USR2'
150
- raise InterruptForRestart
151
- when 'INT', 'TERM'
152
- raise Interrupt
153
- end
154
- end
155
-
156
- # @return [void]
157
- def print_booted_message
158
- message = ProconBypassMan::BootMessage.new
159
- ProconBypassMan.logger.info(message.to_s)
160
- Thread.new { ProconBypassMan::Reporter.report(body: message.to_hash) }
161
- puts message.to_s
162
- end
163
62
  end
@@ -0,0 +1,92 @@
1
+ module ProconBypassMan
2
+ class Scheduler
3
+ class Schedule
4
+ attr_accessor :klass, :args, :interval, :next_enqueue_at
5
+
6
+ # @param [any] klass
7
+ # @param [Array] args
8
+ # @param [Integer] interval
9
+ def initialize(klass: , args: , interval: )
10
+ self.klass = klass
11
+ self.args = args
12
+ self.interval = interval
13
+ self.next_enqueue_at = Time.now
14
+ end
15
+
16
+ # @return [void]
17
+ def enqueue
18
+ klass.perform_async(*unwrap_args(args))
19
+ set_next_enqueue_at!
20
+ end
21
+
22
+ # @return [boolean]
23
+ def past_interval?
24
+ next_enqueue_at < Time.now
25
+ end
26
+
27
+ private
28
+
29
+ # @return [void]
30
+ def set_next_enqueue_at!
31
+ self.next_enqueue_at = Time.now + interval
32
+ end
33
+
34
+ # @param [Array] args
35
+ # @return [void]
36
+ def unwrap_args(args)
37
+ args.map do |arg|
38
+ case arg
39
+ when Proc
40
+ arg.call
41
+ else
42
+ arg
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ # @return [Hash]
49
+ def self.schedules
50
+ @@schedules
51
+ end
52
+
53
+ @@schedules = {}
54
+
55
+ def self.start!
56
+ register_schedules
57
+
58
+ @@thread = Thread.start do
59
+ loop do
60
+ schedules.each do |_key, schedule|
61
+ if schedule.past_interval?
62
+ schedule.enqueue
63
+ end
64
+ end
65
+ sleep 10
66
+ end
67
+ end
68
+ end
69
+
70
+ def self.register_schedules
71
+ register(
72
+ schedule: Schedule.new(
73
+ klass: ProconBypassMan::FetchAndRunRemotePbmActionJob,
74
+ args: [],
75
+ interval: 3,
76
+ )
77
+ )
78
+ register(
79
+ schedule: Schedule.new(
80
+ klass: ProconBypassMan::SyncDeviceStatsJob,
81
+ args: [->{ ProconBypassMan::DeviceStatus.current }],
82
+ interval: 60,
83
+ )
84
+ )
85
+ end
86
+
87
+ # @param [Schedule] schedule
88
+ def self.register(schedule: )
89
+ schedules[schedule.klass] = schedule
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,56 @@
1
+ module ProconBypassMan
2
+ class CompressArray
3
+ class CompressibleValue
4
+ # @params [String] prev
5
+ # @params [String] current
6
+ def initialize(prev, current)
7
+ @prev = prev
8
+ @current = current
9
+ end
10
+
11
+ # @return [Boolean]
12
+ def compress?
13
+ @prev.include?(@current)
14
+ end
15
+
16
+ # @return [String]
17
+ def to_s_with_mark
18
+ if /^(.+) \* (\d+)/ =~ @prev
19
+ value = $1
20
+ count = $2
21
+ return "#{value} * #{count.to_i + 1}"
22
+ end
23
+ if /^(.+)/ =~ @prev
24
+ value = $1
25
+ return "#{value} * 1"
26
+ end
27
+ end
28
+ end
29
+
30
+ def initialize(array)
31
+ @array = array
32
+ end
33
+
34
+ # @return [Array<String>]
35
+ def compress
36
+ previous_value = nil
37
+ @array.reduce([]) do |acc, item|
38
+ if previous_value.nil?
39
+ acc << item
40
+ previous_value = item
41
+ next acc
42
+ end
43
+
44
+ if CompressibleValue.new(previous_value, item).compress?
45
+ registered_value = acc.pop
46
+ acc << CompressibleValue.new(registered_value, item).to_s_with_mark
47
+ else
48
+ acc << item
49
+ end
50
+
51
+ previous_value = item
52
+ next acc
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,102 @@
1
+ module ProconBypassMan
2
+ class HttpClient
3
+ class HttpRequest
4
+ class Get
5
+ def self.request!(uri: )
6
+ http = Net::HTTP.new(uri.host, uri.port)
7
+ http.use_ssl = uri.scheme === "https"
8
+ http.get(uri.path, { "Content-Type" => "application/json" })
9
+ end
10
+ end
11
+
12
+ class Post
13
+ def self.request!(uri: , request_body: {})
14
+ http = Net::HTTP.new(uri.host, uri.port)
15
+ http.use_ssl = uri.scheme === "https"
16
+ http.post(uri.path, request_body.to_json, { "Content-Type" => "application/json" })
17
+ end
18
+ end
19
+
20
+ class Put
21
+ def self.request!(uri: , request_body: {})
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ http.use_ssl = uri.scheme === "https"
24
+ http.put(uri.path, request_body.to_json, { "Content-Type" => "application/json" })
25
+ end
26
+ end
27
+ end
28
+
29
+ def initialize(path: , server_pool: , retry_on_connection_error: false)
30
+ @server_pool = server_pool
31
+ @uri = URI.parse("#{server_pool.server}#{path}")
32
+ @retry_on_connection_error = retry_on_connection_error
33
+ end
34
+
35
+ def get
36
+ handle_request do
37
+ response = HttpRequest::Get.request!(
38
+ uri: @uri,
39
+ )
40
+ break process_response(response)
41
+ end
42
+ end
43
+
44
+ def post(request_body: )
45
+ handle_request do
46
+ body = {}.merge!(request_body)
47
+ response = HttpRequest::Post.request!(
48
+ uri: @uri,
49
+ request_body: body,
50
+ )
51
+ break process_response(response)
52
+ end
53
+ end
54
+
55
+ def put(request_body: )
56
+ handle_request do
57
+ body = {
58
+ hostname: `hostname`.chomp,
59
+ }.merge!(request_body)
60
+ response = HttpRequest::Put.request!(
61
+ uri: @uri,
62
+ request_body: body,
63
+ )
64
+ break process_response(response)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ def process_response(response)
71
+ case response.code
72
+ when /^200/
73
+ begin
74
+ return JSON.parse(response.body)
75
+ rescue JSON::ParserError
76
+ return response.body
77
+ end
78
+ else
79
+ @server_pool.next!
80
+ ProconBypassMan.logger.error("#{@uri}から200以外(#{response.code})が帰ってきました. #{response.body}")
81
+ end
82
+ end
83
+
84
+ def handle_request
85
+ raise "need block" unless block_given?
86
+ if @server_pool.server.nil?
87
+ ProconBypassMan.logger.info('送信先が未設定なのでスキップしました')
88
+ return
89
+ end
90
+ return yield
91
+ rescue SocketError => e
92
+ ProconBypassMan.logger.error(e)
93
+ if @retry_on_connection_error
94
+ sleep(10)
95
+ retry
96
+ end
97
+ rescue => e
98
+ puts e
99
+ ProconBypassMan.logger.error(e)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,19 @@
1
+ module ProconBypassMan
2
+ class ReportHttpClient < HttpClient
3
+ def post(body: , event_type: )
4
+ if body.is_a?(Hash)
5
+ b = body
6
+ else
7
+ b = { text: body }
8
+ end
9
+
10
+ super(request_body: {
11
+ session_id: ProconBypassMan.session_id,
12
+ device_id: ProconBypassMan.device_id,
13
+ hostname: `hostname`.chomp,
14
+ event_type: event_type,
15
+ body: b,
16
+ })
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  module ProconBypassMan
2
- class Timer
2
+ class SafeTimeout
3
3
  class Timeout < StandardError; end
4
4
 
5
5
  # 5秒後がタイムアウト
@@ -0,0 +1,9 @@
1
+ module ProconBypassMan
2
+ class SendDeviceStatsHttpClient < HttpClient
3
+ def post(status: , pbm_session_id: )
4
+ super(request_body: {
5
+ body: { status: status, pbm_session_id: pbm_session_id },
6
+ })
7
+ end
8
+ end
9
+ end