Skip to content

Commit edb43a9

Browse files
Merge pull request #491 from pro3d-space/bugs/httpclient_v2
Bugs/httpclient v2
2 parents e426862 + ce26832 commit edb43a9

File tree

6 files changed

+38
-54
lines changed

6 files changed

+38
-54
lines changed

src/PRo3D.Base/Utilities.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,10 +1385,10 @@ module ScreenshotUtilities =
13851385
frameTime : float
13861386
}
13871387

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

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

14131413
let takeScreenshotFromAllViews baseAddress (width:int) (height:int) name folder format =
1414-
let wc = new HttpClient()
1415-
let clientStatistics = downloadClientStatistics baseAddress wc
1414+
let httpClient = new HttpClient()
1415+
let clientStatistics = downloadClientStatistics baseAddress httpClient
14161416

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

14251425
let takeScreenshot baseAddress (width:int) (height:int) name folder format =
1426-
let wc = new HttpClient()
1427-
let clientStatistics = downloadClientStatistics baseAddress wc
1426+
let httpClient = new HttpClient()
1427+
let clientStatistics = downloadClientStatistics baseAddress httpClient
14281428

14291429
let cs =
14301430
match clientStatistics.Length with
@@ -1434,7 +1434,7 @@ module ScreenshotUtilities =
14341434

14351435
let screenshot = getScreenshotUrl baseAddress cs width height
14361436
let filename = getScreenshotFilename folder name cs format
1437-
wc.DownloadFile(screenshot,filename)
1437+
httpClient.DownloadFile(screenshot,filename)
14381438

14391439
module JsInterop =
14401440
let escapePath (s : string) =

src/PRo3D.Base/WebClientDeprecation.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ module Helpers =
1313
}
1414

1515
member x.DownloadFile(uri : string, filename : string) =
16-
x.DownloadFileAsync(uri, filename).RunSynchronously()
16+
x.DownloadFileAsync(uri, filename).Wait()

src/PRo3D.SimulatedViews/Screenshots/ScreenshotApp.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ module ScreenshotApp =
2020
baseUrl
2121
numberOfSamples
2222
(m : ScreenshotModel)
23-
(wc : System.Net.Http.HttpClient) =
23+
(httpClient : System.Net.Http.HttpClient) =
2424

25-
let stats = ScreenshotUtilities.Utilities.downloadClientStatistics baseUrl wc
25+
let stats = ScreenshotUtilities.Utilities.downloadClientStatistics baseUrl httpClient
2626

2727
let color = m.backgroundColor.c.ToC4f().ToV4f()
2828
let renderingNodeId = stats.[0].name

src/PRo3D.Viewer/RemoteControlApp.fs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ open PRo3D.SimulatedViews
1111
open RemoteControlModel
1212
open MBrace
1313

14+
15+
1416
type ClientStatistics =
1517
{
1618
session : System.Guid
@@ -26,37 +28,9 @@ module RemoteControlApp =
2628

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

29-
30-
3131
let fromDate (dt : DateTime) =
3232
dt.ToString("yyyymmdd_hhmmss")
3333

34-
let takeScreenshot baseAddress (width:int) (height:int) name folder =
35-
let wc = new System.Net.WebClient()
36-
let path = "screenshots"
37-
if System.IO.Directory.Exists path |> not then
38-
System.IO.Directory.CreateDirectory path |> ignore
39-
let clientStatistic =
40-
let path = sprintf "%s/rendering/stats.json" baseAddress
41-
Log.line "[RemoteControl] querying rendering stats at: %s" path
42-
let result = wc.DownloadString(path)
43-
let clientBla : list<ClientStatistics> =
44-
Pickler.unpickleOfJson result
45-
match clientBla with
46-
| [] -> failwith "no client bla"
47-
| x::[] -> x
48-
| _ -> failwith "doent know"
49-
let screenshot =
50-
sprintf "%s/rendering/screenshot/%s?w=%d&h=%d&samples=8" baseAddress clientStatistic.name width height
51-
Log.line "[RemoteControl] Running screenshot on: %s" screenshot
52-
53-
match System.IO.Directory.Exists folder with
54-
| true -> ()
55-
| false -> System.IO.Directory.CreateDirectory folder |> ignore
56-
57-
let filename = System.IO.Path.ChangeExtension (name,".jpg")
58-
wc.DownloadFile(screenshot,Path.combine [folder; filename])
59-
6034
let mkInstrumetnWps m =
6135
let pShots =
6236
m.shots |> IndexList.choose(fun x -> PlatformShot.froAdaptiveRoverModel m.Rover x)
@@ -108,14 +82,12 @@ module RemoteControlApp =
10882
| CaptureShot sh ->
10983
let view = sh |> Shot.getViewSpec
11084
view |> RemoteAction.SetView |> send
111-
112-
try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
85+
try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
11386
m
11487
| CapturePlatform psh ->
11588
let view = PlatformShot.getViewSpec m.Rover psh
11689
view |> RemoteAction.SetView |> send
117-
118-
try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y psh.id psh.folder ".png" with e -> printfn "error: %A" e
90+
try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y psh.id psh.folder ".png" with e -> printfn "error: %A" e
11991
m
12092
| UpdateCameraTest sh ->
12193
send <| RemoteAction.SetCameraView (sh |> Shot.getCamera)
@@ -127,8 +99,7 @@ module RemoteControlApp =
12799

128100
let view = PlatformShot.getViewSpec m.Rover p
129101
view |> RemoteAction.SetView |> send
130-
131-
try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y sh.id sh.folder ".png" with e -> printfn "error: %A" e
102+
try Utilities.takeScreenshot baseAddress view.resolution.X view.resolution.Y sh.id sh.folder ".png" with e -> printfn "error: %A" e
132103
m
133104
| None -> m
134105
| SelectShot sh ->
@@ -138,8 +109,8 @@ module RemoteControlApp =
138109
{ m with selectedShot = Some sh }
139110
| Play ->
140111
for sh in m.shots do
141-
send <| RemoteAction.SetCameraView (sh |> Shot.getCamera)
142-
try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
112+
send <| RemoteAction.SetCameraView (sh |> Shot.getCamera)
113+
try Utilities.takeScreenshot baseAddress sh.col sh.row sh.id sh.folder ".png" with e -> printfn "error: %A" e
143114
m
144115
| Load ->
145116
loadData m

src/PRo3D.Viewer/Utilities.fs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ open OpcViewer.Base
1010

1111
open PRo3D
1212
open PRo3D.Base.Annotation
13+
14+
open System.Net.Http
1315

1416
module Mod =
1517
open FSharp.Data.Adaptive
@@ -30,24 +32,28 @@ module Net =
3032
open System.Threading
3133
open Aardvark.UI
3234
let getClient () =
35+
let downloadString_ (httpClient: HttpClient) (path: string) = async {
36+
let! result = httpClient.GetStringAsync(path) |> Async.AwaitTask
37+
return result
38+
}
3339
use cancelToken = new CancellationTokenSource()
3440
let waitForClient =
3541
async {
3642
for i in 1..100 do
37-
let wc = new System.Net.WebClient()
43+
let httpClient = new HttpClient()
3844
try
39-
let lst = wc.DownloadString("http://localhost:54321/rendering/stats.json")
45+
let lst = downloadString_ httpClient "http://localhost:54321/rendering/stats.json" |> Async.RunSynchronously
4046
match String.length lst > 3 with
4147
| true -> cancelToken.Cancel ()
4248
| false -> do! Async.Sleep 1000
4349
with ex -> do! Async.Sleep 1000
4450
}
4551
try Async.RunSynchronously (waitForClient, -1, cancelToken.Token) with e -> ()
46-
let wc = new System.Net.WebClient()
47-
let jsonString = wc.DownloadString("http://localhost:54321/rendering/stats.json")
52+
let httpClient = new HttpClient()
53+
let jsonString = downloadString_ httpClient "http://localhost:54321/rendering/stats.json" |> Async.RunSynchronously
4854
let clientStats : list<PRo3D.Base.Utilities.ClientStatistics> =
4955
Pickler.unpickleOfJson jsonString
50-
(wc, clientStats)
56+
(httpClient, clientStats)
5157

5258

5359
namespace Aardvark.UI

src/PRo3D.Viewer/Viewer/SceneConverter.fs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
open System
44
open System.Diagnostics
55

6+
open System.Net.Http
67

78
// helper module for spawning processes
89
module private Process =
@@ -71,11 +72,17 @@ module SceneLoading =
7172

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

75+
let downloadFile_ (url: string) (fileStream: FileStream) (client : HttpClient) = async {
76+
let! responseStream = client.GetStreamAsync(url) |> Async.AwaitTask
77+
do! responseStream.CopyToAsync(fileStream) |> Async.AwaitTask
78+
}
79+
7480
let downloadConverter () =
75-
use wc = new WebClient()
81+
use httpClient = new HttpClient()
7682
let temp = Path.GetTempFileName()
7783
Log.line "Downloading converter from: %s" DownloadPath
78-
wc.DownloadFile(DownloadPath, temp)
84+
use fileStream = File.Create(DownloadPath)
85+
downloadFile_ temp fileStream httpClient |> Async.RunSynchronously
7986
if Directory.Exists ConverterPath then Directory.Delete(ConverterPath, true)
8087
Directory.CreateDirectory ConverterPath |> ignore
8188
Log.line "unpacking to: %s" ConverterPath

0 commit comments

Comments
 (0)