Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. package SKJ;
  2.  
  3. import java.io.*;
  4. import java.net.InetAddress;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Scanner;
  11.  
  12. public class Agent2 implements Externalizable{
  13. private static boolean first=true;
  14. private String name;
  15. private int port;
  16. private InetAddress iip;
  17. private int iport;
  18. private ServerSocket serverSocket;
  19. private Socket clientSocket;
  20. private Socket outSocket;
  21. private HashMap<Agent2,ArrayList<String>> scores = new HashMap<>();
  22. private ArrayList<Agent2> agents = new ArrayList<>();
  23. private int serverPort;
  24. private InetAddress serverIP;
  25.  
  26. public Agent2(){}
  27. public Agent2(String name, int port, InetAddress iip, int iport) {
  28. this.name = name;
  29. this.port = port;
  30. this.iip = iip;
  31. this.iport = iport;/*
  32. try {
  33. serverSocket = new ServerSocket(port, 0, InetAddress.getByName("localhost"));
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. */
  38. }
  39.  
  40. public static void main(String[] args) {
  41. try {
  42. Agent2 a = new Agent2("Agent2", 7000, InetAddress.getByName("localhost"), 6000);
  43.  
  44. int decision=-1;
  45. Scanner sc = new Scanner(System.in);
  46. while (decision!=0) {
  47. System.out.println("What do you want to do?\nPress 0 to quit\nPress 1 to join lobby\nIf you are already in a lobby:\nPress 2 to play with someone ");
  48. if(sc.hasNextInt()){
  49. decision=sc.nextInt();
  50. switch(decision){
  51. case 0:
  52. //if który sprawdza czy rozegrał z każdym
  53. for (Agent2 ag : a.agents){
  54. if(!a.scores.containsKey(ag)) {
  55. decision = -2;
  56. System.err.println("You can't leave, u didnt play a game with " + ag.toString());
  57. }
  58. }
  59. break;
  60. case 1:
  61. //pobranie listy graczy
  62. //sprawdzenie czy na starej liście jest jakiś gracz, którego nie ma na nowej.
  63. // Jeśli tak to usuwa obiekt tego gracza z wyników
  64. //sprawdzenie czy już nie jest połączony
  65. if(!a.agents.contains(a)){
  66. if(!first)
  67. a.join();
  68. a.joinLobby();
  69. first=false;
  70. }
  71. break;
  72. case 2:
  73. //zaczęcie rozgrywki
  74. System.out.println("Write name of player with who you want to play with?");
  75. System.out.println(a.agents);
  76. String name=sc.nextLine();
  77. if(a.agents.toString().contains(name)) {
  78. System.out.println("And now write his port ");
  79. String port=sc.nextLine();
  80. if(a.agents.toString().contains(port)){}
  81. /*new AgentGameThread(new Socket(name, port));
  82. zrobić z String InetAdress, pytanie jak?
  83. */
  84. else System.out.println("There is no player with such port");
  85. }else System.out.println("There is no player with such name");
  86. break;
  87. case 3:
  88. System.out.println(a.agents);
  89. default:
  90. System.out.println("No such command, try again!");
  91. }
  92. }else {
  93. System.out.println("Wrong number");
  94. break;
  95. }
  96. }
  97. /*while(socket.isOpen()){
  98. switch(command):
  99. case getAgents itp.
  100. */
  101. } catch (UnknownHostException e) {
  102. e.printStackTrace();
  103. }
  104.  
  105. }
  106.  
  107. // połączenie się z innym graczem w celu pobrania ip servera oraz portu
  108. public void join() {
  109. try {
  110. clientSocket = new Socket(iip, iport);
  111. PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  112. BufferedReader in = new BufferedReader(
  113. new InputStreamReader(clientSocket.getInputStream()));
  114. out.print("Send iip and port of server");
  115. //serverIP= jak pobrać InetAdress sprawdzic czy dziala, rowniez w AgentSendingThread
  116. ObjectInputStream objectInput = new ObjectInputStream(clientSocket.getInputStream());
  117. serverIP = (InetAddress) objectInput.readObject();
  118. serverPort=Integer.parseInt(in.readLine());
  119. } catch (UnknownHostException e) {
  120. System.err.println("Don't know about player " + iip);
  121. System.exit(1);
  122. } catch (IOException e) {
  123. System.err.println("Couldn't get I/O for the connection to " +
  124. iip);
  125. e.printStackTrace();
  126. System.exit(1);
  127. } catch (ClassNotFoundException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. public void joinLobby(){
  132. try {
  133. if (first)
  134. outSocket = new Socket(iip, iport);
  135. else
  136. outSocket = new Socket(serverIP, serverPort);
  137. this.sendAgent();
  138. //this.setAgents();
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. }
  142. }
  143.  
  144. //czekanie na połączenie, jeśli nadejdzie to obsługa komend (albo wysłanie InetAdress oraz portu servera albo rozpoczęcie gry) w nowym wątku
  145. public void listen() {
  146. //bez sensu bo nie wyjdzie z tęj pętli i zablokuje agenta
  147. // zrobić tak, że a outSocket = serverSocket.accept(); jest w innym wątku (on sam uruchamia nowe wątki)
  148. //później jeśli inGame zmieni się na false to wykombinować coś z interruptem threada który nasłuchuje
  149. try {
  150. serverSocket=new ServerSocket(port);
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. new AgentListenThread(serverSocket,serverIP,serverPort);
  155.  
  156. }
  157.  
  158. //pobranie listy Agentów z LobbyServer oraz sprawdzenie czy na starej liście jest jakiś gracz, którego nie ma na nowej.
  159. //Jeśli tak to usuwa obiekt tego gracza z wyników
  160. public void setAgents() {
  161. try {
  162. PrintWriter out = new PrintWriter(outSocket.getOutputStream(), true);
  163. BufferedReader in = new BufferedReader(
  164. new InputStreamReader(outSocket.getInputStream()));
  165. System.out.println(in.readLine());
  166. out.print("Get players");
  167. System.out.println("flag2");
  168. ObjectInputStream objectInput = new ObjectInputStream(outSocket.getInputStream());
  169. ArrayList<Agent2> agents2= (ArrayList<Agent2>) objectInput.readObject();
  170. System.out.println("flag3");
  171. agents2.forEach(obj -> {
  172. if(!agents.contains(obj))
  173. scores.remove(obj);
  174. });
  175. } catch (IOException | ClassNotFoundException e) {
  176. e.printStackTrace();
  177. }
  178. }
  179. // wysłanie obiektu tego Agenta do LobbyServer
  180. public void sendAgent() {
  181. try {
  182. PrintWriter out = new PrintWriter(outSocket.getOutputStream(), true);
  183. BufferedReader in = new BufferedReader(
  184. new InputStreamReader(outSocket.getInputStream()));
  185. System.out.println(in.readLine());
  186. out.print("Set player");
  187. out.print("Set player");
  188. out.print("Set player");
  189. ObjectOutputStream objectOutput = new ObjectOutputStream(outSocket.getOutputStream());
  190. objectOutput.writeObject(this);
  191. } catch (IOException e) {
  192. e.printStackTrace();
  193. }
  194. }
  195. public void quit(){
  196. //if(scores containsAll Agents)
  197. try {
  198. PrintWriter out = new PrintWriter(outSocket.getOutputStream(), true);
  199. out.print("Quit");
  200. } catch (IOException e) {
  201. e.printStackTrace();
  202. }
  203.  
  204. }
  205.  
  206. public String getName() {
  207. return name;
  208. }
  209.  
  210. public int getPort() {
  211. return port;
  212. }
  213.  
  214. @Override
  215. public void writeExternal(ObjectOutput out) throws IOException {
  216. out.writeObject(this);
  217. }
  218.  
  219. @Override
  220. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  221.  
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement