Guest User

Untitled

a guest
Jul 15th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. package Server;
  2.  
  3. import java.io.IOException;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.Map.Entry;
  10.  
  11.  
  12. public class Server {
  13.  
  14. //handle singleton
  15. private static Server myInstance = new Server();
  16.  
  17. public static Server getInstance() {
  18. return myInstance;
  19. }
  20.  
  21. //private constructor
  22. private Server() {
  23. userMap = new HashMap<String, String>();
  24. lastSeen = new HashMap<String,String>();
  25. user2Ip = new HashMap<String,String>();
  26. }
  27.  
  28.  
  29. private static void usage(){
  30. System.err.println("Nap use this: ./server tcpport udpport where port is an int from 12400-12500");
  31. System.exit(1);
  32. }
  33.  
  34. private static void checkPorts(){
  35.  
  36. if( (tcpPort<12400 || tcpPort>12500) || (udpPort<12400 || udpPort>12500) )
  37. usage();
  38. }
  39.  
  40. /**
  41. * returns 0 if user successfuly logged in
  42. * returns 1 if user is not known
  43. * returns 2 if username and password do not match
  44. */
  45. public static int logonUser(String _username,String _password, String _ipAdress)
  46. {
  47. boolean hasKey = userMap.containsKey(_username);
  48. if(!hasKey)
  49. return 1;
  50. String pw4User = userMap.get(_username);
  51. if(pw4User.equals(_password))
  52. {
  53. //Do logon stuff here
  54. //boolean hasKeyLS = lastSeen.containsKey(_username);
  55. //if(!hasKeyLS){
  56. // when user logs on or off
  57. java.util.Date now = new Date(System.currentTimeMillis());
  58. java.text.DateFormat formatter = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm");
  59. String result = formatter.format(now); // e.g. result == "10.09.2009 17:44"
  60. String loggedOn = "1:"+result;
  61. lastSeen.put(_username, loggedOn);
  62. user2Ip.put(_username, _ipAdress);
  63. //}else{
  64.  
  65. //}
  66. return 0;
  67. }
  68. return 2;
  69. }
  70.  
  71. public static void logoffUser(String _username)
  72. {
  73. java.util.Date now = new Date(System.currentTimeMillis());
  74. java.text.DateFormat formatter = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm");
  75. String result = formatter.format(now); // e.g. result == "10.09.2009 17:44"
  76. String loggedOff = "0:"+result;
  77. lastSeen.put(_username, loggedOff);
  78. }
  79.  
  80. public static String getUserInfo(String _username)
  81. {
  82. boolean hasKey = lastSeen.containsKey(_username);
  83. if(!hasKey)
  84. return "2:never logged on";
  85.  
  86. //returns the userstatus
  87. return lastSeen.get(_username);
  88. }
  89.  
  90. /**
  91. * @param args
  92. * [TcpPort] Tcp Port on which the server should listen
  93. * [UdpPort] Udp Port on which the server should listen
  94. */
  95. public static void main(String[] args) {
  96. // TODO Auto-generated method stub
  97.  
  98. if(args.length != 2)
  99. usage();
  100. //check if the 2 port arguments are integers
  101. try {
  102. tcpPort = Integer.parseInt(args[0]);
  103. udpPort = Integer.parseInt(args[1]);
  104. } catch (NumberFormatException e) {
  105. System.err.println("Arguments must be an integer");
  106. System.exit(1);
  107. }
  108.  
  109. //print usage message and exit if ports are not valid
  110. checkPorts();
  111.  
  112. java.io.InputStream in = ClassLoader.getSystemResourceAsStream("users.properties");
  113. if (in != null) {
  114.  
  115. java.util.Properties users = new java.util.Properties();
  116. try{
  117. users.load(in);
  118. }catch(IOException e){
  119.  
  120. }
  121. java.util.Set<String> usernames = users.stringPropertyNames(); // get all usernames
  122.  
  123. for (String username : usernames) {
  124. String password = users.getProperty(username); // get password for user with username
  125. //fil map with users from config file
  126. userMap.put(username, password);
  127. }
  128. }else{
  129. System.err.println("The required file users.properties was not found.");
  130. System.exit(1);
  131. }
  132.  
  133. Set<Entry<String, String>> s = userMap.entrySet();
  134. Iterator<Entry<String, String>> it = s.iterator();
  135.  
  136. while(it.hasNext())
  137. {
  138. // key=value separator this by Map.Entry to get key and value
  139. Map.Entry<String, String> m =(Map.Entry<String, String>)it.next();
  140.  
  141. // getKey is used to get key of Map
  142. String key=(String)m.getKey();
  143.  
  144. // getValue is used to get value of key in Map
  145. String value=(String)m.getValue();
  146.  
  147. System.out.println("Key :"+key+" Value :"+value);
  148. }
  149. System.out.println("logging on user john returns: "+logonUser("john", "12345", "192.168.0.1:12000"));
  150. System.out.println("userinfo for john: "+getUserInfo("john"));
  151.  
  152.  
  153.  
  154. }
  155.  
  156. //Map<username, password>
  157. Map<String,String> userMap;
  158. //Map<username, loggedOn[0||1]:timestamp>
  159. Map<String,String> lastSeen;
  160. //Map<username, ip:port>
  161. Map<String,String> user2Ip;
  162. int tcpPort, udpPort;
  163.  
  164.  
  165.  
  166. }
Add Comment
Please, Sign In to add comment