View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.spdy.parser;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.spdy.frames.PingFrame;
24  
25  public class PingBodyParser extends ControlFrameBodyParser
26  {
27      private final ControlFrameParser controlFrameParser;
28      private State state = State.PING_ID;
29      private int cursor;
30      private int pingId;
31  
32      public PingBodyParser(ControlFrameParser controlFrameParser)
33      {
34          this.controlFrameParser = controlFrameParser;
35      }
36  
37      @Override
38      public boolean parse(ByteBuffer buffer)
39      {
40          while (buffer.hasRemaining())
41          {
42              switch (state)
43              {
44                  case PING_ID:
45                  {
46                      if (buffer.remaining() >= 4)
47                      {
48                          pingId = buffer.getInt() & 0x7F_FF_FF_FF;
49                          onPing();
50                          return true;
51                      }
52                      else
53                      {
54                          state = State.PING_ID_BYTES;
55                          cursor = 4;
56                      }
57                      break;
58                  }
59                  case PING_ID_BYTES:
60                  {
61                      byte currByte = buffer.get();
62                      --cursor;
63                      pingId += (currByte & 0xFF) << 8 * cursor;
64                      if (cursor == 0)
65                      {
66                          onPing();
67                          return true;
68                      }
69                      break;
70                  }
71                  default:
72                  {
73                      throw new IllegalStateException();
74                  }
75              }
76          }
77          return false;
78      }
79  
80      private void onPing()
81      {
82          PingFrame frame = new PingFrame(controlFrameParser.getVersion(), pingId);
83          controlFrameParser.onControlFrame(frame);
84          reset();
85      }
86  
87      private void reset()
88      {
89          state = State.PING_ID;
90          cursor = 0;
91          pingId = 0;
92      }
93  
94      private enum State
95      {
96          PING_ID, PING_ID_BYTES
97      }
98  }