@Override
  protected SocketResult doInBackground(Boolean... params) {
    boolean picturesOnly = params[0];
    assertNotNull(picturesOnly);

    SocketWrapper socketWrapper;
    ObjectOutputStream outStream; // wrapped stream to client
    ObjectInputStream inStream; // stream from client
    try // create socket
    {
      socketWrapper = new SocketWrapper();
      outStream = socketWrapper.getOut();
      inStream = socketWrapper.getIn();
    } catch (IOException e) {
      e.printStackTrace();

      return SocketResult.FAILED_CONNECTION;
    }

    LinkedList<ChannelIdentifier> channelIdentifiers;
    try {
      Log.i(LOG_TAG, "Attempting to get channels.");
      outStream.writeObject(Commands.IOCommand.GET_CHANNEL_IDENTIFIERS);
      outStream.flush();

      Log.i(LOG_TAG, "Retrieving reply...");
      channelIdentifiers = (LinkedList<ChannelIdentifier>) inStream.readObject();

      if (channelIdentifiers == null) return SocketResult.FAILED_FORMATTING;
    } catch (IOException e) {
      e.printStackTrace();

      return SocketResult.UNKNOWN_ERROR;
    } catch (ClassNotFoundException e) {
      e.printStackTrace();

      return SocketResult.CLASS_NOT_FOUND;
    } finally {
      socketWrapper.closeAll();
    }

    ArrayList<AppChannelIdentifier> appChannelIdentifiers =
        new ArrayList<AppChannelIdentifier>(channelIdentifiers.size());
    for (int i = 0; i < channelIdentifiers.size(); i++)
      appChannelIdentifiers.add(i, new AppChannelIdentifierWithWrapper(channelIdentifiers.get(i)));

    activity.setChannelIdentifiers(appChannelIdentifiers);

    Log.d(LOG_TAG, appChannelIdentifiers.toString());
    return SocketResult.SUCCESS;
  }
示例#2
0
  private void discriminateConnection(Socket localSocket) throws IOException, ShutdownException {

    boolean closeSocket = true;

    try {
      final Connector.ConnectDetails connectDetails = Connector.read(localSocket.getInputStream());

      final SocketWrapper socketWrapper = new SocketWrapper(localSocket);
      socketWrapper.setAddress(connectDetails.getAddress());

      // Possible minor race if the socket is closed between here...
      final ResourcePool.Closeable closeable =
          getSocketSet(connectDetails.getConnectionType()).add(socketWrapper);

      // .. and the time a listener is registered. Will pick up such a zombie
      // the next time we try to use the resource.
      socketWrapper.addClosedListener(
          new SocketWrapper.ClosedListener() {
            public void socketClosed() {
              closeable.close();
            }
          });

      // We did good.
      closeSocket = false;
    } catch (CommunicationException e) {
      try {
        m_exceptionQueue.queue(e);
      } catch (ThreadSafeQueue.ShutdownException shutdownException) {
        // Can happen due to race condition with shutdown, ignore.
      }
    } finally {
      if (closeSocket) {
        try {
          localSocket.close();
        } catch (IOException ioException) {
          UncheckedInterruptedException.ioException(ioException);
          // Ignore.
        }
      }
    }
  }
 /** Cleanly shut down the <code>Receiver</code>. */
 public void shutdown() {
   // Close the socket wrapper first as that needs to use the socket.
   m_socketWrapper.close();
   super.shutdown();
 }
 private ClientReceiver(SocketWrapper socketWrapper) {
   super(socketWrapper.getInputStream());
   m_socketWrapper = socketWrapper;
 }
    public void interruptibleRun() {
      try {
        // Did we do some work on the last pass?
        boolean idle = false;

        while (true) {
          final Reservation reservation = m_sockets.reserveNext();
          boolean holdReservation = false;

          try {
            if (reservation.isSentinel()) {
              if (idle) {
                Thread.sleep(m_delay);
              }

              idle = true;
            } else {
              final SocketWrapper socketWrapper = (SocketWrapper) reservation.getResource();

              // We don't need to synchronise access to the SocketWrapper
              // stream; access is protected through the socket set and only we
              // hold the reservation.
              final InputStream inputStream = socketWrapper.getInputStream();

              if (inputStream.available() > 0) {

                idle = false;

                final ObjectInputStream objectStream = new ObjectInputStream(inputStream);

                final Message message = (Message) objectStream.readObject();

                if (message instanceof CloseCommunicationMessage) {
                  reservation.close();
                  continue;
                }

                if (message instanceof MessageRequiringResponse) {

                  final MessageRequiringResponse messageRequiringResponse =
                      (MessageRequiringResponse) message;

                  messageRequiringResponse.setResponder(
                      new SenderWithReservation(
                          new StreamSender(socketWrapper.getOutputStream()), reservation));

                  m_messageQueue.queue(message);

                  // Whatever handles the MessageExpectingResponse takes
                  // responsibility for the reservation.
                  holdReservation = true;
                } else {
                  m_messageQueue.queue(message);
                }
              }
            }
          } catch (IOException e) {
            reservation.close();
            UncheckedInterruptedException.ioException(e);
            m_messageQueue.queue(e);
          } catch (ClassNotFoundException e) {
            reservation.close();
            m_messageQueue.queue(e);
          } catch (InterruptedException e) {
            reservation.close();
            throw new UncheckedInterruptedException(e);
          } finally {
            if (!holdReservation) {
              reservation.free();
            }
          }
        }
      } catch (ThreadSafeQueue.ShutdownException e) {
        // We've been shutdown, exit this thread.
      } finally {
        // Ensure we're shutdown.
        shutdown();
      }
    }