Advertisement
JavNac

Server

Apr 26th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package com.ajnin.game;
  2.  
  3. import com.ajnin.game.serverobjects.TypeMessage;
  4. import com.cubes.BlockTerrainControl;
  5. import com.cubes.Vector3Int;
  6. import com.cubes.network.CubesSerializer;
  7. import com.cubes.test.CubesTestAssets;
  8. import com.cubes.test.blocks.Block_Grass;
  9. import com.jme3.app.SimpleApplication;
  10. import com.jme3.network.ConnectionListener;
  11. import com.jme3.network.HostedConnection;
  12. import com.jme3.network.Message;
  13. import com.jme3.network.MessageListener;
  14. import com.jme3.network.Network;
  15. import com.jme3.network.Server;
  16. import com.jme3.network.serializing.Serializer;
  17. import com.jme3.system.JmeContext;
  18. import java.io.IOException;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21.  
  22. /**
  23.  *
  24.  * @author Ajnin
  25.  */
  26. public class MainServer extends SimpleApplication implements MessageListener<HostedConnection>, ConnectionListener {
  27.  
  28.     //private BlockTerrainControl map;
  29.     //byte[] chunk;
  30.     Server myServer;
  31.  
  32.     public static void main(String[] args) throws IOException {
  33.         MainServer app = new MainServer();
  34.         app.start(JmeContext.Type.Headless);
  35.     }
  36.  
  37.     @Override
  38.     public void simpleInitApp() {
  39.         Serializer.registerClass(TypeMessage.class);
  40.  
  41.         try {
  42.             System.out.println("Starting Server...");
  43.             myServer = Network.createServer(7777);
  44.             myServer.addConnectionListener(this);
  45.             myServer.start();
  46.             System.out.println("Server Started!");
  47.         } catch (IOException ex) {
  48.             Logger.getLogger(MainServer.class.getName()).log(Level.SEVERE, null, ex);
  49.         }
  50.  
  51.         //Ignore For Now
  52.         //map = new BlockTerrainControl(CubesTestAssets.getSettings(this), new Vector3Int(64, 1, 64));
  53.         //map.setBlocksFromNoise(new Vector3Int(0, 0, 0), new Vector3Int(64, 16, 64), 0.5f, Block_Grass.class);
  54.         //chunk = CubesSerializer.writeToBytes(map.getChunk(new Vector3Int(0, 0, 0)));
  55.     }
  56.  
  57.     @Override
  58.     public void messageReceived(HostedConnection source, Message message) {
  59.         if (message instanceof TypeMessage) {
  60.             TypeMessage information = (TypeMessage) message;
  61.             System.out.println("Message: " + information.getMessage());
  62.  
  63.             if (source.getAttribute("Verified") == "True") {
  64.                 if (information.getType().equals("Message")) {
  65.                     TypeMessage text = (TypeMessage) message;
  66.                     System.out.println("Server Recieved '" + text.getMessage() + "' from client #" + source.getId());
  67.                 }
  68.             } else {
  69.                 if (information.getType().equals("Game Identity")) {
  70.                     String name = information.getMessage().split("|")[0];
  71.                     Integer version = Integer.valueOf(information.getMessage().split(",")[1]);
  72.                     if (name.equals(myServer.getGameName()) && version.equals(myServer.getVersion())) {
  73.                         source.setAttribute("Verified", "True");
  74.                         System.out.println("Client # " + source.getId() + " is now verified!");
  75.                     } else {
  76.                         System.out.println("Vertification Failed");
  77.                         source.close("Verification Failed. Game Name or Version does not match.");
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.     @Override
  85.     public void simpleUpdate(float tpf) {
  86.     }
  87.  
  88.     @Override
  89.     public void destroy() {
  90.         myServer.close();
  91.         super.destroy();
  92.     }
  93.  
  94.     @Override
  95.     public void connectionAdded(Server server, HostedConnection conn) {
  96.         System.out.println("Client Connected # " + conn);
  97.     }
  98.  
  99.     @Override
  100.     public void connectionRemoved(Server server, HostedConnection conn) {
  101.         System.out.println("Removed Client: " + conn);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement