private void upgradeConnection(ClientUpgradeResponse response) {
    EndPoint endp = getEndPoint();
    Executor executor = getExecutor();
    WebSocketClientConnection connection = new WebSocketClientConnection(endp, executor, client);

    // Initialize / Negotiate Extensions
    EventDriver websocket = client.getWebSocket();
    WebSocketPolicy policy = client.getPolicy();
    String acceptedSubProtocol = response.getAcceptedSubProtocol();

    WebSocketSession session = new WebSocketSession(request.getRequestURI(), websocket, connection);
    session.setPolicy(policy);
    session.setNegotiatedSubprotocol(acceptedSubProtocol);

    connection.setSession(session);
    List<Extension> extensions = client.getFactory().initExtensions(response.getExtensions());

    // Start with default routing.
    IncomingFrames incoming = session;
    // OutgoingFrames outgoing = connection;

    // Connect extensions
    if (extensions != null) {
      connection.getParser().configureFromExtensions(extensions);
      connection.getGenerator().configureFromExtensions(extensions);

      // FIXME
      // Iterator<Extension> extIter;
      // // Connect outgoings
      // extIter = extensions.iterator();
      // while (extIter.hasNext())
      // {
      // Extension ext = extIter.next();
      // ext.setNextOutgoingFrames(outgoing);
      // outgoing = ext;
      // }
      //
      // // Connect incomings
      // Collections.reverse(extensions);
      // extIter = extensions.iterator();
      // while (extIter.hasNext())
      // {
      // Extension ext = extIter.next();
      // ext.setNextIncomingFrames(incoming);
      // incoming = ext;
      // }
    }

    // configure session for outgoing flows
    // session.setOutgoing(outgoing);
    // configure connection for incoming flows
    connection.getParser().setIncomingFramesHandler(incoming);

    // Now swap out the connection
    endp.setConnection(connection);
    connection.onOpen();
  }
  public UpgradeConnection(EndPoint endp, Executor executor, DefaultWebSocketClient client) {
    super(endp, executor);
    this.client = client;
    this.bufferPool = client.getFactory().getBufferPool();
    this.parser = new HttpResponseHeaderParser();

    try {
      this.request = client.getUpgradeRequest();
    } catch (ClassCastException e) {
      client.failed(new RuntimeException("Invalid Upgrade Request structure", e));
    }
  }
 public WebSocketClientConnection(
     EndPoint endp, Executor executor, DefaultWebSocketClient client) {
   super(
       endp,
       executor,
       client.getFactory().getScheduler(),
       client.getPolicy(),
       client.getFactory().getBufferPool());
   this.client = client;
   this.factory = client.getFactory();
   this.connected = false;
   this.masker = client.getMasker();
 }
 /**
  * Read / Parse the waiting read/fill buffer
  *
  * @param buffer the buffer to fill into from the endpoint
  * @return true if there is more to read, false if reading should stop
  */
 private boolean read(ByteBuffer buffer) {
   EndPoint endPoint = getEndPoint();
   try {
     while (true) {
       int filled = endPoint.fill(buffer);
       if (filled == 0) {
         return true;
       } else if (filled < 0) {
         LOG.debug("read - EOF Reached");
         return false;
       } else {
         if (LOG.isDebugEnabled()) {
           LOG.debug("Filled {} bytes - {}", filled, BufferUtil.toDetailString(buffer));
         }
         ClientUpgradeResponse resp = parser.parse(buffer);
         if (resp != null) {
           // Got a response!
           client.setUpgradeResponse(resp);
           validateResponse(resp);
           notifyConnect(resp);
           upgradeConnection(resp);
           return false; // do no more reading
         }
       }
     }
   } catch (IOException e) {
     LOG.warn(e);
     client.failed(e);
     disconnect(false);
     return false;
   } catch (UpgradeException e) {
     LOG.warn(e);
     client.failed(e);
     disconnect(false);
     return false;
   }
 }
 private void notifyConnect(ClientUpgradeResponse response) {
   client.succeeded(response);
 }