1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.oncrpc;
19 
20 /**
21  * Represent an RPC message as defined in RFC 1831.
22  */
23 public abstract class RpcMessage {
24   /** Message type */
25   public static enum Type {
26     // the order of the values below are significant.
27     RPC_CALL,
28     RPC_REPLY;
29 
getValue()30     public int getValue() {
31       return ordinal();
32     }
33 
fromValue(int value)34     public static Type fromValue(int value) {
35       if (value < 0 || value >= values().length) {
36         return null;
37       }
38       return values()[value];
39     }
40   }
41 
42   protected final int xid;
43   protected final Type messageType;
44 
RpcMessage(int xid, Type messageType)45   RpcMessage(int xid, Type messageType) {
46     if (messageType != Type.RPC_CALL && messageType != Type.RPC_REPLY) {
47       throw new IllegalArgumentException("Invalid message type " + messageType);
48     }
49     this.xid = xid;
50     this.messageType = messageType;
51   }
52 
write(XDR xdr)53   public abstract XDR write(XDR xdr);
54 
getXid()55   public int getXid() {
56     return xid;
57   }
58 
getMessageType()59   public Type getMessageType() {
60     return messageType;
61   }
62 
validateMessageType(Type expected)63   protected void validateMessageType(Type expected) {
64     if (expected != messageType) {
65       throw new IllegalArgumentException("Message type is expected to be "
66           + expected + " but got " + messageType);
67     }
68   }
69 }
70