1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  package org.waarp.openr66.protocol.localhandler.packet;
21  
22  import io.netty.buffer.ByteBuf;
23  import io.netty.buffer.ByteBufAllocator;
24  import org.waarp.common.utility.WaarpNettyUtil;
25  import org.waarp.common.utility.WaarpStringUtils;
26  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
27  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28  
29  import java.nio.charset.Charset;
30  
31  
32  
33  
34  
35  
36  public class EndTransferPacket extends AbstractLocalPacket {
37    private static final byte ASKVALIDATE = 0;
38  
39    private static final byte ANSWERVALIDATE = 1;
40  
41    private final byte request;
42  
43    private byte way;
44  
45    private String hashOptional;
46  
47    
48  
49  
50  
51  
52  
53  
54  
55  
56  
57    public static EndTransferPacket createFromBuffer(final int headerLength,
58                                                     final int middleLength,
59                                                     final int endLength,
60                                                     final ByteBuf buf)
61        throws OpenR66ProtocolPacketException {
62      if (headerLength - 1 != 1) {
63        throw new OpenR66ProtocolPacketException("Not enough data");
64      }
65      if (middleLength != 1) {
66        throw new OpenR66ProtocolPacketException("Not enough data");
67      }
68      final byte bheader = buf.readByte();
69      final byte valid = buf.readByte();
70      final String optional;
71      if (endLength > 0) {
72        optional =
73            buf.toString(buf.readerIndex(), endLength, Charset.defaultCharset());
74        buf.skipBytes(endLength);
75        return new EndTransferPacket(bheader, valid, optional);
76      }
77      return new EndTransferPacket(bheader, valid);
78    }
79  
80    
81  
82  
83  
84  
85    private EndTransferPacket(final byte request, final byte valid,
86                              final String hashOptional) {
87      this.request = request;
88      way = valid;
89      this.hashOptional = hashOptional;
90    }
91  
92    
93  
94  
95  
96    private EndTransferPacket(final byte request, final byte valid) {
97      this.request = request;
98      way = valid;
99    }
100 
101   
102 
103 
104   public EndTransferPacket(final byte request) {
105     this.request = request;
106     way = ASKVALIDATE;
107   }
108 
109   
110 
111 
112 
113   public EndTransferPacket(final byte request, final String hashOptional) {
114     this.request = request;
115     way = ASKVALIDATE;
116     this.hashOptional = hashOptional;
117   }
118 
119   @Override
120   public final boolean hasGlobalBuffer() {
121     return true;
122   }
123 
124   @Override
125   public final synchronized void createAllBuffers(
126       final LocalChannelReference lcr, final int networkHeader) {
127     final int headerSize = 1;
128     final int middleSize = 1;
129     final byte[] endBytes =
130         hashOptional != null? hashOptional.getBytes(WaarpStringUtils.UTF8) :
131             EMPTY_ARRAY;
132     final int endSize = endBytes.length;
133     final int globalSize =
134         networkHeader + LOCAL_HEADER_SIZE + headerSize + middleSize + endSize;
135     int offset = networkHeader + LOCAL_HEADER_SIZE;
136     global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
137     header = WaarpNettyUtil.slice(global, offset, headerSize);
138     header.writeByte(request);
139     offset += headerSize;
140     middle = WaarpNettyUtil.slice(global, offset, middleSize);
141     middle.writeByte(way);
142     offset += middleSize;
143     end = WaarpNettyUtil.slice(global, offset, endSize);
144     if (hashOptional != null) {
145       end.writeBytes(endBytes);
146     }
147   }
148 
149   @Override
150   public final byte getType() {
151     return LocalPacketFactory.ENDTRANSFERPACKET;
152   }
153 
154   @Override
155   public final String toString() {
156     return "EndTransferPacket: " + request + ' ' + way +
157            (hashOptional != null? ' ' + hashOptional : "");
158   }
159 
160   
161 
162 
163   public final byte getRequest() {
164     return request;
165   }
166 
167   
168 
169 
170   public final boolean isToValidate() {
171     return way == ASKVALIDATE;
172   }
173 
174   
175 
176 
177   public final void validate() {
178     way = ANSWERVALIDATE;
179     clear();
180   }
181 
182   
183 
184 
185   public final String getOptional() {
186     return hashOptional;
187   }
188 
189   
190 
191 
192   public final void setOptional(final String optional) {
193     hashOptional = optional;
194   }
195 }