mirror of
https://github.com/cyberarm/w3d_hub_linux_launcher.git
synced 2026-03-22 04:06:18 +00:00
Compare commits
2 Commits
f98d8c3394
...
f651143937
| Author | SHA1 | Date | |
|---|---|---|---|
| f651143937 | |||
| 1425225eef |
@@ -43,7 +43,7 @@ GEM
|
||||
fiber-storage (1.0.1)
|
||||
fiddle (1.1.8)
|
||||
gosu (1.4.6)
|
||||
io-endpoint (0.17.1)
|
||||
io-endpoint (0.17.2)
|
||||
io-event (1.14.2)
|
||||
io-stream (0.11.1)
|
||||
ircparser (1.0.0)
|
||||
@@ -97,4 +97,4 @@ DEPENDENCIES
|
||||
win32-security
|
||||
|
||||
BUNDLED WITH
|
||||
2.6.8
|
||||
4.0.3
|
||||
|
||||
261
lib/api.rb
261
lib/api.rb
@@ -16,32 +16,7 @@ class W3DHub
|
||||
].freeze
|
||||
|
||||
def self.on_thread(method, *args, &callback)
|
||||
raise "Renew."
|
||||
BackgroundWorker.foreground_job(-> { Api.send(method, *args) }, callback)
|
||||
end
|
||||
|
||||
class Response
|
||||
def initialize(error: nil, status: -1, body: "")
|
||||
@status = status
|
||||
@body = body
|
||||
@error = error
|
||||
end
|
||||
|
||||
def success?
|
||||
@status == 200
|
||||
end
|
||||
|
||||
def status
|
||||
@status
|
||||
end
|
||||
|
||||
def body
|
||||
@body
|
||||
end
|
||||
|
||||
def error
|
||||
@error
|
||||
end
|
||||
Api.send(method, *args, &callback)
|
||||
end
|
||||
|
||||
#! === W3D Hub API === !#
|
||||
@@ -50,7 +25,9 @@ class W3DHub
|
||||
|
||||
HTTP_CLIENTS = {}
|
||||
|
||||
def self.async_http(method, path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub)
|
||||
def self.async_http(method, path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub, &callback)
|
||||
raise "NO CALLBACK DEFINED!" unless callback
|
||||
|
||||
case backend
|
||||
when :w3dhub
|
||||
endpoint = W3DHUB_API_ENDPOINT
|
||||
@@ -79,24 +56,7 @@ class W3DHub
|
||||
headers << ["authorization", "Bearer #{Store.account.access_token}"]
|
||||
end
|
||||
|
||||
Sync do
|
||||
begin
|
||||
response = provision_http_client(endpoint).send(method, path, headers, body)
|
||||
|
||||
Response.new(status: response.status, body: response.read)
|
||||
rescue Async::TimeoutError => e
|
||||
logger.error(LOG_TAG) { "Connection to \"#{url}\" timed out after: #{API_TIMEOUT} seconds" }
|
||||
|
||||
Response.new(error: e)
|
||||
rescue StandardError => e
|
||||
logger.error(LOG_TAG) { "Connection to \"#{url}\" errored:" }
|
||||
logger.error(LOG_TAG) { e }
|
||||
|
||||
Response.new(error: e)
|
||||
ensure
|
||||
response&.close
|
||||
end
|
||||
end
|
||||
Store.network_manager.request(method, url, headers, body, nil, &callback)
|
||||
end
|
||||
|
||||
def self.provision_http_client(hostname)
|
||||
@@ -114,17 +74,17 @@ class W3DHub
|
||||
HTTP_CLIENTS[Thread.current][hostname.downcase] = Async::HTTP::Client.new(endpoint)
|
||||
end
|
||||
|
||||
def self.post(path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub)
|
||||
async_http(:post, path, headers, body, backend)
|
||||
def self.post(path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub, &callback)
|
||||
async_http(:post, path, headers, body, backend, &callback)
|
||||
end
|
||||
|
||||
def self.get(path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub)
|
||||
async_http(:get, path, headers, body, backend)
|
||||
def self.get(path, headers = DEFAULT_HEADERS, body = nil, backend = :w3dhub, &callback)
|
||||
async_http(:get, path, headers, body, backend, &callback)
|
||||
end
|
||||
|
||||
# Api.get but handles any URL instead of known hosts
|
||||
def self.fetch(path, headers = DEFAULT_HEADERS, body = nil, backend = nil)
|
||||
async_http(:get, path, headers, body, backend)
|
||||
def self.fetch(path, headers = DEFAULT_HEADERS, body = nil, backend = nil, &callback)
|
||||
async_http(:get, path, headers, body, backend, &callback)
|
||||
end
|
||||
|
||||
# Method: POST
|
||||
@@ -142,92 +102,113 @@ class W3DHub
|
||||
#
|
||||
# On a failed login the service responds with:
|
||||
# {"error":"login-failed"}
|
||||
def self.refresh_user_login(refresh_token, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({refreshToken: refresh_token}))
|
||||
response = post("/apis/launcher/1/user-login", FORM_ENCODED_HEADERS, body, backend)
|
||||
def self.refresh_user_login(refresh_token, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
user_data = JSON.parse(result.data, symbolize_names: true)
|
||||
|
||||
if response.status == 200
|
||||
user_data = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
return false if user_data[:error]
|
||||
if user_data[:error]
|
||||
callback.call(false)
|
||||
next
|
||||
end
|
||||
|
||||
user_details_data = user_details(user_data[:userid]) || {}
|
||||
|
||||
Account.new(user_data, user_details_data)
|
||||
callback.call(Account.new(user_data, user_details_data))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch refresh user login:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ refreshToken: refresh_token }))
|
||||
post("/apis/launcher/1/user-login", FORM_ENCODED_HEADERS, body, backend, &handler)
|
||||
end
|
||||
|
||||
# See #user_refresh_token
|
||||
def self.user_login(username, password, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({username: username, password: password}))
|
||||
response = post("/apis/launcher/1/user-login", FORM_ENCODED_HEADERS, body, backend)
|
||||
def self.user_login(username, password, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
user_data = JSON.parse(result.data, symbolize_names: true)
|
||||
|
||||
if response.status == 200
|
||||
user_data = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
return false if user_data[:error]
|
||||
if user_data[:error]
|
||||
callback.call(false)
|
||||
next
|
||||
end
|
||||
|
||||
user_details_data = user_details(user_data[:userid]) || {}
|
||||
|
||||
Account.new(user_data, user_details_data)
|
||||
callback.call(Account.new(user_data, user_details_data))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch user login:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ username: username, password: password }))
|
||||
post("/apis/launcher/1/user-login", FORM_ENCODED_HEADERS, body, backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/w3dhub/1/get-user-details
|
||||
#
|
||||
# Response: avatar-uri (Image download uri), id, username
|
||||
def self.user_details(id, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({ id: id }))
|
||||
user_details = post("/apis/w3dhub/1/get-user-details", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if user_details.status == 200
|
||||
JSON.parse(user_details.body, symbolize_names: true)
|
||||
def self.user_details(id, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(JSON.parse(result.data, symbolize_names: true))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch user details:" }
|
||||
logger.error(LOG_TAG) { user_details }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ id: id }))
|
||||
post("/apis/w3dhub/1/get-user-details", FORM_ENCODED_HEADERS, body, backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/w3dhub/1/get-service-status
|
||||
# Service response:
|
||||
# {"services":{"authentication":true,"packageDownload":true}}
|
||||
def self.service_status(backend = :w3dhub)
|
||||
response = post("/apis/w3dhub/1/get-service-status", DEFAULT_HEADERS, nil, backend)
|
||||
|
||||
if response.status == 200
|
||||
ServiceStatus.new(response.body)
|
||||
def self.service_status(backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(ServiceStatus.new(result.data))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch service status:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
post("/apis/w3dhub/1/get-service-status", DEFAULT_HEADERS, nil, backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/launcher/1/get-applications
|
||||
# Client sends an Authorization header bearer token which is received from logging in (Optional)
|
||||
# Launcher sends an empty data request: data={}
|
||||
# Response is a list of applications/games
|
||||
def self.applications(backend = :w3dhub)
|
||||
response = post("/apis/launcher/1/get-applications", DEFAULT_HEADERS, nil, backend)
|
||||
|
||||
if response.status == 200
|
||||
Applications.new(response.body, backend)
|
||||
def self.applications(backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(Applications.new(result.data, backend))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch applications list:" }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
post("/apis/launcher/1/get-applications", DEFAULT_HEADERS, nil, backend, &handler)
|
||||
end
|
||||
|
||||
# Populate applications list from primary and alternate backends
|
||||
# (alternate only has latest public builds of _most_ games)
|
||||
def self._applications
|
||||
@@ -301,63 +282,72 @@ class W3DHub
|
||||
# Client sends an Authorization header bearer token which is received from logging in (Optional)
|
||||
# Client requests news for a specific application/game e.g.: data={"category":"ia"} ("launcher-home" retrieves the weekly hub updates)
|
||||
# Response is a JSON hash with a "highlighted" and "news" keys; the "news" one seems to be the desired one
|
||||
def self.news(category, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({category: category}))
|
||||
response = post("/apis/w3dhub/1/get-news", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if response.status == 200
|
||||
News.new(response.body)
|
||||
def self.news(category, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(News.new(result.data))
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch news for:" }
|
||||
logger.error(LOG_TAG) { category }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ category: category }))
|
||||
post("/apis/w3dhub/1/get-news", FORM_ENCODED_HEADERS, body, backend, &handler)
|
||||
end
|
||||
|
||||
# Downloading games
|
||||
|
||||
# /apis/launcher/1/get-package-details
|
||||
# client requests package details: data={"packages":[{"category":"games","name":"apb.ico","subcategory":"apb","version":""}]}
|
||||
def self.package_details(packages, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({ packages: packages }))
|
||||
response = post("/apis/launcher/1/get-package-details", FORM_ENCODED_HEADERS, body, backend)
|
||||
def self.package_details(packages, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
hash = JSON.parse(result.data, symbolize_names: true)
|
||||
|
||||
if response.status == 200
|
||||
hash = JSON.parse(response.body, symbolize_names: true)
|
||||
|
||||
hash[:packages].map { |pkg| Package.new(pkg) }
|
||||
callback.call(hash[:packages].map { |pkg| Package.new(pkg) })
|
||||
else
|
||||
logger.error(LOG_TAG) { "Failed to fetch package details for:" }
|
||||
logger.error(LOG_TAG) { packages }
|
||||
logger.error(LOG_TAG) { response }
|
||||
false
|
||||
logger.error(LOG_TAG) { result.error }
|
||||
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ packages: packages }))
|
||||
post("/apis/launcher/1/get-package-details", FORM_ENCODED_HEADERS, body, backend, &handler)
|
||||
end
|
||||
|
||||
# /apis/launcher/1/get-package
|
||||
# client requests package: data={"category":"games","name":"ECW_Asteroids.zip","subcategory":"ecw","version":"1.0.0.0"}
|
||||
#
|
||||
# server responds with download bytes, probably supports chunked download and resume
|
||||
def self.package(package, &block)
|
||||
Cache.fetch_package(package, block)
|
||||
# FIXME: REFACTOR Cache.fetch_package to use HttpClient
|
||||
def self.package(package, &callback)
|
||||
Cache.fetch_package(package, callback)
|
||||
end
|
||||
|
||||
# /apis/w3dhub/1/get-events
|
||||
#
|
||||
# clients requests events: data={"serverPath":"apb"}
|
||||
def self.events(app_id, backend = :w3dhub)
|
||||
body = URI.encode_www_form("data": JSON.dump({ serverPath: app_id }))
|
||||
response = post("/apis/w3dhub/1/get-server-events", FORM_ENCODED_HEADERS, body, backend)
|
||||
|
||||
if response.status == 200
|
||||
def self.events(app_id, backend = :w3dhub, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
array = JSON.parse(response.body, symbolize_names: true)
|
||||
array.map { |e| Event.new(e) }
|
||||
callback.call(array.map { |e| Event.new(e) })
|
||||
else
|
||||
false
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
body = URI.encode_www_form("data": JSON.dump({ serverPath: app_id }))
|
||||
post("/apis/w3dhub/1/get-server-events", FORM_ENCODED_HEADERS, body, backend, &handler)
|
||||
end
|
||||
|
||||
#! === Server List API === !#
|
||||
|
||||
# SERVER_LIST_ENDPOINT = "https://gsh.w3dhub.com".freeze
|
||||
@@ -381,15 +371,17 @@ class W3DHub
|
||||
# id, name, score, kills, deaths
|
||||
# ...players[]:
|
||||
# nick, team (index of teams array), score, kills, deaths
|
||||
def self.server_list(level = 1, backend = :gsh)
|
||||
response = get("/listings/getAll/v2?statusLevel=#{level}", DEFAULT_HEADERS, nil, backend)
|
||||
|
||||
if response.status == 200
|
||||
data = JSON.parse(response.body, symbolize_names: true)
|
||||
return data.map { |hash| ServerListServer.new(hash) }
|
||||
def self.server_list(level = 1, backend = :gsh, &callback)
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
data = JSON.parse(result.data, symbolize_names: true)
|
||||
callback.call(data.map { |hash| ServerListServer.new(hash) })
|
||||
else
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
false
|
||||
get("/listings/getAll/v2?statusLevel=#{level}", DEFAULT_HEADERS, nil, backend, &handler)
|
||||
end
|
||||
|
||||
# /listings/getStatus/v2/:id?statusLevel=#{0-2}
|
||||
@@ -403,23 +395,24 @@ class W3DHub
|
||||
# id, name, score, kills, deaths
|
||||
# ...players[]:
|
||||
# nick, team (index of teams array), score, kills, deaths
|
||||
def self.server_details(id, level, backend = :gsh)
|
||||
def self.server_details(id, level, backend = :gsh, &callback)
|
||||
return false unless id && level
|
||||
|
||||
response = get("/listings/getStatus/v2/#{id}?statusLevel=#{level}", DEFAULT_HEADERS, nil, backend)
|
||||
|
||||
if response.status == 200
|
||||
hash = JSON.parse(response.body, symbolize_names: true)
|
||||
return hash
|
||||
handler = lambda do |result|
|
||||
if result.okay?
|
||||
callback.call(JSON.parse(response.body, symbolize_names: true))
|
||||
else
|
||||
callback.call(false)
|
||||
end
|
||||
end
|
||||
|
||||
false
|
||||
get("/listings/getStatus/v2/#{id}?statusLevel=#{level}", DEFAULT_HEADERS, nil, backend, &handler)
|
||||
end
|
||||
|
||||
# /listings/push/v2/negotiate?negotiateVersion=1
|
||||
##? /listings/push/v2/?id=#{websocket token?}
|
||||
## Websocket server list listener
|
||||
def self.server_list_push(id)
|
||||
def self.server_list_push(id, &callback)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,7 +23,7 @@ class W3DHub
|
||||
@invocation_id = 0
|
||||
|
||||
logger.info(LOG_TAG) { "Starting emulated SignalR Server List Updater..." }
|
||||
run
|
||||
# run
|
||||
end
|
||||
|
||||
def run
|
||||
@@ -32,7 +32,8 @@ class W3DHub
|
||||
begin
|
||||
@auto_reconnect = true
|
||||
|
||||
while W3DHub::BackgroundWorker.alive?
|
||||
# FIXME
|
||||
while true #W3DHub::BackgroundWorker.alive?
|
||||
connect if @auto_reconnect
|
||||
sleep @reconnection_delay
|
||||
end
|
||||
@@ -54,19 +55,31 @@ class W3DHub
|
||||
@auto_reconnect = false
|
||||
|
||||
logger.debug(LOG_TAG) { "Requesting connection token..." }
|
||||
response = Api.post("/listings/push/v2/negotiate?negotiateVersion=1", Api::DEFAULT_HEADERS, "", :gsh)
|
||||
|
||||
if response.status != 200
|
||||
result = nil
|
||||
Api.post("/listings/push/v2/negotiate?negotiateVersion=1", Api::DEFAULT_HEADERS, "", :gsh) do |callback_result|
|
||||
result = callback_result
|
||||
end
|
||||
|
||||
# FIXME: we've introduced ourselves to callback hell, yay!
|
||||
while result.nil?
|
||||
sleep 0.1
|
||||
end
|
||||
|
||||
if result.error?
|
||||
@auto_reconnect = true
|
||||
@reconnection_delay = @reconnection_delay * 2
|
||||
@reconnection_delay *= 2
|
||||
@reconnection_delay = 60 if @reconnection_delay > 60
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
@reconnection_delay = 1
|
||||
|
||||
data = JSON.parse(response.body, symbolize_names: true)
|
||||
connect_websocket(JSON.parse(result.data, symbolize_names: true))
|
||||
end
|
||||
|
||||
def connect_websocket(data)
|
||||
@invocation_id = 0 if @invocation_id > 9095
|
||||
id = data[:connectionToken]
|
||||
endpoint = "#{Api::SERVER_LIST_ENDPOINT}/listings/push/v2?id=#{id}"
|
||||
|
||||
@@ -5,6 +5,7 @@ class W3DHub
|
||||
Request = Struct.new(:context, :callback)
|
||||
Context = Data.define(
|
||||
:request_id,
|
||||
:method,
|
||||
:url,
|
||||
:headers,
|
||||
:body,
|
||||
@@ -12,52 +13,42 @@ class W3DHub
|
||||
)
|
||||
|
||||
def initialize
|
||||
@requests = {}
|
||||
@requests = []
|
||||
@running = true
|
||||
|
||||
@ractor = Ractor.new do
|
||||
raise "Something has gone quite wrong!" if Ractor.main?
|
||||
|
||||
queue = []
|
||||
api_client = ApiClient.new
|
||||
|
||||
# Ractor has no concept of non-blocking send/receive... :cry:
|
||||
Thread.new do
|
||||
while (context = Ractor.receive) # blocking
|
||||
# we cannot (easily) ensure we always are receive expected data
|
||||
next unless context.is_a?(Context)
|
||||
http_client = HttpClient.new
|
||||
|
||||
queue << context
|
||||
end
|
||||
end
|
||||
Sync do
|
||||
while @running
|
||||
request = @requests.shift
|
||||
|
||||
Async do
|
||||
loop do
|
||||
context = queue.shift
|
||||
|
||||
# goto sleep for an instant if there is no work to be doing
|
||||
unless context
|
||||
sleep 0.1
|
||||
# goto sleep for an second if there is no work to be doing
|
||||
unless request
|
||||
sleep 1
|
||||
next
|
||||
end
|
||||
|
||||
Sync do
|
||||
result = api_client.handle(context)
|
||||
Async do |task|
|
||||
assigned_request = request
|
||||
result = http_client.handle(task, assigned_request)
|
||||
|
||||
Ractor.yield(NetworkEvent.new(context, result))
|
||||
pp [assigned_request, result]
|
||||
|
||||
Store.main_thread_queue << -> { assigned_request.callback.call(result) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
monitor
|
||||
end
|
||||
|
||||
def add_request(url, headers, body, bearer_token, &block)
|
||||
def request(method, url, headers, body, bearer_token, &block)
|
||||
request_id = SecureRandom.hex
|
||||
|
||||
@requests << Request.new(
|
||||
request = Request.new(
|
||||
Context.new(
|
||||
request_id,
|
||||
method,
|
||||
url,
|
||||
headers,
|
||||
body,
|
||||
@@ -66,31 +57,9 @@ class W3DHub
|
||||
block
|
||||
)
|
||||
|
||||
@ractor.send(context)
|
||||
@requests << request
|
||||
|
||||
request_id
|
||||
end
|
||||
|
||||
def monitor
|
||||
raise "Something has gone quite wrong!!!" unless Ractor.main?
|
||||
|
||||
# Thread that spends its days sleeping **yawn**
|
||||
Thread.new do
|
||||
while (event = @ractor.take)
|
||||
pp event
|
||||
|
||||
next unless event.is_a?(NetworkEvent)
|
||||
|
||||
request = @request.find { |r| r.context.request_id == event.context.request_id }
|
||||
|
||||
next if request
|
||||
|
||||
@requests.delete(request)
|
||||
result = event.result
|
||||
|
||||
Store.main_thread_queue << ->(result) { request.callback(result) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
class W3DHub
|
||||
class NetworkManager
|
||||
# Api reimplemented in a Ractor friendly manner
|
||||
class ApiClient
|
||||
def initialize
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
61
lib/network_manager/http_client.rb
Normal file
61
lib/network_manager/http_client.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
class W3DHub
|
||||
class NetworkManager
|
||||
# non-blocking, http requests.
|
||||
class HttpClient
|
||||
def initialize
|
||||
@http_clients = {}
|
||||
end
|
||||
|
||||
def handle(task, request)
|
||||
result = CyberarmEngine::Result.new
|
||||
context = request.context
|
||||
|
||||
task.with_timeout(30) do
|
||||
uri = URI(context.url)
|
||||
|
||||
pp uri
|
||||
|
||||
response = provision_http_client(uri.origin).send(
|
||||
context.method,
|
||||
uri.path,
|
||||
context.headers,
|
||||
context.body
|
||||
)
|
||||
|
||||
pp response
|
||||
|
||||
if response.success?
|
||||
result.data = response.body.read
|
||||
else
|
||||
result.error = response
|
||||
end
|
||||
rescue Async::TimeoutError => e
|
||||
result.error = e
|
||||
rescue StandardError => e
|
||||
result.error = e
|
||||
ensure
|
||||
response&.close
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def provision_http_client(hostname)
|
||||
return @http_clients[hostname.downcase] if @http_clients[hostname.downcase]
|
||||
|
||||
ssl_context = W3DHub.ca_bundle_path ? OpenSSL::SSL::SSLContext.new : nil
|
||||
ssl_context&.set_params(
|
||||
ca_file: W3DHub.ca_bundle_path,
|
||||
verify_mode: OpenSSL::SSL::VERIFY_PEER
|
||||
)
|
||||
|
||||
endpoint = Async::HTTP::Endpoint.parse(hostname, ssl_context: ssl_context)
|
||||
@http_clients[hostname.downcase] = Async::HTTP::Client.new(endpoint)
|
||||
end
|
||||
|
||||
def wrapped_error(error)
|
||||
WrappedError.new(error.class, error.message.to_s, error.backtrace)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -175,7 +175,11 @@ class W3DHub
|
||||
end
|
||||
|
||||
def service_status
|
||||
@status_label.value = "Checking service status..." #I18n.t(:"server_browser.fetching_server_list")
|
||||
|
||||
Api.on_thread(:service_status) do |service_status|
|
||||
pp service_status
|
||||
|
||||
@service_status = service_status
|
||||
|
||||
if @service_status
|
||||
@@ -227,14 +231,14 @@ class W3DHub
|
||||
def applications
|
||||
@status_label.value = I18n.t(:"boot.checking_for_updates")
|
||||
|
||||
Api.on_thread(:_applications) do |applications|
|
||||
# Api.on_thread(:_applications) do |applications|
|
||||
Api.on_thread(:applications, :alt_w3dhub) do |applications|
|
||||
if applications
|
||||
Store.applications = applications
|
||||
Store.settings.save_application_cache(applications.data.to_json)
|
||||
@tasks[:applications][:complete] = true
|
||||
else
|
||||
# FIXME: Failed to retreive!
|
||||
BackgroundWorker.foreground_job(-> {}, ->(_) { @status_label.value = "FAILED TO RETREIVE APPS LIST" })
|
||||
@status_label.value = "FAILED TO RETREIVE APPS LIST"
|
||||
|
||||
@offline_mode = true
|
||||
Store.offline_mode = true
|
||||
|
||||
@@ -113,7 +113,7 @@ require_relative "lib/hardware_survey"
|
||||
require_relative "lib/game_settings"
|
||||
require_relative "lib/websocket_client"
|
||||
require_relative "lib/network_manager"
|
||||
require_relative "lib/network_manager/api_client"
|
||||
require_relative "lib/network_manager/http_client"
|
||||
require_relative "lib/application_manager"
|
||||
require_relative "lib/application_manager/manifest"
|
||||
require_relative "lib/application_manager/status"
|
||||
|
||||
Reference in New Issue
Block a user