SammyVimes commented on a change in pull request #70: URL: https://github.com/apache/ignite-3/pull/70#discussion_r598697442
########## File path: modules/network/src/main/java/org/apache/ignite/network/scalecube/ScaleCubeMessageCodec.java ########## @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.network.scalecube; + +import io.scalecube.cluster.transport.api.Message; +import io.scalecube.cluster.transport.api.MessageCodec; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; +import org.apache.ignite.network.MessageMapper; +import org.apache.ignite.network.MessageMappingException; +import org.apache.ignite.network.NetworkMessage; + +/** + * Serializes and deserialized messages in ScaleCube cluster. + */ +class ScaleCubeMessageCodec implements MessageCodec { + /** Header name for {@link NetworkMessage#type()}. */ + public static final String HEADER_MESSAGE_TYPE = "type"; + + /** Map message type -> {@link MessageMapper} */ + private final Map<Short, MessageMapper> messageMapperMap; + + /** + * Constructor. + * @param map Message mapper map. + */ + ScaleCubeMessageCodec(Map<Short, MessageMapper> map) { + messageMapperMap = map; + } + + /** {@inheritDoc} */ + @Override public Message deserialize(InputStream stream) throws Exception { + Message.Builder builder = Message.builder(); + try (ObjectInputStream ois = new ObjectInputStream(stream)) { + // headers + int headersSize = ois.readInt(); + Map<String, String> headers = new HashMap<>(headersSize); + for (int i = 0; i < headersSize; i++) { + String name = ois.readUTF(); + String value = (String) ois.readObject(); + headers.put(name, value); + } + + builder.headers(headers); + + String typeString = headers.get(HEADER_MESSAGE_TYPE); + + if (typeString == null) { + builder.data(ois.readObject()); + return builder.build(); + } + + short type; + try { + type = Short.parseShort(typeString); + } + catch (NumberFormatException e) { + throw new MessageMappingException("Type is not short", e); + } + + MessageMapper mapper = messageMapperMap.get(type); + + assert mapper != null : "No mapper defined for type " + type; + + NetworkMessage message = mapper.readMessage(ois); + builder.data(message); + } + return builder.build(); + } + + /** {@inheritDoc} */ + @Override public void serialize(Message message, OutputStream stream) throws Exception { + final Object data = message.data(); + + if (!(data instanceof NetworkMessage)) { + try (ObjectOutputStream oos = new ObjectOutputStream(stream)) { Review comment: Well, I looked this up in the default `JdkMessageCodec`. ScaleCube does the same there, though it's not really required, because there is a try-with-resources in TransportImpl. I guess we can leave it as it is until we have our own Transport implementation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
