Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Server;
- import java.io.IOException;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.Map.Entry;
- public class Server {
- //handle singleton
- private static Server myInstance = new Server();
- public static Server getInstance() {
- return myInstance;
- }
- //private constructor
- private Server() {
- userMap = new HashMap<String, String>();
- lastSeen = new HashMap<String,String>();
- user2Ip = new HashMap<String,String>();
- }
- private static void usage(){
- System.err.println("Nap use this: ./server tcpport udpport where port is an int from 12400-12500");
- System.exit(1);
- }
- private static void checkPorts(){
- if( (tcpPort<12400 || tcpPort>12500) || (udpPort<12400 || udpPort>12500) )
- usage();
- }
- /**
- * returns 0 if user successfuly logged in
- * returns 1 if user is not known
- * returns 2 if username and password do not match
- */
- public static int logonUser(String _username,String _password, String _ipAdress)
- {
- boolean hasKey = userMap.containsKey(_username);
- if(!hasKey)
- return 1;
- String pw4User = userMap.get(_username);
- if(pw4User.equals(_password))
- {
- //Do logon stuff here
- //boolean hasKeyLS = lastSeen.containsKey(_username);
- //if(!hasKeyLS){
- // when user logs on or off
- java.util.Date now = new Date(System.currentTimeMillis());
- java.text.DateFormat formatter = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm");
- String result = formatter.format(now); // e.g. result == "10.09.2009 17:44"
- String loggedOn = "1:"+result;
- lastSeen.put(_username, loggedOn);
- user2Ip.put(_username, _ipAdress);
- //}else{
- //}
- return 0;
- }
- return 2;
- }
- public static void logoffUser(String _username)
- {
- java.util.Date now = new Date(System.currentTimeMillis());
- java.text.DateFormat formatter = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm");
- String result = formatter.format(now); // e.g. result == "10.09.2009 17:44"
- String loggedOff = "0:"+result;
- lastSeen.put(_username, loggedOff);
- }
- public static String getUserInfo(String _username)
- {
- boolean hasKey = lastSeen.containsKey(_username);
- if(!hasKey)
- return "2:never logged on";
- //returns the userstatus
- return lastSeen.get(_username);
- }
- /**
- * @param args
- * [TcpPort] Tcp Port on which the server should listen
- * [UdpPort] Udp Port on which the server should listen
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- if(args.length != 2)
- usage();
- //check if the 2 port arguments are integers
- try {
- tcpPort = Integer.parseInt(args[0]);
- udpPort = Integer.parseInt(args[1]);
- } catch (NumberFormatException e) {
- System.err.println("Arguments must be an integer");
- System.exit(1);
- }
- //print usage message and exit if ports are not valid
- checkPorts();
- java.io.InputStream in = ClassLoader.getSystemResourceAsStream("users.properties");
- if (in != null) {
- java.util.Properties users = new java.util.Properties();
- try{
- users.load(in);
- }catch(IOException e){
- }
- java.util.Set<String> usernames = users.stringPropertyNames(); // get all usernames
- for (String username : usernames) {
- String password = users.getProperty(username); // get password for user with username
- //fil map with users from config file
- userMap.put(username, password);
- }
- }else{
- System.err.println("The required file users.properties was not found.");
- System.exit(1);
- }
- Set<Entry<String, String>> s = userMap.entrySet();
- Iterator<Entry<String, String>> it = s.iterator();
- while(it.hasNext())
- {
- // key=value separator this by Map.Entry to get key and value
- Map.Entry<String, String> m =(Map.Entry<String, String>)it.next();
- // getKey is used to get key of Map
- String key=(String)m.getKey();
- // getValue is used to get value of key in Map
- String value=(String)m.getValue();
- System.out.println("Key :"+key+" Value :"+value);
- }
- System.out.println("logging on user john returns: "+logonUser("john", "12345", "192.168.0.1:12000"));
- System.out.println("userinfo for john: "+getUserInfo("john"));
- }
- //Map<username, password>
- Map<String,String> userMap;
- //Map<username, loggedOn[0||1]:timestamp>
- Map<String,String> lastSeen;
- //Map<username, ip:port>
- Map<String,String> user2Ip;
- int tcpPort, udpPort;
- }
Add Comment
Please, Sign In to add comment