Android Open Source - Spidermine Spider Server






From Project

Back to project page Spidermine.

License

The source code is released under:

Copyright ? 2014 PEMapModder This software is open-source and everyone is welcome to share redistributions or modifications, as long as it is clearly specified that this project's original source is ...

If you think the Android project Spidermine 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 pemapmodder.old_spidermine;
// ww w .  j  a  v  a  2s  .  c o  m
import java.io.File;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

import pemapmodder.old_spidermine.managers.ServerManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

public class SpiderServer implements Runnable {
  /////////////////////////
  //////////tools//////////
  /////////////////////////
  public Context app;
  public ConsoleChangeListener ccl;
  /////////////////////
  ///////options///////
  /////////////////////
  public final InetAddress ip;
  public final int port;
  public final String name;
  public boolean doToastWarn;
  public final File dir;
  ////////////////////////
  //////////info//////////
  ////////////////////////
  private short status=PRESTART;
  private long ticks=0;
  /////////////////////////
  //////////debug//////////
  /////////////////////////
  public Logger logger;
  protected long startTime;
  private long lastTick;
  ///////////////////////////
  //////////network//////////
  ///////////////////////////
  public DatagramSocket socket;
  public DatagramPacket packet;
  ////////////////////////////
  //////////managers//////////
  ////////////////////////////
  public ServerManager manager=new ServerManager(this);
  public SpiderServer(Bundle options, Context app, ConsoleChangeListener ccl)
      throws Throwable{
    this.app=app;
    this.ccl=ccl;
    ip=InetAddress.getByName(options.getString(ServerRunner.IP, "0.0.0.0"));
    port=options.getInt(ServerRunner.PORT, 19132);
    name=options.getString(ServerRunner.NAME, "SpiderMine Server");
    doToastWarn=options.getBoolean(ServerRunner.TOAST_WARN, true);
    dir=new File(options.getString(ServerRunner.PATH,
        Environment.getExternalStorageDirectory().getAbsolutePath()+
        "games/SpiderMine/"+name+"/"));
    if(!dir.mkdirs()){//load server
      
    }else{//create server
      
    }
    logger=Logger.getLogger("SpiderServer");
  }
  public void run() {
    status=RUNTIME;
    startTime=System.currentTimeMillis();
    lastTick=startTime;
    try {
      initNetworking();
    } catch (Throwable e) {
      err(e);
    }
    while(status==RUNTIME){
      try {
        tick();
        this.ticks=getTicks() + 1;
        long time=System.currentTimeMillis()-lastTick;
        if(time>100)
          onOverloaded(time);
        lastTick=System.currentTimeMillis();
        Thread.sleep(50);
      } catch (Throwable e) {
        err(e);
      }
    }
    status=PAUSED;
    status=STOPPED;
  }
  protected void initNetworking()throws Throwable{
    socket=new DatagramSocket(port, ip);
    int length=1536;
    byte[] buffer=new byte[length];
    packet=new DatagramPacket(buffer, length);
    socket.setSoTimeout(5000);
    socket.receive(packet);
    socket.setSoTimeout(0);
    ByteBuffer b=ByteBuffer.wrap(packet.getData());
    byte[] data=new byte[packet.getLength()];
    b.get(data);
    DatagramPacket o=new DatagramPacket(data, packet.getPort(), packet.getAddress(), packet.getLength());
    handlePacket(o);
  }
  protected synchronized void handlePacket(DatagramPacket o)throws Throwable{
    if(o!=null){
      switch(o.getData()[0]&0xFF){
      
      }
    }
  }
  protected void onOverloaded(long ti) {
    logger.log(Level.WARNING, "Server overloaded. Tick interval: "+Long.toString(ti)+" ms.");
    if(doToastWarn){
      Toast t=Toast.makeText(app, "Server overloaded! TPS approx. "+Double.toString((Math.pow(ti, -1))), Toast.LENGTH_SHORT);
      t.show();
    }
    ccl.warning("Server overloaded!!!");
  }
  public short getStatus(){
    return status;
  }
  protected void tick()throws Throwable{
    
  }
  public void err(Throwable e){
    if(e instanceof InterruptedException)
      logger.log(Level.SEVERE, "InterruptedException", e);
    ccl.err("An error occurred: "+e.toString());
  }
  public final void stop(){
    status=STOPPED;
  }
  public long getTicks() {
    return ticks;
  }
  public final static short PRESTART=0,
      RUNTIME=1,
      PAUSED=2,
      STOPPED=2;
}




Java Source Code List

pemapmodder.StdObj.java
pemapmodder.easymod.EasyMod.java
pemapmodder.easymod.xml.Attribute.java
pemapmodder.easymod.xml.Element.java
pemapmodder.easymod.xml.XmlLangException.java
pemapmodder.easymod.xml.Xml.java
pemapmodder.old_spidermine.ConsoleChangeListener.java
pemapmodder.old_spidermine.ConsoleInputListener.java
pemapmodder.old_spidermine.ServerRunner.java
pemapmodder.old_spidermine.SpiderServer.java
pemapmodder.old_spidermine.managers.CmdManager.java
pemapmodder.old_spidermine.managers.EntityManager.java
pemapmodder.old_spidermine.managers.Manager.java
pemapmodder.old_spidermine.managers.ServerManager.java
pemapmodder.old_spidermine.managers.TileManager.java
pemapmodder.old_spidermine.managers.UserManager.java
pemapmodder.old_spidermine.managers.WorldManager.java
pemapmodder.old_spidermine.network.Networker.java
pemapmodder.old_spidermine.objects.ChestTile.java
pemapmodder.old_spidermine.objects.Command.java
pemapmodder.old_spidermine.objects.Entity.java
pemapmodder.old_spidermine.objects.SignTile.java
pemapmodder.old_spidermine.objects.Tile.java
pemapmodder.old_spidermine.objects.User.java
pemapmodder.old_spidermine.utils.io.IOUtils.java
pemapmodder.old_spidermine.utils.io.MyReader.java
pemapmodder.old_spidermine.utils.math.Location.java
pemapmodder.old_spidermine.utils.math.MathUtils.java
pemapmodder.old_spidermine.utils.math.PCoord.java
pemapmodder.old_spidermine.utils.math.Position.java
pemapmodder.old_spidermine.world.Block.java
pemapmodder.old_spidermine.world.Chunk.java
pemapmodder.old_spidermine.world.World.java
pemapmodder.old_spidermine.world.populator.WorldPopulator.java
pemapmodder.spidermine.Console.java
pemapmodder.spidermine.SpiderServer.java
pemapmodder.spidermine.Utils.java
pemapmodder.spidermine.android.ConsolePanel.java
pemapmodder.spidermine.android.CreateNewServerActivity.java
pemapmodder.spidermine.android.LauncherActivity.java
pemapmodder.spidermine.android.ServerMainControlPanel.java
pemapmodder.spidermine.client.Client.java
pemapmodder.spidermine.events.EventHandler.java
pemapmodder.spidermine.events.EventResult.java
pemapmodder.spidermine.events.Event.java
pemapmodder.spidermine.events.server.ServerStopEvent.java
pemapmodder.spidermine.exceptions.SocketAddressUsedException.java
pemapmodder.spidermine.managers.ClientManager.java
pemapmodder.spidermine.managers.CommandHandler.java
pemapmodder.spidermine.managers.EventManager.java
pemapmodder.spidermine.managers.Manager.java
pemapmodder.spidermine.managers.ServerManager.java
pemapmodder.spidermine.network.NetworkHandler.java
pemapmodder.spidermine.network.packet.PacketParser.java
pemapmodder.spidermine.network.packet.Packet.java
pemapmodder.spidermine.network.packet.ProtocolInfo.java
pemapmodder.spidermine.objects.CommandIssuer.java
pemapmodder.spidermine.objects.Command.java
pemapmodder.spidermine.objects.Interfaces.java
pemapmodder.spidermine.world.World.java
pemapmodder.utils.io.Saveable.java
pemapmodder.utils.php.functions.P.java