Android Open Source - Gents Server Manager






From Project

Back to project page Gents.

License

The source code is released under:

MIT License

If you think the Android project Gents 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.gsfraley.gents.service;
/*w  w w. jav  a 2s .c om*/
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;

public class ServerManager implements Iterable<ServerGhost> {
  int sidGenerator;
  Context mContext;
  
  private ArrayList<ServerGhost> serverList;
  private ArrayList<ManagerListener> serverListeners;
  
  public ServerManager(Context context) {
    mContext = context;
    
    serverList = new ArrayList<ServerGhost>();
    serverListeners = new ArrayList<ManagerListener>();
  }

  public void addListener(ManagerListener listener) {
    serverListeners.add(listener);
  }
  
  public void removeListener(ManagerListener listener) {
    serverListeners.remove(listener);
  }
  
  public void addServer(ServerGhost server) {
    for(ManagerListener l : serverListeners)
      l.serverAdded(server);
    serverList.add(server);
  }
  
  public void removeServer(ServerGhost server) {
    for(ManagerListener l : serverListeners)
      l.serverRemoved(server);
    serverList.remove(server);
  }
  
  public void startServer(ServerGhost server) {
    server.start();
    for(ManagerListener l : serverListeners)
      l.serverStarted(server);
  }
  
  public void stopServer(ServerGhost server) {
    server.stop();
    for(ManagerListener l : serverListeners)
      l.serverStopped(server);
  }
  
  public ServerGhost getServer(int position) {
    return serverList.get(position);
  }

  @SuppressWarnings("unchecked")
  public ServerGhost createServer(String classString, int serverId) {
    Class<ServerGhost> serverClass = null;
    try {
      serverClass = (Class<ServerGhost>) ServerManager.class.getClassLoader().loadClass(classString);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (ClassCastException e) {
      throw new ClassCastException(classString + " must be a subclass of ServerGhost");
    }
    
    ServerGhost loadedServer = null;
    try {
      loadedServer = (ServerGhost) serverClass.getConstructor(new Class<?>[]{Context.class, int.class}).newInstance(mContext, serverId);
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (ClassCastException e) {
      throw new ClassCastException("Could not cast " + serverClass.toString() + " to a ServerGhost");
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }
    
    addServer(loadedServer);
    return loadedServer;
  }
  
  public ServerGhost createServer(String classString) {
    ServerGhost newServer = createServer(classString, sidGenerator);
    sidGenerator++;
    return newServer;
  }
  
  @Override
  public Iterator<ServerGhost> iterator() {
    return serverList.iterator();
  }
}




Java Source Code List

fi.iki.elonen.NanoHTTPD.java
org.gsfraley.gents.app.adapters.RunningServerAdapter.java
org.gsfraley.gents.app.adapters.ServerTypeAdapter.java
org.gsfraley.gents.app.fragments.RunningServerFragment.java
org.gsfraley.gents.app.fragments.ServerTypeFragment.java
org.gsfraley.gents.app.main.MainActivityListener.java
org.gsfraley.gents.app.main.MainActivity.java
org.gsfraley.gents.service.ManagerListener.java
org.gsfraley.gents.service.ServerBoss.java
org.gsfraley.gents.service.ServerGhost.java
org.gsfraley.gents.service.ServerManager.java
org.gsfraley.gents.service.servers.echoserver.EchoServerPreferenceActivity.java
org.gsfraley.gents.service.servers.echoserver.EchoServer.java
org.gsfraley.gents.service.servers.helloserver.HelloServerPreferenceActivity.java
org.gsfraley.gents.service.servers.helloserver.HelloServer.java
org.gsfraley.gents.service.servers.nullserver.NullServer.java
org.gsfraley.gents.service.servers.yesserver.YesServerPreferenceActivity.java
org.gsfraley.gents.service.servers.yesserver.YesServer.java