Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.network.protocol;
0019 
0020 import java.util.Objects;
0021 
0022 import io.netty.buffer.ByteBuf;
0023 import org.apache.commons.lang3.builder.ToStringBuilder;
0024 import org.apache.commons.lang3.builder.ToStringStyle;
0025 
0026 import org.apache.spark.network.buffer.ManagedBuffer;
0027 import org.apache.spark.network.buffer.NettyManagedBuffer;
0028 
0029 /**
0030  * A RPC that does not expect a reply, which is handled by a remote
0031  * {@link org.apache.spark.network.server.RpcHandler}.
0032  */
0033 public final class OneWayMessage extends AbstractMessage implements RequestMessage {
0034 
0035   public OneWayMessage(ManagedBuffer body) {
0036     super(body, true);
0037   }
0038 
0039   @Override
0040   public Message.Type type() { return Type.OneWayMessage; }
0041 
0042   @Override
0043   public int encodedLength() {
0044     // The integer (a.k.a. the body size) is not really used, since that information is already
0045     // encoded in the frame length. But this maintains backwards compatibility with versions of
0046     // RpcRequest that use Encoders.ByteArrays.
0047     return 4;
0048   }
0049 
0050   @Override
0051   public void encode(ByteBuf buf) {
0052     // See comment in encodedLength().
0053     buf.writeInt((int) body().size());
0054   }
0055 
0056   public static OneWayMessage decode(ByteBuf buf) {
0057     // See comment in encodedLength().
0058     buf.readInt();
0059     return new OneWayMessage(new NettyManagedBuffer(buf.retain()));
0060   }
0061 
0062   @Override
0063   public int hashCode() {
0064     return Objects.hashCode(body());
0065   }
0066 
0067   @Override
0068   public boolean equals(Object other) {
0069     if (other instanceof OneWayMessage) {
0070       OneWayMessage o = (OneWayMessage) other;
0071       return super.equals(o);
0072     }
0073     return false;
0074   }
0075 
0076   @Override
0077   public String toString() {
0078     return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
0079       .append("body", body())
0080       .toString();
0081   }
0082 }