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 io.netty.buffer.Unpooled;
25  import org.waarp.common.utility.WaarpNettyUtil;
26  import org.waarp.openr66.protocol.exception.OpenR66ProtocolPacketException;
27  import org.waarp.openr66.protocol.localhandler.LocalChannelReference;
28  
29  
30  
31  
32  
33  
34  public class KeepAlivePacket extends AbstractLocalPacket {
35    private static final byte ASKVALIDATE = 0;
36  
37    private static final byte ANSWERVALIDATE = 1;
38  
39    private byte way;
40  
41    
42  
43  
44  
45  
46  
47  
48  
49  
50  
51    public static KeepAlivePacket createFromBuffer(final int headerLength,
52                                                   final int middleLength,
53                                                   final int endLength,
54                                                   final ByteBuf buf)
55        throws OpenR66ProtocolPacketException {
56      if (middleLength != 1) {
57        throw new OpenR66ProtocolPacketException("Not enough data");
58      }
59      final byte valid = buf.readByte();
60      return new KeepAlivePacket(valid);
61    }
62  
63    
64  
65  
66    private KeepAlivePacket(final byte valid) {
67      way = valid;
68    }
69  
70    
71  
72  
73    public KeepAlivePacket() {
74      way = ASKVALIDATE;
75    }
76  
77    @Override
78    public final boolean hasGlobalBuffer() {
79      return true;
80    }
81  
82    @Override
83    public final synchronized void createAllBuffers(
84        final LocalChannelReference lcr, final int networkHeader) {
85      final int globalSize = networkHeader + LOCAL_HEADER_SIZE + 1;
86      final int offset = networkHeader + LOCAL_HEADER_SIZE;
87      global = ByteBufAllocator.DEFAULT.ioBuffer(globalSize, globalSize);
88      middle = WaarpNettyUtil.slice(global, offset, 1);
89      middle.writeByte(way);
90      end = Unpooled.EMPTY_BUFFER;
91      header = Unpooled.EMPTY_BUFFER;
92    }
93  
94    @Override
95    public final byte getType() {
96      return LocalPacketFactory.KEEPALIVEPACKET;
97    }
98  
99    @Override
100   public final String toString() {
101     return "KeepAlivePacket: " + way;
102   }
103 
104   
105 
106 
107   public final boolean isToValidate() {
108     return way == ASKVALIDATE;
109   }
110 
111   
112 
113 
114   public final void validate() {
115     way = ANSWERVALIDATE;
116     clear();
117   }
118 }