Search in sources :

Example 1 with MethodDescriptorContainer

use of org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer in project BIMserver by opensourceBIM.

the class Handler method run.

@Override
public void run() {
    running = true;
    try {
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        String token = dis.readUTF();
        ReflectiveRpcChannel reflectiveRpcChannel = new ReflectiveRpcChannel(ServiceInterface.class, serviceFactory.get(token, AccessMethod.INTERNAL).get(ServiceInterface.class), protocolBuffersMetaData, servicesMap);
        while (running) {
            String serviceName = dis.readUTF();
            String methodName = dis.readUTF();
            MethodDescriptorContainer methodDescriptorContainer;
            try {
                methodDescriptorContainer = protocolBuffersMetaData.getMethod(serviceName, methodName);
                Builder newBuilder = DynamicMessage.newBuilder(methodDescriptorContainer.getInputDescriptor());
                newBuilder.mergeDelimitedFrom(dis);
                DynamicMessage request = newBuilder.build();
                Message response = reflectiveRpcChannel.callBlockingMethod(methodDescriptorContainer, request);
                response.writeDelimitedTo(socket.getOutputStream());
            } catch (ServiceNotFoundException e) {
                // TODO should return a usable pb message for the user
                LOGGER.error("", e);
            } catch (ServiceMethodNotFoundException e) {
                // TODO should return a usable pb message for the user
                LOGGER.error("", e);
            }
        }
    } catch (IOException e) {
        LOGGER.error("", e);
    } catch (UserException e) {
        LOGGER.error("", e);
    }
    socketNotificationsClient.notifyDisconnect();
}
Also used : ReflectiveRpcChannel(org.bimserver.shared.pb.ReflectiveRpcChannel) MethodDescriptorContainer(org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) ServiceNotFoundException(org.bimserver.shared.pb.ServiceNotFoundException) ServiceInterface(org.bimserver.shared.interfaces.ServiceInterface) Builder(com.google.protobuf.DynamicMessage.Builder) DynamicMessage(com.google.protobuf.DynamicMessage) IOException(java.io.IOException) UserException(org.bimserver.shared.exceptions.UserException) DataInputStream(java.io.DataInputStream) ServiceMethodNotFoundException(org.bimserver.shared.pb.ServiceMethodNotFoundException)

Example 2 with MethodDescriptorContainer

use of org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer in project BIMserver by opensourceBIM.

the class ProtocolBuffersConnectionHandler method run.

@Override
public void run() {
    try {
        running = true;
        while (running) {
            String serviceName = dataInputStream.readUTF();
            String methodName = dataInputStream.readUTF();
            String token = dataInputStream.readUTF();
            ProtocolBuffersMetaData protocolBuffersMetaData = protocolBuffersServer.getProtocolBuffersMetaData();
            SService sService = servicesMap.getBySimpleName(serviceName);
            if (sService == null) {
                throw new UserException("No service found with name " + serviceName);
            }
            SMethod method = sService.getSMethod(methodName);
            if (method == null) {
                throw new UserException("Method " + methodName + " not found on " + serviceName);
            }
            Class<? extends PublicInterface> clazz = sService.getInterfaceClass();
            PublicInterface service = null;
            if (token.equals("")) {
                service = serviceFactory.get(AccessMethod.PROTOCOL_BUFFERS).get(clazz);
            } else {
                service = serviceFactory.get(token, AccessMethod.PROTOCOL_BUFFERS).get(clazz);
            }
            ReflectiveRpcChannel reflectiveRpcChannel = new ReflectiveRpcChannel(clazz, service, protocolBuffersMetaData, servicesMap);
            MethodDescriptorContainer pbMethod = protocolBuffersMetaData.getMethod(serviceName, methodName);
            Builder requestBuilder = DynamicMessage.getDefaultInstance(pbMethod.getInputDescriptor()).newBuilderForType();
            requestBuilder.mergeDelimitedFrom(dataInputStream);
            Message response = reflectiveRpcChannel.callBlockingMethod(pbMethod, requestBuilder.build());
            response.writeDelimitedTo(outputStream);
            outputStream.flush();
        }
    } catch (SocketException e) {
    } catch (EOFException e) {
    } catch (Exception e) {
        if (running) {
            LOGGER.error("", e);
        }
    }
    protocolBuffersServer.unregister(this);
}
Also used : SocketException(java.net.SocketException) MethodDescriptorContainer(org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) Builder(com.google.protobuf.DynamicMessage.Builder) ProtocolBuffersMetaData(org.bimserver.shared.pb.ProtocolBuffersMetaData) IOException(java.io.IOException) EOFException(java.io.EOFException) SocketException(java.net.SocketException) UserException(org.bimserver.shared.exceptions.UserException) ReflectiveRpcChannel(org.bimserver.shared.pb.ReflectiveRpcChannel) PublicInterface(org.bimserver.shared.interfaces.PublicInterface) EOFException(java.io.EOFException) UserException(org.bimserver.shared.exceptions.UserException) SMethod(org.bimserver.shared.meta.SMethod) SService(org.bimserver.shared.meta.SService)

Example 3 with MethodDescriptorContainer

use of org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer in project BIMserver by opensourceBIM.

the class ProtocolBuffersReflector method callMethod.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Object callMethod(String interfaceName, String methodName, Class<?> definedReturnType, KeyValuePair... args) throws ServerException, UserException {
    try {
        MethodDescriptorContainer methodDescriptorContainer;
        try {
            methodDescriptorContainer = protocolBuffersMetaData.getMethod(interfaceName, methodName);
            SMethod sMethod = servicesMap.getBySimpleName(interfaceName).getSMethod(methodName);
            Descriptor inputDescriptor = methodDescriptorContainer.getInputDescriptor();
            Builder builder = DynamicMessage.newBuilder(methodDescriptorContainer.getInputDescriptor());
            int i = 0;
            for (FieldDescriptor field : inputDescriptor.getFields()) {
                Object arg = args[i].getValue();
                if (field.getJavaType() == JavaType.ENUM) {
                    EnumDescriptor enumType = field.getEnumType();
                    builder.setField(field, enumType.findValueByName(arg.toString()));
                } else {
                    if (arg instanceof SBase) {
                        builder.setField(field, convertSObjectToProtocolBuffersObject((SBase) arg, sMethod.getParameter(i).getType()));
                    } else if (arg instanceof DataHandler) {
                        DataHandler dataHandler = (DataHandler) arg;
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        try {
                            IOUtils.copy(dataHandler.getInputStream(), baos);
                        } catch (IOException e) {
                            LOGGER.error("", e);
                        }
                        builder.setField(field, ByteString.copyFrom(baos.toByteArray()));
                    } else {
                        if (arg != null) {
                            if (arg instanceof Collection) {
                                Collection col = (Collection) arg;
                                Iterator iterator = col.iterator();
                                while (iterator.hasNext()) {
                                    Object o = iterator.next();
                                    builder.addRepeatedField(field, o);
                                }
                            } else {
                                builder.setField(field, arg);
                            }
                        }
                    }
                }
                i++;
            }
            DynamicMessage message = builder.build();
            Message result = channel.callBlockingMethod(methodDescriptorContainer, message);
            FieldDescriptor errorMessageField = methodDescriptorContainer.getOutputField("errorMessage");
            String errorMessage = (String) result.getField(errorMessageField);
            if (errorMessage.equals("OKE")) {
                if (result.getDescriptorForType().getName().equals("VoidResponse")) {
                    return null;
                } else {
                    FieldDescriptor outputField = methodDescriptorContainer.getOutputField("value");
                    Object value = result.getField(outputField);
                    if (outputField.isRepeated()) {
                        if (value instanceof Collection) {
                            Collection collection = (Collection) value;
                            Collection x = null;
                            if (definedReturnType == List.class) {
                                x = new ArrayList();
                            } else if (definedReturnType == Set.class) {
                                x = new HashSet();
                            }
                            for (Object v : collection) {
                                if (v instanceof DynamicMessage) {
                                    x.add(convertProtocolBuffersMessageToSObject((DynamicMessage) v, null, sMethod.getBestReturnType()));
                                } else {
                                    x.add(v);
                                }
                            }
                            return x;
                        } else {
                            return new ArrayList();
                        }
                    } else if (outputField.getJavaType() == JavaType.ENUM) {
                        EnumDescriptor enumType = outputField.getEnumType();
                        return enumType.findValueByName(value.toString());
                    } else if (value instanceof DynamicMessage) {
                        return convertProtocolBuffersMessageToSObject((DynamicMessage) value, null, sMethod.getBestReturnType());
                    } else {
                        if (definedReturnType == Date.class) {
                            return new Date((Long) value);
                        }
                        return value;
                    }
                }
            } else {
                throw new UserException(errorMessage);
            }
        } catch (ConvertException e) {
            throw new ServerException(e);
        } catch (ServiceException e) {
            throw new ServerException(e.getMessage());
        }
    } catch (ServiceNotFoundException e) {
        throw new ServerException(e);
    } catch (ServiceMethodNotFoundException e) {
        throw new ServerException(e);
    }
}
Also used : MethodDescriptorContainer(org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer) HashSet(java.util.HashSet) Set(java.util.Set) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) Builder(com.google.protobuf.DynamicMessage.Builder) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) DynamicMessage(com.google.protobuf.DynamicMessage) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Iterator(java.util.Iterator) SMethod(org.bimserver.shared.meta.SMethod) UserException(org.bimserver.shared.exceptions.UserException) HashSet(java.util.HashSet) ServerException(org.bimserver.shared.exceptions.ServerException) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Date(java.util.Date) ServiceException(com.google.protobuf.ServiceException) SBase(org.bimserver.shared.meta.SBase) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) Collection(java.util.Collection)

Aggregations

DynamicMessage (com.google.protobuf.DynamicMessage)3 Builder (com.google.protobuf.DynamicMessage.Builder)3 Message (com.google.protobuf.Message)3 IOException (java.io.IOException)3 UserException (org.bimserver.shared.exceptions.UserException)3 MethodDescriptorContainer (org.bimserver.shared.pb.ProtocolBuffersMetaData.MethodDescriptorContainer)3 SMethod (org.bimserver.shared.meta.SMethod)2 ReflectiveRpcChannel (org.bimserver.shared.pb.ReflectiveRpcChannel)2 ByteString (com.google.protobuf.ByteString)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 EnumDescriptor (com.google.protobuf.Descriptors.EnumDescriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 ServiceException (com.google.protobuf.ServiceException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1