Method.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.transport;
022 
023 import org.apache.qpid.transport.network.Frame;
024 
025 import java.nio.ByteBuffer;
026 
027 import static org.apache.qpid.transport.util.Functions.*;
028 
029 /**
030  * Method
031  *
032  @author Rafael H. Schloming
033  */
034 
035 public abstract class Method extends Struct implements ProtocolEvent
036 {
037 
038     public static final Method create(int type)
039     {
040         // XXX: should generate separate factories for separate
041         // namespaces
042         return (MethodStructFactory.createInstruction(type);
043     }
044 
045     // XXX: command subclass?
046     private int id;
047     private int channel;
048     private boolean idSet = false;
049     private boolean sync = false;
050     private boolean batch = false;
051     private boolean unreliable = false;
052 
053     public final int getId()
054     {
055         return id;
056     }
057 
058     void setId(int id)
059     {
060         this.id = id;
061         this.idSet = true;
062     }
063 
064     public final int getChannel()
065     {
066         return channel;
067     }
068 
069     public final void setChannel(int channel)
070     {
071         this.channel = channel;
072     }
073 
074     public final boolean isSync()
075     {
076         return sync;
077     }
078 
079     final void setSync(boolean value)
080     {
081         this.sync = value;
082     }
083 
084     public final boolean isBatch()
085     {
086         return batch;
087     }
088 
089     final void setBatch(boolean value)
090     {
091         this.batch = value;
092     }
093 
094     public final boolean isUnreliable()
095     {
096         return unreliable;
097     }
098 
099     final void setUnreliable(boolean value)
100     {
101         this.unreliable = value;
102     }
103 
104     public abstract boolean hasPayload();
105 
106     public Header getHeader()
107     {
108         return null;
109     }
110 
111     public void setHeader(Header header)
112     {
113         throw new UnsupportedOperationException();
114     }
115 
116     public ByteBuffer getBody()
117     {
118         return null;
119     }
120 
121     public void setBody(ByteBuffer body)
122     {
123         throw new UnsupportedOperationException();
124     }
125 
126     public int getBodySize()
127     {
128         ByteBuffer body = getBody();
129         if (body == null)
130         {
131             return 0;
132         }
133         else
134         {
135             return body.remaining();
136         }
137     }
138 
139     public abstract byte getEncodedTrack();
140 
141     public abstract <C> void dispatch(C context, MethodDelegate<C> delegate);
142 
143     public <C> void delegate(C context, ProtocolDelegate<C> delegate)
144     {
145         if (getEncodedTrack() == Frame.L4)
146         {
147             delegate.command(context, this);
148         }
149         else
150         {
151             delegate.control(context, this);
152         }
153     }
154 
155     public String toString()
156     {
157         StringBuilder str = new StringBuilder();
158 
159         str.append("ch=");
160         str.append(channel);
161 
162         if (getEncodedTrack() == Frame.L4 && idSet)
163         {
164             str.append(" id=");
165             str.append(id);
166         }
167 
168         if (sync || batch)
169         {
170             str.append(" ");
171             str.append("[");
172             if (sync)
173             {
174                 str.append("S");
175             }
176             if (batch)
177             {
178                 str.append("B");
179             }
180             str.append("]");
181         }
182 
183         str.append(" ");
184         str.append(super.toString());
185         Header hdr = getHeader();
186         if (hdr != null)
187         {
188             for (Struct st : hdr.getStructs())
189             {
190                 str.append("\n  ");
191                 str.append(st);
192             }
193         }
194         ByteBuffer body = getBody();
195         if (body != null)
196         {
197             str.append("\n  body=");
198             str.append(str(body, 64));
199         }
200 
201         return str.toString();
202     }
203 
204 }