Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.rmi.NotBoundException;
  4. import java.rmi.RemoteException;
  5. import java.rmi.registry.LocateRegistry;
  6. import java.util.LinkedHashMap;
  7.  
  8. public class TCPServerImpl extends java.rmi.server.UnicastRemoteObject implements TCPServer{
  9. static RMIServer RMI = null;
  10. public TCPServerImpl() throws java.rmi.RemoteException{
  11. super();
  12. }
  13.  
  14. public static void main(String args[]){
  15. try {
  16. rmiConnection();
  17. int number=0;
  18. try {
  19. int serverPort = 6000;
  20. System.out.println("Listening on port 6000!");
  21. ServerSocket listenSocket = new ServerSocket(serverPort);
  22. System.out.println("LISTEN SOCKET="+listenSocket);
  23. while(true) {
  24. Socket clientSocket = listenSocket.accept(); // BLOQUEANTE
  25. System.out.println("CLIENT_SOCKET (created at accept())="+clientSocket);
  26. number++;
  27. new Connection(clientSocket, number);
  28. }
  29. } catch(IOException e) {
  30. System.out.println("Listen:" + e.getMessage());
  31. }
  32. } catch (Exception e) {
  33. System.out.println("Exception in main: " + e);
  34. }
  35. }
  36. static void rmiConnection(){
  37. try {
  38. TCPServerImpl.RMI = (RMIServer) LocateRegistry.getRegistry(7000).lookup("iBei");
  39. } catch (RemoteException | NotBoundException e1) {
  40. e1.printStackTrace();
  41. }
  42. }
  43.  
  44. }
  45.  
  46. class Connection extends Thread {
  47.  
  48. PrintWriter out;
  49. BufferedReader in = null;
  50. private Socket clientSocket;
  51. private int thread_number;
  52.  
  53. public Connection (Socket newClientSocket, int number) {
  54. this.thread_number = number;
  55. try{
  56. this.clientSocket = newClientSocket;
  57. this.in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
  58. this.out = new PrintWriter(this.clientSocket.getOutputStream(), true);
  59.  
  60. this.start();
  61. }catch(IOException e){System.out.println("Connection:" + e.getMessage());}
  62. }
  63. //=============================
  64. public void run(){
  65. String resposta;
  66. try{
  67. while(true){
  68. //an echo server
  69. String data = in.readLine();
  70. System.out.println("T["+thread_number + "] Recebeu: "+data);
  71. parseUserInput(data);
  72. //resposta=String.valueOf(RMI.register("Dinis","dinis","dinis"));
  73. //out.writeUTF(resposta);
  74. }
  75. }catch(EOFException e){System.out.println("EOF:" + e);
  76. }catch(IOException e){System.out.println("IO:" + e);}
  77. }
  78.  
  79. private void parseUserInput(String data){
  80.  
  81. String[] aux;
  82. LinkedHashMap<String, String> parsedInput = new LinkedHashMap<String, String>();
  83.  
  84. aux = data.split(",");
  85.  
  86. for (String field : aux) {
  87. String[] split = field.split(":");
  88. String firstSubString = split[0].trim();
  89. String secondSubString = split[1].trim();
  90. parsedInput.put(firstSubString, secondSubString);
  91. }
  92. System.out.println(parsedInput);
  93.  
  94. chosenType(parsedInput);
  95. }
  96.  
  97. private void chosenType(LinkedHashMap<String, String> parsedInput){
  98. String type = parsedInput.get("type");
  99.  
  100. switch(type){
  101. case "login":
  102. login(parsedInput);
  103. break;
  104. case "status":
  105. break;
  106. case "item_list":
  107. System.out.println("123");
  108. break;
  109. case "register":
  110. register(parsedInput);
  111. break;
  112. case "create_auction":
  113. create_auction(parsedInput);
  114. break;
  115. case "search_auction":
  116. System.out.println("123");
  117. break;
  118. case "detail_auction":
  119. System.out.println("123");
  120. break;
  121. case "my_auctions":
  122. System.out.println("123");
  123. break;
  124. case "bid":
  125. System.out.println("123");
  126. break;
  127. case "edit_auction":
  128. System.out.println("123");
  129. break;
  130. case "message":
  131. System.out.println("123");
  132. break;
  133. case "online_users":
  134. System.out.println("123");
  135. break;
  136. }
  137. }
  138.  
  139. //type : login , username : pierre , password : omidyar
  140. //TODO: CHECK IF USER IS ALREADY LOGGED
  141. private void login(LinkedHashMap<String, String> parsedInput){
  142. String username, password;
  143. username = parsedInput.get("username");
  144. password = parsedInput.get("password");
  145. try {
  146. if(TCPServerImpl.RMI.login(username, password)){
  147. out.println("type : login , ok : true");
  148. } else {
  149. out.println("type : login , ok : false");
  150. }
  151. } catch (IOException e) {
  152. try {
  153. System.out.println("Connection with problems...");
  154. Thread.sleep(5000);
  155. } catch (InterruptedException e1) {
  156. e1.printStackTrace();
  157. }
  158. TCPServerImpl.rmiConnection();
  159. login(parsedInput);
  160.  
  161. //System.out.println("Crashei-me todo!!!!!!");
  162. //Problems with rmi connection
  163. }
  164. }
  165.  
  166. // type : register , username : pierre , password : omidyar
  167. private void register(LinkedHashMap<String, String> parsedInput){
  168. String username, password;
  169. username = parsedInput.get("username");
  170. password = parsedInput.get("password");
  171.  
  172. try {
  173. if(TCPServerImpl.RMI.register(username, password)){
  174. out.println("type : register , ok : true");
  175. } else {
  176. out.println("type : register , ok : false");
  177. }
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181. }
  182.  
  183. private void create_auction(LinkedHashMap<String, String> parsedInput){
  184.  
  185. }
  186.  
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement