Package com.google.dataconnector.protocol.proto.SdcFrame

Examples of com.google.dataconnector.protocol.proto.SdcFrame.SocketDataInfo


        .setType(FrameInfo.Type.SOCKET_DATA)
        .setSequence(1)
        .setPayload(mockSocketDataInfo.toByteString())
        .build();

    SocketDataInfo continuingSocketDataInfo = SocketDataInfo.newBuilder()
        .setConnectionId(CONNECTION_ID)
        .setState(SocketDataInfo.State.CONTINUE)
        .setSegment(ByteString.copyFromUtf8(DATA))
        .build();
    FrameInfo continuingFrame = FrameInfo.newBuilder()
        .setType(FrameInfo.Type.SOCKET_DATA)
        .setSequence(1)
        .setPayload(continuingSocketDataInfo.toByteString())
        .build();

    // Execute.
    SocksDataHandler socksDataHandler = new SocksDataHandler(fakeLocalConf,
        socketFactory, localHostAddress, threadPoolExecutor, injector);
    socksDataHandler.setFrameSender(frameSender);
    socksDataHandler.dispatch(mockFrame);
    socksDataHandler.dispatch(continuingFrame);

    // Verify
    SocketDataInfo actualSocketDataInfo = queue.take();
    assertEquals(continuingSocketDataInfo, actualSocketDataInfo);

    EasyMock.verify(socketFactory, inputStreamConnector, outputStreamConnector, injector,
        threadPoolExecutor);
  }
View Full Code Here


    frameSender = new FrameSender(sendQueue, null);
  }

  public void testReceiveInputAndCreateFrames() throws Exception {

    SocketDataInfo expectedContinueSdi = SocketDataInfo.newBuilder()
        .setConnectionId(CONNECTION_ID)
        .setSegment(ByteString.copyFrom(expectedPayload))
        .setState(SocketDataInfo.State.CONTINUE)
        .build();
    FrameInfo expectedContinueFrame = FrameInfo.newBuilder()
        .setType(FrameInfo.Type.SOCKET_DATA)
        .setPayload(expectedContinueSdi.toByteString())
        .build();

    SocketDataInfo expectedClosingSdi = SocketDataInfo.newBuilder()
        .setConnectionId(CONNECTION_ID)
        .setState(SocketDataInfo.State.CLOSE)
        .build();
    FrameInfo expectedClosingFrame = FrameInfo.newBuilder()
        .setType(Type.SOCKET_DATA)
        .setPayload(expectedClosingSdi.toByteString())
        .build();

    MockConnectionRemover connectionRemover = new MockConnectionRemover();

    InputStreamConnector inputStreamConnector = new InputStreamConnector();
View Full Code Here

    assertEquals(expectedClosingFrame, actualClosingFrame);
  }

  public void testIOExceptionInputStream() throws Exception {

    SocketDataInfo expectedClosingSdi = SocketDataInfo.newBuilder()
        .setConnectionId(CONNECTION_ID)
        .setState(SocketDataInfo.State.CLOSE)
        .build();
    FrameInfo expectedClosingFrame = FrameInfo.newBuilder()
        .setType(Type.SOCKET_DATA)
        .setPayload(expectedClosingSdi.toByteString())
        .build();

    InputStream mockIs = EasyMock.createMock(InputStream.class);
    EasyMock.expect(mockIs.read(EasyMock.isA(byte[].class)))
        .andThrow(new IOException("read error"));
View Full Code Here

   */
  @Override
  public void dispatch(final FrameInfo frameInfo) throws FramingException {
    Preconditions.checkNotNull(frameSender, "Must define frameSender before calling dispatch");
    try {
      final SocketDataInfo socketDataInfo = SocketDataInfo.parseFrom(frameInfo.getPayload());
      final long connectionId = socketDataInfo.getConnectionId();

      // Handle incoming start request.
      if (socketDataInfo.getState() == SocketDataInfo.State.START) {
        LOG.info("Starting new connection. ID " + connectionId);
        final Socket socket = socketFactory.createSocket();
        socket.connect(new InetSocketAddress(localHostAddress, localConf.getSocksServerPort()));

        final ConnectionRemover connectionRemoverCallback = new ConnectionRemover();

        // TODO(rayc) Create a pool of connectors instead of making a new instance each time.
        final InputStreamConnector inputStreamConnector =
            injector.getInstance(InputStreamConnector.class);
        inputStreamConnector.setConnectionId(connectionId);
        inputStreamConnector.setInputStream(socket.getInputStream());
        inputStreamConnector.setFrameSender(frameSender);
        inputStreamConnector.setConnectorStateCallback(connectionRemoverCallback);
        inputStreamConnector.setName("Inputconnector-" + connectionId);

        // TODO(rayc) Create a pool of connectors instead of making a new instance each time.
        final OutputStreamConnector outputStreamConnector =
            injector.getInstance(OutputStreamConnector.class);
        outputStreamConnector.setConnectionId(connectionId);
        outputStreamConnector.setOutputStream(socket.getOutputStream());
        outputStreamConnector.setConnectorStateCallback(connectionRemoverCallback);
        outputStreamConnector.setName("Outputconnector-" + connectionId);
        outputQueueMap.put(connectionId, outputStreamConnector.getQueue());

        // Start threads
        threadPoolExecutor.execute(inputStreamConnector);
        threadPoolExecutor.execute(outputStreamConnector);
        LOG.debug("active thread count = " + Thread.activeCount());
      // Deal with continuing connections or close connections.
      } else if (socketDataInfo.getState() == SocketDataInfo.State.CONTINUE ||
          socketDataInfo.getState() == SocketDataInfo.State.CLOSE) {
        if (outputQueueMap.containsKey(socketDataInfo.getConnectionId())) {
          outputQueueMap.get(connectionId).put(socketDataInfo);
        }
      // Unknown states.
      } else {
        throw new FramingException("Unknown State: " + socketDataInfo.getState() +
            " received while dispatching");
      }
    } catch (InvalidProtocolBufferException e) {
      throw new FramingException(e);
    } catch (IOException e) {
View Full Code Here

    Preconditions.checkNotNull(outputStream, "must set outputStream before calling start()");
    Preconditions.checkNotNull(connectionId, "must set connectionId before calling start()");

    try {
      while (true) {
        final SocketDataInfo socketDataInfo = queue.take();

        if (socketDataInfo.getState() == SocketDataInfo.State.CLOSE) {
          LOG.debug("Closing connection " + connectionId);
          outputStream.close();
          break;
        } else if (socketDataInfo.getState() == SocketDataInfo.State.CONTINUE) {
          LOG.debug("frame = " + socketDataInfo.toString());
          outputStream.write(socketDataInfo.getSegment().toByteArray());
        }
      }
    } catch (InterruptedException e) {
      LOG.info("Interrupted while waiting for socket data frames");
    } catch (IOException e) {
View Full Code Here

TOP

Related Classes of com.google.dataconnector.protocol.proto.SdcFrame.SocketDataInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.