Search in sources :

Example 21 with CodedInputStream

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();
    }
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) IOException(java.io.IOException) Protocol(communication.Protocol) IOException(java.io.IOException)

Example 22 with CodedInputStream

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);
    }
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) IOException(java.io.IOException) ExtensionRegistry(com.google.protobuf.ExtensionRegistry)

Example 23 with CodedInputStream

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;
}
Also used : FileInputStream(java.io.FileInputStream) CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 24 with CodedInputStream

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;
}
Also used : CodedInputStream(com.google.protobuf.CodedInputStream)

Example 25 with CodedInputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) JobLogLine(org.platformlayer.jobs.model.JobLogLine) GZIPInputStream(java.util.zip.GZIPInputStream) CodedInputStream(com.google.protobuf.CodedInputStream) InputStream(java.io.InputStream) CodedInputStream(com.google.protobuf.CodedInputStream) JobLog(org.platformlayer.jobs.model.JobLog)

Aggregations

CodedInputStream (com.google.protobuf.CodedInputStream)26 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteString (com.google.protobuf.ByteString)3 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)3 BytesWritable (org.apache.hadoop.io.BytesWritable)3 Text (org.apache.hadoop.io.Text)3 ChangedFile (boa.types.Diff.ChangedFile)2 Protocol (communication.Protocol)2 FileInputStream (java.io.FileInputStream)2 WireMessage (org.apache.calcite.avatica.proto.Common.WireMessage)2 CompiledFile (android.aapt.pb.internal.ResourcesInternal.CompiledFile)1 IssuesRoot (boa.types.Issues.IssuesRoot)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 Parser (com.google.protobuf.Parser)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 KnownLength (io.grpc.KnownLength)1 PrototypeMarshaller (io.grpc.MethodDescriptor.PrototypeMarshaller)1 BufferedInputStream (java.io.BufferedInputStream)1