Android Open Source - DistributedMemory Host Socket Handler






From Project

Back to project page DistributedMemory.

License

The source code is released under:

Apache License

If you think the Android project DistributedMemory listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.faudroids.distributedmemory.network;
/* w ww. j  av a 2s  .  c  o  m*/

import org.faudroids.distributedmemory.utils.Assert;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.inject.Inject;
import javax.inject.Singleton;

import timber.log.Timber;

@Singleton
final class HostSocketHandler {

  private ServerRunnable serverRunnable;


  @Inject
  public HostSocketHandler() { }


  public int start(ClientConnectionListener clientConnectionListener) throws IOException {
    Assert.assertFalse(isRunning(), "server already running");

    ServerSocket serverSocket = new ServerSocket(0);
    this.serverRunnable = new ServerRunnable(
        serverSocket,
        clientConnectionListener);
    new Thread(serverRunnable).start();
    Timber.d("Hosting on port " + serverSocket.getLocalPort());
    return serverSocket.getLocalPort();
  }


  public boolean isRunning() {
    return serverRunnable != null;
  }


  public void shutdown() {
    Assert.assertTrue(isRunning(), "server not running");

    serverRunnable.shutdown();
    serverRunnable = null;
  }


  private static final class ServerRunnable implements Runnable {

    private final ServerSocket serverSocket;
    private final ClientConnectionListener clientConnectionListener;

    private boolean alive = true;

    public ServerRunnable(
        ServerSocket serverSocket,
        ClientConnectionListener clientConnectionListener) {

      this.serverSocket = serverSocket;
      this.clientConnectionListener = clientConnectionListener;
    }


    public void shutdown() {
      alive = false;
      try {
        serverSocket.close();
      } catch (IOException ioe) {
        Timber.w("failed to close server socket");
      }
    }


    @Override
    public void run() {
      while (alive) {
        try {
          Socket socket = serverSocket.accept();
          clientConnectionListener.onClientConnected(socket);
        } catch(IOException ioe) {
          if (alive) Timber.e(ioe, "failed to accept client");
        }
      }
      try {
        serverSocket.close();
      } catch (IOException ioe) {
        Timber.w(ioe, "failed to close server socket");
      }
    }

  }


  static interface ClientConnectionListener {

    public void onClientConnected(Socket hostSocket);

  }
}




Java Source Code List

org.faudroids.distributedmemory.ApplicationTest.java
org.faudroids.distributedmemory.app.AppModule.java
org.faudroids.distributedmemory.app.DistributedMemoryApplication.java
org.faudroids.distributedmemory.common.BaseActivity.java
org.faudroids.distributedmemory.common.BaseApplication.java
org.faudroids.distributedmemory.common.BaseListActivity.java
org.faudroids.distributedmemory.common.BaseService.java
org.faudroids.distributedmemory.common.CommonModule.java
org.faudroids.distributedmemory.common.Injector.java
org.faudroids.distributedmemory.core.Card.java
org.faudroids.distributedmemory.core.ClientGameListener.java
org.faudroids.distributedmemory.core.ClientGameManager.java
org.faudroids.distributedmemory.core.CoreModule.java
org.faudroids.distributedmemory.core.Device.java
org.faudroids.distributedmemory.core.Evaluation.java
org.faudroids.distributedmemory.core.GameSetupInfo.java
org.faudroids.distributedmemory.core.GameStateManager.java
org.faudroids.distributedmemory.core.GameState.java
org.faudroids.distributedmemory.core.HostGameListener.java
org.faudroids.distributedmemory.core.HostGameManager.java
org.faudroids.distributedmemory.core.HostGameStateManager.java
org.faudroids.distributedmemory.core.HostStateTransitionListener.java
org.faudroids.distributedmemory.core.HostStateTransition.java
org.faudroids.distributedmemory.core.MessageConstants.java
org.faudroids.distributedmemory.core.MessageReader.java
org.faudroids.distributedmemory.core.MessageWriter.java
org.faudroids.distributedmemory.core.Player.java
org.faudroids.distributedmemory.network.BroadcastMessage.java
org.faudroids.distributedmemory.network.ClientNetworkListener.java
org.faudroids.distributedmemory.network.ConnectionHandlerFactory.java
org.faudroids.distributedmemory.network.ConnectionHandler.java
org.faudroids.distributedmemory.network.HostInfo.java
org.faudroids.distributedmemory.network.HostNetworkListener.java
org.faudroids.distributedmemory.network.HostSocketHandler.java
org.faudroids.distributedmemory.network.JsonConnectionHandler.java
org.faudroids.distributedmemory.network.NetworkManager.java
org.faudroids.distributedmemory.network.NetworkModule.java
org.faudroids.distributedmemory.network.StringConnectionHandler.java
org.faudroids.distributedmemory.ui.AboutActivity.java
org.faudroids.distributedmemory.ui.BitmapCache.java
org.faudroids.distributedmemory.ui.ClientUtils.java
org.faudroids.distributedmemory.ui.GameActivity.java
org.faudroids.distributedmemory.ui.HelpActivity.java
org.faudroids.distributedmemory.ui.HelpDialogActivity.java
org.faudroids.distributedmemory.ui.HostGameActivity.java
org.faudroids.distributedmemory.ui.HostService.java
org.faudroids.distributedmemory.ui.JoinGameActivity.java
org.faudroids.distributedmemory.ui.LobbyActivity.java
org.faudroids.distributedmemory.ui.MainActivity.java
org.faudroids.distributedmemory.ui.QRCodeUtils.java
org.faudroids.distributedmemory.ui.StopServerBroadcastReceiver.java
org.faudroids.distributedmemory.ui.UiModule.java
org.faudroids.distributedmemory.utils.Assert.java
org.faudroids.distributedmemory.utils.NotificationUtils.java
org.faudroids.distributedmemory.utils.QRCodeEncoder.java
org.faudroids.distributedmemory.utils.ServiceUtils.java
org.faudroids.distributedmemory.utils.UtilsModule.java