Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import java.util.*;
  7.  
  8. public class Server extends JFrame
  9. {
  10. static final long serialVersionUID = 999;
  11.  
  12. final double VERSION = 0.020;
  13. int FLOOD = 3;
  14. int FLOODDELAY = 5;
  15. int MAX_CLIENTS = 10;
  16. int PORT = 8910;
  17. String MAP = "map.rpg";
  18. String TILESET = "tileset.tls";
  19.  
  20. Map map;
  21. Tileset tileset;
  22. Vector<Entity> entities = new Vector<Entity>();
  23. Vector<SpritePack> spritePacks = new Vector<SpritePack>();
  24. Vector<Sprite> sprites = new Vector<Sprite>();
  25.  
  26. Vector<Connection> connections = new Vector<Connection>();
  27. Vector<Message> inbox = new Vector<Message>();
  28. Vector<Message> outbox = new Vector<Message>();
  29. int nextID = 0;
  30.  
  31. Accounts accounts = new Accounts();
  32.  
  33. ServerSocket serverSocket = null;
  34. boolean listening = false;
  35.  
  36. OutThread outThread;
  37. ProcThread procThread;
  38.  
  39. String rootpath;
  40.  
  41. ServerFrame serverFrame = new ServerFrame(this);
  42.  
  43. public Server()
  44. {
  45. rootpath = this.getClass().getClassLoader().getSystemResource(".").getPath();
  46. rootpath = rootpath.substring(0,rootpath.indexOf("/classes"));
  47. rootpath = rootpath.replace("%20"," ");
  48.  
  49. accounts.newUser("admin","w00t!admin!",true);
  50.  
  51. readConfig();
  52.  
  53. try
  54. {
  55. loadMap(MAP);
  56. loadTileset(TILESET);
  57. //loadSprites();
  58.  
  59. startServer(PORT,MAX_CLIENTS);
  60. }
  61. catch (Exception ex)
  62. {
  63. ex.printStackTrace();
  64. this.dispose();
  65. }
  66. }
  67.  
  68. public static void main(String[] args)
  69. {
  70. new Server();
  71. }
  72.  
  73. private void readConfig()
  74. {
  75. BufferedReader in = null;
  76. String line;
  77. int temp;
  78. int spriteStart;
  79.  
  80. try
  81. {
  82. in = new BufferedReader(new FileReader(rootpath+"/config/server.cfg"));
  83.  
  84. while((line = in.readLine()) != null)
  85. {
  86. try
  87. {
  88. if(line.startsWith("max_clients"))
  89. {
  90. MAX_CLIENTS = Integer.parseInt(split(line)[1]);
  91. }
  92. else if(line.startsWith("port"))
  93. {
  94. PORT = Integer.parseInt(split(line)[1]);
  95. }
  96. else if(line.startsWith("flood_count"))
  97. {
  98. FLOOD = Integer.parseInt(split(line)[1]);
  99. }
  100. else if(line.startsWith("flood_delay"))
  101. {
  102. FLOODDELAY = Integer.parseInt(split(line)[1]);
  103. }
  104. else if(line.startsWith("map"))
  105. {
  106. MAP = line.substring(3).trim();
  107. }
  108. else if(line.startsWith("tileset"))
  109. {
  110. TILESET = line.substring(8).trim();
  111. }
  112. else if(line.startsWith("spritepack"))
  113. {
  114. temp = Integer.parseInt(split(line)[1]);
  115. spriteStart = sprites.size();
  116.  
  117. sprites.add(loadSprite(in.readLine()));
  118. sprites.add(loadSprite(in.readLine()));
  119. sprites.add(loadSprite(in.readLine()));
  120. sprites.add(loadSprite(in.readLine()));
  121.  
  122. spritePacks.add(new SpritePack(temp == 1,spriteStart,spriteStart+1,spriteStart+2,spriteStart+3));
  123. }
  124. }
  125. catch(Exception e)
  126. {
  127.  
  128. }
  129. }
  130. }
  131. catch(Exception e){}
  132. }
  133.  
  134. private void loadMap(String filename) throws Exception
  135. {
  136. FileInputStream in = new FileInputStream(rootpath+"/maps/"+filename);
  137. ObjectInputStream s = new ObjectInputStream(in);
  138. map = (Map)s.readObject();
  139. }
  140.  
  141. private void loadTileset(String filename) throws Exception
  142. {
  143. FileInputStream in = new FileInputStream(rootpath+"/tilesets/"+filename);
  144. ObjectInputStream s = new ObjectInputStream(in);
  145.  
  146. tileset = (Tileset)s.readObject();
  147. }
  148.  
  149. /*private void loadSprites(String filename) throws Exception
  150. {
  151. sprites
  152.  
  153. sprites[0] = loadSprite("char1N.spt");
  154. sprites[1] = loadSprite("char1E.spt");
  155. sprites[2] = loadSprite("char1S.spt");
  156. sprites[3] = loadSprite("char1W.spt");
  157.  
  158. sprites[4] = loadSprite("char2N.spt");
  159. sprites[5] = loadSprite("char2E.spt");
  160. sprites[6] = loadSprite("char2S.spt");
  161. sprites[7] = loadSprite("char2W.spt");
  162.  
  163. spritePacks.add(new SpritePack(true,0,1,2,3));
  164. spritePacks.add(new SpritePack(true,4,5,6,7));
  165. }*/
  166.  
  167. private Sprite loadSprite(String filename) throws Exception
  168. {
  169. FileInputStream in = new FileInputStream(rootpath+"/sprites/"+filename);
  170. ObjectInputStream s = new ObjectInputStream(in);
  171.  
  172. return (Sprite)s.readObject();
  173. }
  174.  
  175. private void startServer(int port,int maxClients)
  176. {
  177. Connection temp;
  178. Socket tempSocket;
  179. ObjectOutputStream objOut;
  180. ObjectInputStream objIn;
  181.  
  182. serverFrame.consoleMessage("Starting Server...");
  183. System.out.println("Starting Server...");
  184.  
  185. try
  186. {
  187. serverSocket = new ServerSocket(port);
  188. }
  189. catch (IOException e)
  190. {
  191. serverFrame.consoleMessage("Could not listen on port "+port);
  192. System.err.println("Could not listen on port "+port);
  193. System.exit(1);
  194. }
  195.  
  196. outThread = new OutThread(outbox);
  197. procThread = new ProcThread(inbox,outbox,connections,entities,outThread,serverFrame);
  198.  
  199. serverFrame.consoleMessage("Server Started Successfully");
  200. System.out.println("Server Started Successfully");
  201.  
  202. listening = true;
  203.  
  204. javax.swing.Timer timer = new javax.swing.Timer(30, null);
  205. timer.addActionListener(new FrameListen());
  206. timer.start();
  207.  
  208. while (listening)
  209. {
  210. try
  211. {
  212. tempSocket = serverSocket.accept();
  213.  
  214. serverFrame.consoleMessage("-----------------------");
  215. System.out.println("-----------------------");
  216. serverFrame.consoleMessage("Connection attempt from "+tempSocket.getInetAddress().toString().substring(1));
  217. System.out.println("Connection attempt from "+tempSocket.getInetAddress().toString().substring(1));
  218.  
  219. if(connections.size() < maxClients)
  220. {
  221. nextID++;
  222.  
  223. objOut = new ObjectOutputStream(tempSocket.getOutputStream());
  224. objIn = new ObjectInputStream(tempSocket.getInputStream());
  225.  
  226. temp = new Connection(tempSocket,accounts,nextID,inbox,procThread,connections,entities,objOut,objIn,serverFrame);
  227. if(authClient(tempSocket,temp,objOut,objIn))
  228. temp.start();
  229. //connections.add(temp);
  230. }
  231. else
  232. {
  233. tempSocket.close();
  234. serverFrame.consoleMessage("Server is full");
  235. System.out.println("Server is full");
  236. serverFrame.consoleMessage("-----------------------");
  237. System.out.println("-----------------------");
  238. }
  239. }
  240. catch (Exception e)
  241. {
  242. }
  243. }
  244. }
  245.  
  246. private boolean authClient(Socket socket,Connection cnxn,ObjectOutputStream objOut,ObjectInputStream objIn)
  247. {
  248. double clientVersion;
  249. Entity player;
  250.  
  251. try
  252. {
  253. clientVersion = Double.parseDouble(cnxn.get());
  254. cnxn.send(VERSION);
  255.  
  256. if(clientVersion != VERSION)
  257. {
  258. objOut.writeObject(new String("Your client is not the same version as the server. Download version "+VERSION+" from speal.gotdns.org"));
  259. serverFrame.consoleMessage("Connection attempt failed - invalid client version");
  260. System.out.println("Connection attempt failed - invalid client version");
  261. serverFrame.consoleMessage("-----------------------");
  262. System.out.println("-----------------------");
  263. return false;
  264. }
  265. else
  266. {
  267. serverFrame.consoleMessage("Sending tileset");
  268. System.out.println("Sending tileset");
  269. objOut.writeObject(tileset);
  270. serverFrame.consoleMessage("Sending sprites");
  271. System.out.println("Sending sprites");
  272. objOut.writeObject(sprites);
  273. objOut.writeObject(spritePacks);
  274. serverFrame.consoleMessage("Sending map");
  275. System.out.println("Sending map");
  276. objOut.writeObject(map);
  277.  
  278. serverFrame.consoleMessage("Generating unique client ID");
  279. System.out.println("Generating unique client ID");
  280. objOut.writeObject(new Integer(cnxn.ID));
  281.  
  282. serverFrame.consoleMessage("-----------------------");
  283. System.out.println("-----------------------");
  284. }
  285. return true;
  286. }
  287. catch(Exception e)
  288. {
  289.  
  290. }
  291. return false;
  292. }
  293.  
  294. private String[] split(String line)
  295. {
  296. String[] result = line.split("\\s");
  297. return result;
  298. }
  299.  
  300. public void kickAll(String msg)
  301. {
  302. if(msg != "")
  303. sendAll("ALR "+msg);
  304. sendAll("KCK");
  305. }
  306.  
  307. public void sendAll(String msg)
  308. {
  309. outbox.add(Message.outgoing(msg,procThread.allConnections()));
  310. if(!procThread.isAlive())
  311. {
  312. procThread = new ProcThread(procThread.inbox,procThread.outbox,procThread.connections,procThread.entities,procThread.outThread,serverFrame);
  313. procThread.start();
  314. }
  315. }
  316.  
  317. public void send(String msg,Connection dest)
  318. {
  319. outbox.add(Message.outgoing(msg,dest));
  320. if(!procThread.isAlive())
  321. {
  322. procThread = new ProcThread(procThread.inbox,procThread.outbox,procThread.connections,procThread.entities,procThread.outThread,serverFrame);
  323. procThread.start();
  324. }
  325. }
  326.  
  327. private class FrameListen implements ActionListener
  328. {
  329. public void actionPerformed(ActionEvent event)
  330. {
  331. Entity temp;
  332. for(int i=0;i<entities.size();i++)
  333. {
  334. temp = (Entity)entities.get(i);
  335. temp.update(sprites);
  336. }
  337. }
  338. }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement