using CodexOpenApi; using Core; using KubernetesWorkflow; using KubernetesWorkflow.Types; using Utils; namespace CodexPlugin { public class CodexAccess : ILogHandler { private readonly IPluginTools tools; private readonly Mapper mapper = new Mapper(); private bool hasContainerCrashed; public CodexAccess(IPluginTools tools, RunningContainer container, CrashWatcher crashWatcher) { this.tools = tools; Container = container; CrashWatcher = crashWatcher; hasContainerCrashed = false; CrashWatcher.Start(this); } public RunningContainer Container { get; } public CrashWatcher CrashWatcher { get; } public DebugInfo GetDebugInfo() { return Map(OnCodex(api => api.GetDebugInfoAsync())); } public DebugPeer GetDebugPeer(string peerId) { // Cannot use openAPI: debug/peer endpoint is not specified there. var endoint = GetEndpoint(); var str = endoint.HttpGetString($"debug/peer/{peerId}"); if (str.ToLowerInvariant() == "unable to find peer!") { return new DebugPeer { IsPeerFound = false }; } var result = endoint.Deserialize(str); result.IsPeerFound = true; return result; } public CodexDebugBlockExchangeResponse GetDebugBlockExchange() { return Http().HttpGetJson("debug/blockexchange"); } public CodexDebugRepoStoreResponse[] GetDebugRepoStore() { return LongHttp().HttpGetJson("debug/repostore"); } public CodexDebugThresholdBreaches GetDebugThresholdBreaches() { return Http().HttpGetJson("debug/loop"); } public string UploadFile(FileStream fileStream) { return Http().HttpPostStream("data", fileStream); } public Stream DownloadFile(string contentId) { return Http().HttpGetStream("data/" + contentId + "/network"); } public CodexLocalDataResponse[] LocalFiles() { return Http().HttpGetJson("data"); } public CodexSalesAvailabilityResponse SalesAvailability(CodexSalesAvailabilityRequest request) { return Http().HttpPostJson("sales/availability", request); } public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId) { return Http().HttpPostJson($"storage/request/{contentId}", request); } public CodexStoragePurchase GetPurchaseStatus(string purchaseId) { return Http().HttpGetJson($"storage/purchases/{purchaseId}"); } public string ConnectToPeer(string peerId, string peerMultiAddress) { return Http().HttpGetString($"connect/{peerId}?addrs={peerMultiAddress}"); } public string GetName() { return Container.Name; } public PodInfo GetPodInfo() { var workflow = tools.CreateWorkflow(); return workflow.GetPodInfo(Container); } private dynamic Map(dynamic input) { return mapper.Map(input); } private T OnCodex(Func> action) { var address = GetAddress(); var result = tools.CreateHttp(CheckContainerCrashed) .OnClient(client => { var api = new CodexApi(client); api.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1"; return Time.Wait(action(api)); }); return result; } //private IHttp Http() //{ // var address = GetAddress(); // var api = new CodexOpenApi.CodexApi(new HttpClient()); // api.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1"; // var debugInfo = Time.Wait(api.GetDebugInfoAsync()); // using var stream = File.OpenRead("C:\\Users\\thatb\\Desktop\\Collect\\Wallpapers\\demerui_djinn_illuminatus_fullbody_full_body_view_in_the_style__86ea9491-1fe1-44ab-8577-a3636cad1b21.png"); // var cid = Time.Wait(api.UploadAsync(stream)); // var file = Time.Wait(api.DownloadNetworkAsync(cid)); // while (file.IsPartial) Thread.Sleep(100); // using var outfile = File.OpenWrite("C:\\Users\\thatb\\Desktop\\output.png"); // file.Stream.CopyTo(outfile); // return tools.CreateHttp(GetAddress(), baseUrl: "/api/codex/v1", CheckContainerCrashed, Container.Name); //} //private IHttp LongHttp() //{ // return tools.CreateHttp(GetAddress(), baseUrl: "/api/codex/v1", CheckContainerCrashed, new LongTimeSet(), Container.Name); //} private IEndpoint GetEndpoint() { return tools .CreateHttp(CheckContainerCrashed) .CreateEndpoint(GetAddress(), "/api/codex/v1/", Container.Name); } private Address GetAddress() { return Container.GetAddress(tools.GetLog(), CodexContainerRecipe.ApiPortTag); } private void CheckContainerCrashed(HttpClient client) { if (hasContainerCrashed) throw new Exception("Container has crashed."); } public void Log(Stream crashLog) { var log = tools.GetLog(); var file = log.CreateSubfile(); log.Log($"Container {Container.Name} has crashed. Downloading crash log to '{file.FullFilename}'..."); file.Write($"Container Crash Log for {Container.Name}."); using var reader = new StreamReader(crashLog); var line = reader.ReadLine(); while (line != null) { file.Write(line); line = reader.ReadLine(); } log.Log("Crash log successfully downloaded."); hasContainerCrashed = true; } } }