-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGRPCFileReaderImplementation.cs
More file actions
212 lines (179 loc) · 9.43 KB
/
GRPCFileReaderImplementation.cs
File metadata and controls
212 lines (179 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
---- Copyright Start ----
This file is part of the OpenVectorFormatTools collection. This collection provides tools to facilitate the usage of the OpenVectorFormat.
Copyright (C) 2024 Digital-Production-Aachen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
---- Copyright End ----
*/
using Grpc.Core;
using GrpcReaderWriterInterface;
using System;
using System.IO;
using System.Threading.Tasks;
using OpenVectorFormat.FileReaderWriterFactory;
using OpenVectorFormat.AbstractReaderWriter;
namespace OpenVectorFormat.FileHandlerFactoryGRPCWrapper
{
/// <summary>
/// Implements server calls for grpc connection
/// </summary>
partial class GRPCWrapperFunctionsImplementation : VectorFileHandler.VectorFileHandlerBase
{
/// <summary>
/// Simple all-at-once reading of job
/// </summary>
/// <param name="readRequest">Input Message with flename</param>
/// <param name="context"></param>
/// <returns>SimpleJobReadOutput with message and <see cref="Job"/></returns>
public override async Task<SimpleJobReadReply> SimpleJobRead(SimpleJobReadRequest readRequest, ServerCallContext context)
{
Console.WriteLine("\"SimpleJobRead\" called for " + readRequest.JobUri);
string filename = readRequest.JobUri;
string extension = Path.GetExtension(filename);
if (!FileReaderFactory.SupportedFileFormats.Contains(extension))
{
string supFormats = string.Join(";", FileReaderFactory.SupportedFileFormats);
throw new RpcException(new Status(StatusCode.FailedPrecondition, "Reading failed: FileFormat " + extension + " is not supported!\n Supported formats are " + supFormats));
}
if (!File.Exists(filename))
{
throw new RpcException(new Status(StatusCode.FailedPrecondition, "Reading failed: File " + filename + " does not exist."));
}
FileReader reader = FileReaderFactory.CreateNewReader(extension);
reader.OpenJob(filename, new FileReaderWriterProgress());
SimpleJobReadReply ret = new SimpleJobReadReply();
ret.InfoMessage = "File read from " + filename + "!";
ret.Job = reader.CacheJobToMemory();
reader.Dispose();
return ret;
}
public override async Task PartialRead(Grpc.Core.IAsyncStreamReader<PartialReadRequest> requestStream, Grpc.Core.IServerStreamWriter<PartialReadReply> responseStream, Grpc.Core.ServerCallContext context)
{
bool disposeReader = false;
bool readerStarted = false;
string jobURI = null;
FileReader reader= null;
FileReaderWriterProgress progress = null;
while (await requestStream.MoveNext() && !disposeReader)
{
PartialReadRequest inputMsg = requestStream.Current;
PartialReadReply outputMsg = new PartialReadReply();
outputMsg.RequestId = inputMsg.RequestId;
switch (inputMsg.SelectedCommandMode)
{
case PartialReadCommandMode.OpenJob:
Console.WriteLine("\"OpenJob\" command called for " + inputMsg.JobUri);
jobURI = inputMsg.JobUri;
string extension = Path.GetExtension(jobURI);
if (!FileReaderFactory.SupportedFileFormats.Contains(extension))
{
string supFormats = string.Join(";", FileReaderFactory.SupportedFileFormats);
throw new RpcException(new Status(StatusCode.InvalidArgument, "Reading failed: FileFormat " + extension + " is not supported!\n Supported formats are " + supFormats));
}
if (!File.Exists(jobURI))
{
throw new RpcException(new Status(StatusCode.InvalidArgument, "File " + jobURI + " does not exist!"));
}
reader = FileReaderFactory.CreateNewReader(extension);
reader.AutomatedCachingThresholdBytes = Config.AutomatedCachingThresholdBytes;
progress = new FileReaderWriterProgress();
try
{
reader.OpenJob(jobURI, progress);
}
catch (Exception ex)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.Internal, "Opening file " + jobURI + " for reading failed: " + ex.Message));
}
readerStarted = true;
break;
case PartialReadCommandMode.UnloadJobFromMemory:
// Console.WriteLine("\"UnloadJobFromMemory\" command called");
if (!readerStarted)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.FailedPrecondition, "No active reader found. Start with \"OpenJobfile\" command."));
}
try
{
reader.UnloadJobFromMemory();
}
catch (Exception ex)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.Internal, "UnloadJobFromMemory failed: " + ex.Message));
}
break;
case PartialReadCommandMode.GetJobShell:
// Console.WriteLine("\"GetJobShell\" command called");
if (!readerStarted)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.FailedPrecondition, "No active reader found. Start with \"OpenJobfile\" command."));
}
try
{
outputMsg.JobShell = reader.JobShell.Clone();
}
catch (Exception ex)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.Internal, "GetJobShell failed: " + ex.Message));
}
break;
case PartialReadCommandMode.GetPlane:
// Console.WriteLine("\"GetPlane\" command called");
if (!readerStarted)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.FailedPrecondition, "No active reader found. Start with \"OpenJobfile\" command."));
}
try
{
outputMsg.WorkPlane = reader.GetWorkPlane(inputMsg.PlaneIndex);
}
catch (Exception ex)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.Internal, "GetPlane failed: " + ex.Message));
}
break;
case PartialReadCommandMode.GetVectorBlock:
// Console.WriteLine("\"GetVectorBlock\" command called");
if (!readerStarted)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.FailedPrecondition, "No active reader found. Start with \"OpenJobfile\" command."));
}
try
{
outputMsg.VectorBlock = reader.GetVectorBlock(inputMsg.PlaneIndex, inputMsg.VectorBlockIndex);
}
catch (Exception ex)
{
reader?.Dispose();
throw new RpcException(new Status(StatusCode.Internal, "GetVectorBlock failed: " + ex.Message));
}
break;
//default:
// reader?.Dispose();
// throw new RpcException(new Status(StatusCode.FailedPrecondition, "Unrecognized PartialReadCommandMode " + inputMsg.SelectedCommandMode));
}
if (inputMsg.ReflectRequest) { outputMsg.Request = inputMsg; }
await responseStream.WriteAsync(outputMsg);
}
reader?.Dispose();
}
}
}