Skip to content

Bugs/httpclient v2 #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/PRo3D.Base/Utilities.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,10 +1385,10 @@ module ScreenshotUtilities =
frameTime : float
}

let downloadClientStatistics baseAddress (webClient : HttpClient) =
let downloadClientStatistics baseAddress (httpClient : HttpClient) =
let path = sprintf "%s/rendering/stats.json" baseAddress //sprintf "%s/rendering/stats.json" baseAddress
Log.line "[Screenshot] querying rendering stats at: %s" path
let result = webClient.GetStringAsync(path).Result
let result = httpClient.GetStringAsync(path).Result

let clientBla : list<ClientStatistics> =
Pickler.unpickleOfJson result
Expand All @@ -1411,20 +1411,20 @@ module ScreenshotUtilities =
Path.combine [folder; name + "_" + clientStats.name + format]

let takeScreenshotFromAllViews baseAddress (width:int) (height:int) name folder format =
let wc = new HttpClient()
let clientStatistics = downloadClientStatistics baseAddress wc
let httpClient = new HttpClient()
let clientStatistics = downloadClientStatistics baseAddress httpClient

for cs in clientStatistics do
let screenshot = getScreenshotUrl baseAddress cs width height
let filename = getScreenshotFilename folder name cs format
wc.DownloadFile(screenshot, filename)
httpClient.DownloadFile(screenshot, filename)
let fullpath =
try System.IO.Path.GetFullPath(filename) with e -> filename
Log.line "[Screenshot] saved to %s" fullpath

let takeScreenshot baseAddress (width:int) (height:int) name folder format =
let wc = new HttpClient()
let clientStatistics = downloadClientStatistics baseAddress wc
let httpClient = new HttpClient()
let clientStatistics = downloadClientStatistics baseAddress httpClient

let cs =
match clientStatistics.Length with
Expand All @@ -1434,7 +1434,7 @@ module ScreenshotUtilities =

let screenshot = getScreenshotUrl baseAddress cs width height
let filename = getScreenshotFilename folder name cs format
wc.DownloadFile(screenshot,filename)
httpClient.DownloadFile(screenshot,filename)

module JsInterop =
let escapePath (s : string) =
Expand Down
2 changes: 1 addition & 1 deletion src/PRo3D.Base/WebClientDeprecation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ module Helpers =
}

member x.DownloadFile(uri : string, filename : string) =
x.DownloadFileAsync(uri, filename).RunSynchronously()
x.DownloadFileAsync(uri, filename).Wait()
4 changes: 2 additions & 2 deletions src/PRo3D.SimulatedViews/Screenshots/ScreenshotApp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ module ScreenshotApp =
baseUrl
numberOfSamples
(m : ScreenshotModel)
(wc : System.Net.Http.HttpClient) =
(httpClient : System.Net.Http.HttpClient) =

let stats = ScreenshotUtilities.Utilities.downloadClientStatistics baseUrl wc
let stats = ScreenshotUtilities.Utilities.downloadClientStatistics baseUrl httpClient

let color = m.backgroundColor.c.ToC4f().ToV4f()
let renderingNodeId = stats.[0].name
Expand Down
43 changes: 7 additions & 36 deletions src/PRo3D.Viewer/RemoteControlApp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ open PRo3D.SimulatedViews
open RemoteControlModel
open MBrace



type ClientStatistics =
{
session : System.Guid
Expand All @@ -26,37 +28,9 @@ module RemoteControlApp =

let jsonSerializer = FsPickler.Json.JsonSerializer(indent=true)



let fromDate (dt : DateTime) =
dt.ToString("yyyymmdd_hhmmss")

let takeScreenshot baseAddress (width:int) (height:int) name folder =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was unused, right?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let wc = new System.Net.WebClient()
let path = "screenshots"
if System.IO.Directory.Exists path |> not then
System.IO.Directory.CreateDirectory path |> ignore
let clientStatistic =
let path = sprintf "%s/rendering/stats.json" baseAddress
Log.line "[RemoteControl] querying rendering stats at: %s" path
let result = wc.DownloadString(path)
let clientBla : list<ClientStatistics> =
Pickler.unpickleOfJson result
match clientBla with
| [] -> failwith "no client bla"
| x::[] -> x
| _ -> failwith "doent know"
let screenshot =
sprintf "%s/rendering/screenshot/%s?w=%d&h=%d&samples=8" baseAddress clientStatistic.name width height
Log.line "[RemoteControl] Running screenshot on: %s" screenshot

match System.IO.Directory.Exists folder with
| true -> ()
| false -> System.IO.Directory.CreateDirectory folder |> ignore

let filename = System.IO.Path.ChangeExtension (name,".jpg")
wc.DownloadFile(screenshot,Path.combine [folder; filename])

let mkInstrumetnWps m =
let pShots =
m.shots |> IndexList.choose(fun x -> PlatformShot.froAdaptiveRoverModel m.Rover x)
Expand Down Expand Up @@ -108,14 +82,12 @@ module RemoteControlApp =
| CaptureShot sh ->
let view = sh |> Shot.getViewSpec
view |> RemoteAction.SetView |> send

try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
m
| CapturePlatform psh ->
let view = PlatformShot.getViewSpec m.Rover psh
view |> RemoteAction.SetView |> send

try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y psh.id psh.folder ".png" with e -> printfn "error: %A" e
try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y psh.id psh.folder ".png" with e -> printfn "error: %A" e
m
| UpdateCameraTest sh ->
send <| RemoteAction.SetCameraView (sh |> Shot.getCamera)
Expand All @@ -127,8 +99,7 @@ module RemoteControlApp =

let view = PlatformShot.getViewSpec m.Rover p
view |> RemoteAction.SetView |> send

try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y sh.id sh.folder ".png" with e -> printfn "error: %A" e
try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y sh.id sh.folder ".png" with e -> printfn "error: %A" e
m
| None -> m
| SelectShot sh ->
Expand All @@ -138,8 +109,8 @@ module RemoteControlApp =
{ m with selectedShot = Some sh }
| Play ->
for sh in m.shots do
send <| RemoteAction.SetCameraView (sh |> Shot.getCamera)
try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
send <| RemoteAction.SetCameraView (sh |> Shot.getCamera)
try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
m
| Load ->
loadData m
Expand Down
16 changes: 11 additions & 5 deletions src/PRo3D.Viewer/Utilities.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ open OpcViewer.Base

open PRo3D
open PRo3D.Base.Annotation

open System.Net.Http

module Mod =
open FSharp.Data.Adaptive
Expand All @@ -30,24 +32,28 @@ module Net =
open System.Threading
open Aardvark.UI
let getClient () =
let downloadString_ (httpClient: HttpClient) (path: string) = async {
let! result = httpClient.GetStringAsync(path) |> Async.AwaitTask
return result
}
use cancelToken = new CancellationTokenSource()
let waitForClient =
async {
for i in 1..100 do
let wc = new System.Net.WebClient()
let httpClient = new HttpClient()
try
let lst = wc.DownloadString("http://localhost:54321/rendering/stats.json")
let lst = downloadString_ httpClient "http://localhost:54321/rendering/stats.json" |> Async.RunSynchronously
match String.length lst > 3 with
| true -> cancelToken.Cancel ()
| false -> do! Async.Sleep 1000
with ex -> do! Async.Sleep 1000
}
try Async.RunSynchronously (waitForClient, -1, cancelToken.Token) with e -> ()
let wc = new System.Net.WebClient()
let jsonString = wc.DownloadString("http://localhost:54321/rendering/stats.json")
let httpClient = new HttpClient()
let jsonString = downloadString_ httpClient "http://localhost:54321/rendering/stats.json" |> Async.RunSynchronously
let clientStats : list<PRo3D.Base.Utilities.ClientStatistics> =
Pickler.unpickleOfJson jsonString
(wc, clientStats)
(httpClient, clientStats)


namespace Aardvark.UI
Expand Down
11 changes: 9 additions & 2 deletions src/PRo3D.Viewer/Viewer/SceneConverter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open System
open System.Diagnostics

open System.Net.Http

// helper module for spawning processes
module private Process =
Expand Down Expand Up @@ -71,11 +72,17 @@ module SceneLoading =

let mutable DownloadPath = "http://download.vrvis.at/acquisition/pro3d/49a0d346a7ecd7f8eca596a7d895da7cb38ed8c0.zip"

let downloadFile_ (url: string) (fileStream: FileStream) (client : HttpClient) = async {
let! responseStream = client.GetStreamAsync(url) |> Async.AwaitTask
do! responseStream.CopyToAsync(fileStream) |> Async.AwaitTask
}

let downloadConverter () =
use wc = new WebClient()
use httpClient = new HttpClient()
let temp = Path.GetTempFileName()
Log.line "Downloading converter from: %s" DownloadPath
wc.DownloadFile(DownloadPath, temp)
use fileStream = File.Create(DownloadPath)
downloadFile_ temp fileStream httpClient |> Async.RunSynchronously
if Directory.Exists ConverterPath then Directory.Delete(ConverterPath, true)
Directory.CreateDirectory ConverterPath |> ignore
Log.line "unpacking to: %s" ConverterPath
Expand Down
Loading