Advertisement
strikero

edejerb

Jan 22nd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.49 KB | None | 0 0
  1. package algomidtermserver;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.util.LinkedList;
  8. import java.util.Random;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import javax.swing.*;
  12.  
  13. public class Server extends JFrame {
  14.  
  15. private JTextField userText;
  16. private JTextArea chatWindow;
  17. private ServerSocket server;
  18. private int clientNo = 0;
  19. int z = 0;
  20. boolean check[] = {false, false, false, false};
  21. private LinkedList<Connection> AllConnections = new LinkedList<Connection>();
  22. private int maxPlayers = 3;
  23.  
  24. //constructor
  25. public Server() {
  26. super("Server");
  27. userText = new JTextField();
  28. userText.addActionListener(
  29. new ActionListener() {
  30. @Override
  31. public void actionPerformed(ActionEvent e) {
  32.  
  33. String command = e.getActionCommand();
  34.  
  35. if (command.substring(0, 4).equals("inf-")) {
  36. int ID = Character.getNumericValue(command.charAt(4));
  37.  
  38. for (int i = 0; i < AllConnections.size(); i++) {
  39. if (ID == Integer.parseInt(AllConnections.get(i).clientID)) {
  40. AllConnections.get(i).tag = "1";
  41. break;
  42. }
  43. }
  44. }
  45. userText.setText("");
  46. }
  47. }
  48. );
  49. add(userText, BorderLayout.NORTH);
  50. chatWindow = new JTextArea();
  51. add(new JScrollPane(chatWindow));
  52. setSize(300, 1300);
  53. setVisible(true);
  54. }
  55.  
  56. public void startRunning() {
  57.  
  58. try {
  59. server = new ServerSocket(6789, 100);
  60. while (true) {
  61. showMessage("\nWaiting for a connection");
  62. Socket newConnection = server.accept();
  63. try {
  64. AllConnections.add(new Connection(newConnection));
  65. clientNo++;
  66.  
  67. if (AllConnections.size() == maxPlayers) {
  68. System.out.println("max player reached");
  69. //-----------------------------------------------------------------------------------------------
  70. sendCommandz(1);
  71.  
  72. try {
  73. Thread.sleep(10000);
  74. } catch (InterruptedException ex) {
  75. Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  76. }
  77.  
  78. System.out.println("starting to send locations");
  79. StartSendingLocations locationSender = new StartSendingLocations();
  80.  
  81. sendCommandz(2);
  82. try {
  83.  
  84. for (int i = 0; i < (60 * 5); i++) {
  85. Thread.sleep(1000);
  86. int zombieCounter = 0;
  87. for (int x = 0; x < AllConnections.size(); x++) {
  88. if (AllConnections.get(x).tag.equals("1")) {
  89.  
  90. zombieCounter++;
  91. }
  92.  
  93. }
  94. if (zombieCounter == maxPlayers) {
  95. sendCommandz(3);
  96. break;
  97. }
  98. if (i == 300 && zombieCounter != maxPlayers) {
  99. sendCommandz(4);
  100. break;
  101. }
  102. if(AllConnections.size()!= maxPlayers){
  103. closeAllConnection();
  104. break;
  105. }
  106. }
  107.  
  108. locationSender.stopSending();
  109. clientNo = 0;
  110. } catch (InterruptedException ex) {
  111. Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  112. }
  113.  
  114. }
  115.  
  116. } catch (IOException iOException) {
  117. showMessage("\n Error: A client failed to connect");
  118. }
  119. }
  120. } catch (IOException ioException) {
  121. ioException.printStackTrace();
  122. }
  123. }
  124.  
  125. private class StartSendingLocations extends Thread {
  126.  
  127. LinkedList<String[]> AllLocations = new LinkedList<String[]>();
  128.  
  129. StartSendingLocations() {
  130. start();
  131. }
  132.  
  133. boolean sendingLocation = true;
  134.  
  135. void stopSending() {
  136. sendingLocation = false;
  137. }
  138.  
  139. @Override
  140. public void run() {
  141. do {
  142. showMessage("\nCLIENT - preparing locations");
  143.  
  144. for (int i = 0; i < AllConnections.size(); i++) {
  145. if (AllConnections.get(i).location[0].isEmpty()) {
  146. continue;
  147. }
  148. AllLocations.add(new String[]{AllConnections.get(i).location[0], AllConnections.get(i).location[1],
  149. AllConnections.get(i).clientID, AllConnections.get(i).tag, AllConnections.get(i).nickName});
  150. // showMessage("\n Name: " + AllLocations.get(i)[4] + "\nlat: " + AllLocations.get(i)[0] + "\nlong: " + AllLocations.get(i)[1]);
  151. }
  152.  
  153. for (int i = 0; i < AllConnections.size(); i++) {
  154. System.out.println("Player " + AllConnections.get(i).clientID + " - " + AllConnections.get(i).tag);
  155. }
  156.  
  157. showMessage("\nCLIENT - sending location");
  158.  
  159. showMessage("\nSize - " + AllLocations.size());
  160.  
  161. for (int i = 0; i < AllConnections.size(); i++) {
  162. try {
  163. AllConnections.get(i).output.writeObject(AllLocations);
  164. AllConnections.get(i).output.flush();
  165. AllConnections.get(i).output.reset();
  166. } catch (IOException ioException) {
  167. chatWindow.append("\n something went wrong in sending location");
  168. }
  169. }
  170.  
  171. AllLocations.clear();
  172.  
  173. showMessage("\nCLIENT - location sent");
  174.  
  175. try {
  176. Thread.sleep(1000);
  177. } catch (InterruptedException ex) {
  178. Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
  179. }
  180. } while (sendingLocation);
  181. }
  182.  
  183. }
  184.  
  185. private class Connection extends Thread {
  186.  
  187. private String clientID = Integer.toString(clientNo);
  188. private ObjectOutputStream output;
  189. private ObjectInputStream input;
  190. private Socket clientSocket;
  191. private String[] location = {"", ""};
  192. private String nickName;
  193. private String tag = "0";
  194.  
  195. //Creates a thread for the client
  196. Connection(Socket clientThread) throws IOException {
  197. clientSocket = clientThread;
  198. output = new ObjectOutputStream(clientThread.getOutputStream());
  199. output.flush();
  200. input = new ObjectInputStream(clientThread.getInputStream());
  201. showMessage("\n Stream are now setup!");
  202.  
  203. start();
  204. }
  205.  
  206. //Starts conversation
  207. @Override
  208. public void run() {
  209. //Get the nickname sended by client when connection established
  210. try {
  211. nickName = (String) input.readObject();
  212. output.writeObject(new String[]{"CLIENTID", clientID});
  213. } catch (IOException ex) {
  214. ex.printStackTrace();
  215. System.out.println("Cannot read nickname");
  216. } catch (ClassNotFoundException ex) {
  217. System.out.println("Client did not send a String");
  218. }
  219.  
  220. showMessage("\nCLIENT " + clientID + " - has joined the server");
  221.  
  222. do {
  223. try {
  224. //reads this clients details from the clients device
  225. // System.out.println("waiting Data " + clientID);
  226. Object objectInput = input.readObject();
  227. //System.out.println("data Received " + clientID);
  228. if (objectInput.getClass().isArray()) {
  229. String[] arrayInput = (String[]) objectInput;
  230. if (arrayInput[0].equals("INFECT")) {
  231. System.out.println(clientID + " INFECTING SOMEONE");
  232. System.out.println(AllConnections.size());
  233. for (int i = 0; i < AllConnections.size(); i++) {
  234. System.out.println("i = " + i);
  235. if (arrayInput[1].equals(AllConnections.get(i).clientID)) {
  236. System.out.println(AllConnections.get(i).clientID + " tag changed");
  237. AllConnections.get(i).tag = "1";
  238. break;
  239. }
  240. }
  241. } else {
  242. showMessage("\nCLIENT " + clientID + " - location recieved");
  243. location = (String[]) objectInput;
  244. }
  245. }
  246. } catch (ClassNotFoundException classNotFoundException) {
  247. showMessage("\n idk what that user send!");
  248. } catch (EOFException ex) {
  249. //Client properly closed connection
  250. System.out.println("Client end the connection with .close");
  251. break;
  252. } catch (SocketException ex) {
  253. //Client prematurely closed connection
  254. System.out.println("Client closed the connection not using close");
  255. break;
  256. } catch (IOException ex) {
  257. ex.printStackTrace();
  258. }
  259. } while (true);
  260.  
  261. showMessage("\nCLIENT " + clientID + " - has left the connection");
  262. //When chat is over. close all connections.
  263. try {
  264. output.close();
  265. input.close();
  266. clientSocket.close();
  267. AllConnections.remove(this);
  268. } catch (IOException ex) {
  269. ex.printStackTrace();
  270. }
  271. }
  272. }
  273.  
  274. //updates chatWindow
  275. private void showMessage(final String text) {
  276. SwingUtilities.invokeLater(
  277. new Runnable() {
  278. public void run() {
  279. chatWindow.append(text);
  280. }
  281. }
  282. );
  283. }
  284.  
  285. //Send message to all clients
  286. private void sendMessage(int ClientID, final String text) {
  287. for (int i = 0; i < AllConnections.size(); i++) {
  288. try {
  289. AllConnections.get(i).output.writeObject("CLIENT " + ClientID + " - " + text);
  290. AllConnections.get(i).output.flush();
  291. } catch (IOException ex) {
  292. showMessage("\nError occured in sending message");
  293. }
  294. }
  295. }
  296.  
  297. private void sendCommandz(int Switch) {
  298. //commands
  299. // 1 - COUNTDOWN - starts countdown on android devices and server;
  300. // 2 - START - if android devices and server recieve this command, timer for the whole game (2 mins) will start;
  301. // 3 - END - ends the streams;
  302.  
  303. switch (Switch) {
  304. case 1:
  305.  
  306. Random randomizer = new Random();
  307. int zombie = randomizer.nextInt(maxPlayers);
  308. System.out.println("first infect: " + zombie) ;
  309. AllConnections.get(zombie).tag = "1";
  310. try {
  311. for (int i = 0; i < AllConnections.size(); i++) {
  312. AllConnections.get(i).output.writeObject("COUNTDOWN");
  313. AllConnections.get(i).output.flush();
  314. AllConnections.get(i).output.reset();
  315.  
  316. }
  317. } catch (IOException IOe) {
  318. System.out.println("xd");
  319. }
  320. break;
  321.  
  322. case 2:
  323. try {
  324. System.out.println("Start send");
  325. for (int i = 0; i < AllConnections.size(); i++) {
  326. AllConnections.get(i).output.writeObject("START");
  327. AllConnections.get(i).output.flush();
  328. AllConnections.get(i).output.reset();
  329. }
  330. } catch (IOException IOe) {
  331. System.out.println("xd");
  332. }
  333.  
  334. break;
  335.  
  336. case 3:
  337. try {
  338. System.out.println("zombies send");
  339. for (int i = 0; i < AllConnections.size(); i++) {
  340. AllConnections.get(i).output.writeObject("ZOMBIES");
  341. AllConnections.get(i).output.flush();
  342. AllConnections.get(i).output.reset();
  343. }
  344. } catch (IOException IOe) {
  345. System.out.println("xd");
  346. }
  347. break;
  348.  
  349. case 4:
  350. try {
  351. System.out.println("humans send");
  352. for (int i = 0; i < AllConnections.size(); i++) {
  353. AllConnections.get(i).output.writeObject("HUMANS");
  354. AllConnections.get(i).output.flush();
  355. AllConnections.get(i).output.reset();
  356. }
  357. } catch (IOException IOe) {
  358. System.out.println("xd");
  359. }
  360. break;
  361.  
  362. }
  363.  
  364.  
  365. }
  366. public void closeAllConnection(){
  367. for (int i = 0; i < AllConnections.size(); i++) {
  368. try {
  369. AllConnections.get(i).output.close();
  370. AllConnections.get(i).input.close();
  371. AllConnections.get(i).clientSocket.close();
  372. } catch (IOException ioException) {
  373. chatWindow.append("\n something went wrong in sending location");
  374. }
  375. }
  376.  
  377. AllConnections.clear();
  378. }
  379.  
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement