Guest User

Untitled

a guest
Mar 4th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. package scheduler.threads;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.Socket;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.concurrent.ExecutionException;
  11. import java.util.concurrent.Future;
  12.  
  13. import scheduler.Scheduler;
  14. import scheduler.TaskEngine;
  15. import scheduler.ThreadPool;
  16. import scheduler.enumerations.Load;
  17. import scheduler.enumerations.TaskEngineState;
  18.  
  19. public class ThreadManageClientMessages implements Runnable {
  20.  
  21.     private Scheduler _scheduler = null;
  22.    
  23.     private Socket _socket = null;
  24.    
  25.     private BufferedReader _in = null;
  26.    
  27.     private PrintWriter _out = null;
  28.    
  29.     private String _identifier = null;
  30.    
  31.     private Boolean _active = true;
  32.    
  33.     private List<Future<TaskEngine>> _futureList = null;
  34.    
  35.     public ThreadManageClientMessages(Scheduler scheduler, Socket socket) {
  36.         _scheduler = scheduler;
  37.         _socket = socket;
  38.         _scheduler.get_clients().put(_socket.toString(), this);
  39.         try {
  40.             _futureList = new ArrayList<Future<TaskEngine>>();
  41.             _in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
  42.             _out = new PrintWriter(_socket.getOutputStream(), true);
  43.         } catch (IOException e) {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47.  
  48.     @Override
  49.     public void run() {
  50.         String input = "";
  51.         try {
  52.             while (_active) {
  53.                 input = _in.readLine();
  54.                 if (input == null) break;
  55.                 if (input.startsWith("!login")) {
  56.                     checkLogin(input);
  57.                 }
  58.                 else if (input.equals("!logout")) {
  59.                     checkLogout();
  60.                 }
  61.                 else if (input.startsWith("!requestEngine")) {
  62.                     requestEngine(input);
  63.                 }
  64.                 else {
  65.                     _out.println("Unknown command.");
  66.                 }
  67.             }
  68.             _scheduler.get_clients().remove(_socket.toString());
  69.             _in.close();
  70.             _out.close();
  71.             _socket.close();
  72.             if (_identifier != null)_scheduler.get_companies().get(_identifier).set_online(false);         
  73.         } catch (IOException e) {
  74.             if (_identifier != null) _scheduler.get_companies().get(_identifier).set_online(false);
  75.             try {
  76.                 _socket.close();
  77.                 _scheduler.get_clients().remove(_socket.toString());
  78.             } catch (Exception e1) {
  79.                
  80.             }
  81.         }
  82.     }
  83.    
  84.     private void checkLogout() {
  85.         if (_identifier == null) {
  86.             _out.println("You have to log in first.");
  87.         }
  88.         else {
  89.             _scheduler.get_companies().get(_identifier).set_online(false);
  90.             _identifier = null;
  91.             _out.println("Successfully logged out.");
  92.         }
  93.     }
  94.    
  95.     private void checkLogin(String input) {
  96.         String parts[] = input.split(" ");
  97.         if (parts.length < 3) {
  98.             _out.println("!login <username> <password>");
  99.             return;
  100.         }
  101.        
  102.         if (_identifier != null) {
  103.             _out.println("You are already logged in.");
  104.             return;
  105.         }  
  106.        
  107.         String username = String.valueOf(parts[1]);
  108.         String password = String.valueOf(parts[2]);
  109.         //System.out.println(_scheduler.get_companies().containsKey(username));
  110.         if (!_scheduler.get_companies().containsKey(username)) {
  111.             _out.println("Username or password do not exist.");
  112.             return;
  113.         }
  114.         else if (!_scheduler.get_companies().get(username).get_password().equals(password)) {
  115.             _out.println("Username or password do not exist.");
  116.             return;
  117.         }
  118.         else if (_scheduler.get_companies().get(username).is_online() == true) {
  119.             _out.println("User is already logged in.");
  120.             return;
  121.         }
  122.         _scheduler.get_companies().get(username).set_online(true);
  123.         _identifier = username;
  124.         _out.println("Login successful.");
  125.     }
  126.    
  127.     private void requestEngine(String input) {
  128.         if (_identifier == null) {
  129.             _out.println("You have to log in first.");
  130.             return;
  131.         }
  132.         Load load = null;
  133.         String id = null;
  134.         String parts[] = (input + " ").split(" ");
  135.         if (parts.length < 3) {
  136.             _out.println("!request <load>");
  137.             return;
  138.         }
  139.         try {
  140.             load = Load.valueOf(parts[1]);
  141.             id = parts[2];
  142.         } catch (Exception e) {
  143.             _out.println("!request <load>");
  144.             return;
  145.         }
  146.        
  147.         if (_scheduler.get_engines().size() == 0) {
  148.             _out.println("No engines available.");
  149.             return;
  150.         }
  151.        
  152.         // Update all current engines loads
  153.        
  154.         getTaskEnginesLoad();
  155.        
  156.        
  157.         TaskEngine currentEngine = null;
  158.         float energy = Float.MAX_VALUE;
  159.         float newLoad = 0.0f;
  160.        
  161.         for (TaskEngine engine : _scheduler.get_engines().values()) {
  162.             if (engine.get_state() != TaskEngineState.ONLINE) {
  163.                 continue;
  164.             }
  165.  
  166.             newLoad = _scheduler.getLoadValueOf(engine.get_load()) + _scheduler.getLoadValueOf(load);
  167.             if (newLoad == 0.99f) newLoad = 1.0f;
  168.             if (newLoad <= 1.0f && _scheduler.calculateConsumption(engine, newLoad) < energy) {
  169.                 currentEngine = engine;
  170.                 energy = _scheduler.calculateConsumption(engine, newLoad);
  171.             }
  172.         }
  173.        
  174.         if (currentEngine == null) {
  175.             _out.println("Not enough capacity. Try again later.");
  176.             return;
  177.         }
  178.        
  179.         newLoad = _scheduler.getLoadValueOf(currentEngine.get_load()) + _scheduler.getLoadValueOf(load);
  180.         // Update local load
  181.         _scheduler.get_engines().get(currentEngine.get_identifier()).set_load(_scheduler.getLoadOf(newLoad));
  182.         _out.println("!assignedEngine " + currentEngine.get_taskEngineHost() + " " + currentEngine.get_tcpPort()
  183.                 + " " + id);
  184.         if (load == Load.LOW) {
  185.             _scheduler.get_companies().get(_identifier).set_numberLow(_scheduler.get_companies().get(_identifier).get_numberLow() + 1);
  186.         }
  187.         else if (load == Load.MIDDLE) {
  188.             _scheduler.get_companies().get(_identifier).set_numberMiddle(_scheduler.get_companies().get(_identifier).get_numberMiddle() + 1);
  189.         }
  190.         else if (load == Load.HIGH) {
  191.             _scheduler.get_companies().get(_identifier).set_numberHigh(_scheduler.get_companies().get(_identifier).get_numberHigh() + 1);
  192.         }      
  193.        
  194.     }
  195.    
  196.     private void getTaskEnginesLoad() {
  197.        
  198.         for (TaskEngine taskEngine : _scheduler.get_engines().values()) {
  199.             if (taskEngine.get_state() != TaskEngineState.OFFLINE) {
  200.                 CallableTaskEngineManageLoad tmpCallable = new CallableTaskEngineManageLoad(taskEngine);
  201.                 _futureList.add(ThreadPool.getInstance().getExecutor().submit(tmpCallable));
  202.             }
  203.         }
  204.        
  205.         for (Future<TaskEngine> future : _futureList) {
  206.             try {
  207.                 _scheduler.get_engines().put(future.get().get_identifier(), future.get());
  208.             } catch (InterruptedException e) {
  209.                 e.printStackTrace();
  210.             } catch (ExecutionException e) {
  211.                 e.printStackTrace();
  212.             }
  213.         }  
  214.     }
  215.    
  216.     public void set_active(Boolean b) {
  217.         _active = b;
  218.         try {
  219.             _socket.close();
  220.         } catch (IOException e) {
  221.         }
  222.     }
  223.  
  224.    
  225. }
Add Comment
Please, Sign In to add comment