Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Server extends JFrame
- {
- static final long serialVersionUID = 999;
- final double VERSION = 0.020;
- int FLOOD = 3;
- int FLOODDELAY = 5;
- int MAX_CLIENTS = 10;
- int PORT = 8910;
- String MAP = "map.rpg";
- String TILESET = "tileset.tls";
- Map map;
- Tileset tileset;
- Vector<Entity> entities = new Vector<Entity>();
- Vector<SpritePack> spritePacks = new Vector<SpritePack>();
- Vector<Sprite> sprites = new Vector<Sprite>();
- Vector<Connection> connections = new Vector<Connection>();
- Vector<Message> inbox = new Vector<Message>();
- Vector<Message> outbox = new Vector<Message>();
- int nextID = 0;
- Accounts accounts = new Accounts();
- ServerSocket serverSocket = null;
- boolean listening = false;
- OutThread outThread;
- ProcThread procThread;
- String rootpath;
- ServerFrame serverFrame = new ServerFrame(this);
- public Server()
- {
- rootpath = this.getClass().getClassLoader().getSystemResource(".").getPath();
- rootpath = rootpath.substring(0,rootpath.indexOf("/classes"));
- rootpath = rootpath.replace("%20"," ");
- accounts.newUser("admin","w00t!admin!",true);
- readConfig();
- try
- {
- loadMap(MAP);
- loadTileset(TILESET);
- //loadSprites();
- startServer(PORT,MAX_CLIENTS);
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- this.dispose();
- }
- }
- public static void main(String[] args)
- {
- new Server();
- }
- private void readConfig()
- {
- BufferedReader in = null;
- String line;
- int temp;
- int spriteStart;
- try
- {
- in = new BufferedReader(new FileReader(rootpath+"/config/server.cfg"));
- while((line = in.readLine()) != null)
- {
- try
- {
- if(line.startsWith("max_clients"))
- {
- MAX_CLIENTS = Integer.parseInt(split(line)[1]);
- }
- else if(line.startsWith("port"))
- {
- PORT = Integer.parseInt(split(line)[1]);
- }
- else if(line.startsWith("flood_count"))
- {
- FLOOD = Integer.parseInt(split(line)[1]);
- }
- else if(line.startsWith("flood_delay"))
- {
- FLOODDELAY = Integer.parseInt(split(line)[1]);
- }
- else if(line.startsWith("map"))
- {
- MAP = line.substring(3).trim();
- }
- else if(line.startsWith("tileset"))
- {
- TILESET = line.substring(8).trim();
- }
- else if(line.startsWith("spritepack"))
- {
- temp = Integer.parseInt(split(line)[1]);
- spriteStart = sprites.size();
- sprites.add(loadSprite(in.readLine()));
- sprites.add(loadSprite(in.readLine()));
- sprites.add(loadSprite(in.readLine()));
- sprites.add(loadSprite(in.readLine()));
- spritePacks.add(new SpritePack(temp == 1,spriteStart,spriteStart+1,spriteStart+2,spriteStart+3));
- }
- }
- catch(Exception e)
- {
- }
- }
- }
- catch(Exception e){}
- }
- private void loadMap(String filename) throws Exception
- {
- FileInputStream in = new FileInputStream(rootpath+"/maps/"+filename);
- ObjectInputStream s = new ObjectInputStream(in);
- map = (Map)s.readObject();
- }
- private void loadTileset(String filename) throws Exception
- {
- FileInputStream in = new FileInputStream(rootpath+"/tilesets/"+filename);
- ObjectInputStream s = new ObjectInputStream(in);
- tileset = (Tileset)s.readObject();
- }
- /*private void loadSprites(String filename) throws Exception
- {
- sprites
- sprites[0] = loadSprite("char1N.spt");
- sprites[1] = loadSprite("char1E.spt");
- sprites[2] = loadSprite("char1S.spt");
- sprites[3] = loadSprite("char1W.spt");
- sprites[4] = loadSprite("char2N.spt");
- sprites[5] = loadSprite("char2E.spt");
- sprites[6] = loadSprite("char2S.spt");
- sprites[7] = loadSprite("char2W.spt");
- spritePacks.add(new SpritePack(true,0,1,2,3));
- spritePacks.add(new SpritePack(true,4,5,6,7));
- }*/
- private Sprite loadSprite(String filename) throws Exception
- {
- FileInputStream in = new FileInputStream(rootpath+"/sprites/"+filename);
- ObjectInputStream s = new ObjectInputStream(in);
- return (Sprite)s.readObject();
- }
- private void startServer(int port,int maxClients)
- {
- Connection temp;
- Socket tempSocket;
- ObjectOutputStream objOut;
- ObjectInputStream objIn;
- serverFrame.consoleMessage("Starting Server...");
- System.out.println("Starting Server...");
- try
- {
- serverSocket = new ServerSocket(port);
- }
- catch (IOException e)
- {
- serverFrame.consoleMessage("Could not listen on port "+port);
- System.err.println("Could not listen on port "+port);
- System.exit(1);
- }
- outThread = new OutThread(outbox);
- procThread = new ProcThread(inbox,outbox,connections,entities,outThread,serverFrame);
- serverFrame.consoleMessage("Server Started Successfully");
- System.out.println("Server Started Successfully");
- listening = true;
- javax.swing.Timer timer = new javax.swing.Timer(30, null);
- timer.addActionListener(new FrameListen());
- timer.start();
- while (listening)
- {
- try
- {
- tempSocket = serverSocket.accept();
- serverFrame.consoleMessage("-----------------------");
- System.out.println("-----------------------");
- serverFrame.consoleMessage("Connection attempt from "+tempSocket.getInetAddress().toString().substring(1));
- System.out.println("Connection attempt from "+tempSocket.getInetAddress().toString().substring(1));
- if(connections.size() < maxClients)
- {
- nextID++;
- objOut = new ObjectOutputStream(tempSocket.getOutputStream());
- objIn = new ObjectInputStream(tempSocket.getInputStream());
- temp = new Connection(tempSocket,accounts,nextID,inbox,procThread,connections,entities,objOut,objIn,serverFrame);
- if(authClient(tempSocket,temp,objOut,objIn))
- temp.start();
- //connections.add(temp);
- }
- else
- {
- tempSocket.close();
- serverFrame.consoleMessage("Server is full");
- System.out.println("Server is full");
- serverFrame.consoleMessage("-----------------------");
- System.out.println("-----------------------");
- }
- }
- catch (Exception e)
- {
- }
- }
- }
- private boolean authClient(Socket socket,Connection cnxn,ObjectOutputStream objOut,ObjectInputStream objIn)
- {
- double clientVersion;
- Entity player;
- try
- {
- clientVersion = Double.parseDouble(cnxn.get());
- cnxn.send(VERSION);
- if(clientVersion != VERSION)
- {
- objOut.writeObject(new String("Your client is not the same version as the server. Download version "+VERSION+" from speal.gotdns.org"));
- serverFrame.consoleMessage("Connection attempt failed - invalid client version");
- System.out.println("Connection attempt failed - invalid client version");
- serverFrame.consoleMessage("-----------------------");
- System.out.println("-----------------------");
- return false;
- }
- else
- {
- serverFrame.consoleMessage("Sending tileset");
- System.out.println("Sending tileset");
- objOut.writeObject(tileset);
- serverFrame.consoleMessage("Sending sprites");
- System.out.println("Sending sprites");
- objOut.writeObject(sprites);
- objOut.writeObject(spritePacks);
- serverFrame.consoleMessage("Sending map");
- System.out.println("Sending map");
- objOut.writeObject(map);
- serverFrame.consoleMessage("Generating unique client ID");
- System.out.println("Generating unique client ID");
- objOut.writeObject(new Integer(cnxn.ID));
- serverFrame.consoleMessage("-----------------------");
- System.out.println("-----------------------");
- }
- return true;
- }
- catch(Exception e)
- {
- }
- return false;
- }
- private String[] split(String line)
- {
- String[] result = line.split("\\s");
- return result;
- }
- public void kickAll(String msg)
- {
- if(msg != "")
- sendAll("ALR "+msg);
- sendAll("KCK");
- }
- public void sendAll(String msg)
- {
- outbox.add(Message.outgoing(msg,procThread.allConnections()));
- if(!procThread.isAlive())
- {
- procThread = new ProcThread(procThread.inbox,procThread.outbox,procThread.connections,procThread.entities,procThread.outThread,serverFrame);
- procThread.start();
- }
- }
- public void send(String msg,Connection dest)
- {
- outbox.add(Message.outgoing(msg,dest));
- if(!procThread.isAlive())
- {
- procThread = new ProcThread(procThread.inbox,procThread.outbox,procThread.connections,procThread.entities,procThread.outThread,serverFrame);
- procThread.start();
- }
- }
- private class FrameListen implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- Entity temp;
- for(int i=0;i<entities.size();i++)
- {
- temp = (Entity)entities.get(i);
- temp.update(sprites);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement