use of com.google.protobuf.CodedInputStream in project hpcourse by cscenter.
the class SocketConnectionThread method run.
@Override
public void run() {
try {
CodedInputStream inputStream = CodedInputStream.newInstance(socket.getInputStream());
int messageLength = inputStream.readRawVarint32();
Protocol.ServerRequest request = Protocol.ServerRequest.parseFrom(inputStream.readRawBytes(messageLength));
if (request.hasSubmit()) {
int id;
long[] params = getTaskParams(request.getSubmit().getTask());
if (params == null) {
// Error during extracting params from task
sendResponse(getTaskResponse(-1, false));
return;
} else {
id = Server.idCounter.getAndIncrement();
Server.m.put(id, new CustomTaskDescription(params, request.getClientId(), -1L));
sendResponse(getTaskResponse(id, true));
}
long result = task(params[0], params[1], params[2], params[3], params[4]);
synchronized (Server.m.get(id)) {
CustomTaskDescription description = Server.m.get(id);
description.setResult(result);
description.notifyAll();
}
} else if (request.hasSubscribe()) {
int requested_id = request.getSubscribe().getTaskId();
try {
CustomTaskDescription description;
long value;
synchronized (Server.m.get(requested_id)) {
description = Server.m.get(requested_id);
if (description.getResult() < 0L) {
// task is not done
description.wait();
}
value = description.getResult();
}
sendResponse(getSubscribeResponse(value, true));
} catch (Exception e) {
sendResponse(getSubscribeResponse(-1L, false));
}
} else if (request.hasList()) {
Set<Integer> keySet = Server.m.keySet();
CustomTaskDescription[] tasks;
synchronized (Server.m) {
tasks = new CustomTaskDescription[keySet.size()];
for (Integer id : keySet) {
tasks[id] = Server.m.get(id);
}
}
sendResponse(getListTasksResponse(tasks));
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.google.protobuf.CodedInputStream in project incubator-heron by apache.
the class ExtraActionUtils method getExtraActionInfo.
public static ExtraActionInfo getExtraActionInfo(String extraActionFile) {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
ExtraActionsBase.registerAllExtensions(registry);
try (InputStream stream = Files.newInputStream(Paths.get(extraActionFile))) {
CodedInputStream coded = CodedInputStream.newInstance(stream);
return ExtraActionInfo.parseFrom(coded, registry);
} catch (IOException e) {
throw new RuntimeException("ERROR: failed to deserialize extra action file " + extraActionFile + ": " + e.getMessage(), e);
}
}
use of com.google.protobuf.CodedInputStream in project atlasdb by palantir.
the class PaxosStateLogImpl method getBytesAndCheckChecksum.
/**
* Gets the data payload of the given file (data minus header) and verfies the header checksum.
*
* @param file to read data bytes from
* @return data after the checksum in the file
* @throws IOException when the data checksum fails or there is another problem reading from disk
*/
private byte[] getBytesAndCheckChecksum(File file) throws IOException {
lock.lock();
try {
InputStream fileIn = null;
PaxosPersistence.PaxosHeader.Builder headerBuilder = PaxosPersistence.PaxosHeader.newBuilder();
try {
fileIn = new FileInputStream(file);
headerBuilder.mergeDelimitedFrom(fileIn);
CodedInputStream in = CodedInputStream.newInstance(fileIn);
byte[] bytes = in.readBytes().toByteArray();
byte[] checksum = Sha256Hash.computeHash(bytes).getBytes();
if (Arrays.equals(headerBuilder.getChecksum().toByteArray(), checksum)) {
return bytes;
} else {
throw new CorruptLogFileException();
}
} catch (FileNotFoundException e) {
// TODO (jkong): Check if this is intentional, or if the author intended FileNotFound to be a problem
// that should be treated in the same way as IOException.
} catch (IOException e) {
// Note that the file name is a Paxos log entry - so it is the round number - and thus safe.
log.error("Problem reading paxos state, specifically when reading file {} (file-name {})", UnsafeArg.of("full path", file.getAbsolutePath()), SafeArg.of("file name", file.getName()));
throw Throwables.rewrap(e);
} finally {
IOUtils.closeQuietly(fileIn);
}
} finally {
lock.unlock();
}
return null;
}
use of com.google.protobuf.CodedInputStream in project motan by weibocom.
the class ProtobufSerialization method deserializeMulti.
@Override
public Object[] deserializeMulti(byte[] data, Class<?>[] classes) throws IOException {
CodedInputStream in = CodedInputStream.newInstance(data);
Object[] objects = new Object[classes.length];
for (int i = 0; i < classes.length; i++) {
objects[i] = deserialize(in, classes[i]);
}
return objects;
}
use of com.google.protobuf.CodedInputStream in project platformlayer by platformlayer.
the class JobLogStoreBase method deserialize.
protected JobLog deserialize(InputSupplier<? extends InputStream> iss, int logSkip) throws IOException {
ArrayList<JobLogLine> lines = Lists.newArrayList();
InputStream is = null;
CodedInputStream in = null;
try {
is = iss.getInput();
is = new GZIPInputStream(is);
in = CodedInputStream.newInstance(is);
int i = 0;
JobDataProtobuf.JobLogLine.Builder protobuf = JobDataProtobuf.JobLogLine.newBuilder();
while (!in.isAtEnd()) {
int length = in.readRawVarint32();
if (i < logSkip) {
in.skipRawBytes(length);
} else {
int oldLimit = in.pushLimit(length);
protobuf.clear();
protobuf.mergeFrom(in);
JobLogLine line = new JobLogLine();
line.level = protobuf.getLevel();
line.timestamp = protobuf.getTimestamp();
line.message = protobuf.getMessage();
line.type = protobuf.getType();
if (protobuf.hasException()) {
line.exception = mapFromProtobuf(protobuf.getExceptionBuilder());
}
lines.add(line);
in.popLimit(oldLimit);
}
i++;
}
} finally {
// Closeables.closeQuietly(in);
Closeables.closeQuietly(is);
}
JobLog jobLog = new JobLog();
jobLog.lines = lines;
return jobLog;
}
Aggregations