Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scheduler.threads;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- import scheduler.Scheduler;
- import scheduler.TaskEngine;
- import scheduler.ThreadPool;
- import scheduler.enumerations.Load;
- import scheduler.enumerations.TaskEngineState;
- public class ThreadManageClientMessages implements Runnable {
- private Scheduler _scheduler = null;
- private Socket _socket = null;
- private BufferedReader _in = null;
- private PrintWriter _out = null;
- private String _identifier = null;
- private Boolean _active = true;
- private List<Future<TaskEngine>> _futureList = null;
- public ThreadManageClientMessages(Scheduler scheduler, Socket socket) {
- _scheduler = scheduler;
- _socket = socket;
- _scheduler.get_clients().put(_socket.toString(), this);
- try {
- _futureList = new ArrayList<Future<TaskEngine>>();
- _in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
- _out = new PrintWriter(_socket.getOutputStream(), true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void run() {
- String input = "";
- try {
- while (_active) {
- input = _in.readLine();
- if (input == null) break;
- if (input.startsWith("!login")) {
- checkLogin(input);
- }
- else if (input.equals("!logout")) {
- checkLogout();
- }
- else if (input.startsWith("!requestEngine")) {
- requestEngine(input);
- }
- else {
- _out.println("Unknown command.");
- }
- }
- _scheduler.get_clients().remove(_socket.toString());
- _in.close();
- _out.close();
- _socket.close();
- if (_identifier != null)_scheduler.get_companies().get(_identifier).set_online(false);
- } catch (IOException e) {
- if (_identifier != null) _scheduler.get_companies().get(_identifier).set_online(false);
- try {
- _socket.close();
- _scheduler.get_clients().remove(_socket.toString());
- } catch (Exception e1) {
- }
- }
- }
- private void checkLogout() {
- if (_identifier == null) {
- _out.println("You have to log in first.");
- }
- else {
- _scheduler.get_companies().get(_identifier).set_online(false);
- _identifier = null;
- _out.println("Successfully logged out.");
- }
- }
- private void checkLogin(String input) {
- String parts[] = input.split(" ");
- if (parts.length < 3) {
- _out.println("!login <username> <password>");
- return;
- }
- if (_identifier != null) {
- _out.println("You are already logged in.");
- return;
- }
- String username = String.valueOf(parts[1]);
- String password = String.valueOf(parts[2]);
- //System.out.println(_scheduler.get_companies().containsKey(username));
- if (!_scheduler.get_companies().containsKey(username)) {
- _out.println("Username or password do not exist.");
- return;
- }
- else if (!_scheduler.get_companies().get(username).get_password().equals(password)) {
- _out.println("Username or password do not exist.");
- return;
- }
- else if (_scheduler.get_companies().get(username).is_online() == true) {
- _out.println("User is already logged in.");
- return;
- }
- _scheduler.get_companies().get(username).set_online(true);
- _identifier = username;
- _out.println("Login successful.");
- }
- private void requestEngine(String input) {
- if (_identifier == null) {
- _out.println("You have to log in first.");
- return;
- }
- Load load = null;
- String id = null;
- String parts[] = (input + " ").split(" ");
- if (parts.length < 3) {
- _out.println("!request <load>");
- return;
- }
- try {
- load = Load.valueOf(parts[1]);
- id = parts[2];
- } catch (Exception e) {
- _out.println("!request <load>");
- return;
- }
- if (_scheduler.get_engines().size() == 0) {
- _out.println("No engines available.");
- return;
- }
- // Update all current engines loads
- getTaskEnginesLoad();
- TaskEngine currentEngine = null;
- float energy = Float.MAX_VALUE;
- float newLoad = 0.0f;
- for (TaskEngine engine : _scheduler.get_engines().values()) {
- if (engine.get_state() != TaskEngineState.ONLINE) {
- continue;
- }
- newLoad = _scheduler.getLoadValueOf(engine.get_load()) + _scheduler.getLoadValueOf(load);
- if (newLoad == 0.99f) newLoad = 1.0f;
- if (newLoad <= 1.0f && _scheduler.calculateConsumption(engine, newLoad) < energy) {
- currentEngine = engine;
- energy = _scheduler.calculateConsumption(engine, newLoad);
- }
- }
- if (currentEngine == null) {
- _out.println("Not enough capacity. Try again later.");
- return;
- }
- newLoad = _scheduler.getLoadValueOf(currentEngine.get_load()) + _scheduler.getLoadValueOf(load);
- // Update local load
- _scheduler.get_engines().get(currentEngine.get_identifier()).set_load(_scheduler.getLoadOf(newLoad));
- _out.println("!assignedEngine " + currentEngine.get_taskEngineHost() + " " + currentEngine.get_tcpPort()
- + " " + id);
- if (load == Load.LOW) {
- _scheduler.get_companies().get(_identifier).set_numberLow(_scheduler.get_companies().get(_identifier).get_numberLow() + 1);
- }
- else if (load == Load.MIDDLE) {
- _scheduler.get_companies().get(_identifier).set_numberMiddle(_scheduler.get_companies().get(_identifier).get_numberMiddle() + 1);
- }
- else if (load == Load.HIGH) {
- _scheduler.get_companies().get(_identifier).set_numberHigh(_scheduler.get_companies().get(_identifier).get_numberHigh() + 1);
- }
- }
- private void getTaskEnginesLoad() {
- for (TaskEngine taskEngine : _scheduler.get_engines().values()) {
- if (taskEngine.get_state() != TaskEngineState.OFFLINE) {
- CallableTaskEngineManageLoad tmpCallable = new CallableTaskEngineManageLoad(taskEngine);
- _futureList.add(ThreadPool.getInstance().getExecutor().submit(tmpCallable));
- }
- }
- for (Future<TaskEngine> future : _futureList) {
- try {
- _scheduler.get_engines().put(future.get().get_identifier(), future.get());
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- }
- }
- public void set_active(Boolean b) {
- _active = b;
- try {
- _socket.close();
- } catch (IOException e) {
- }
- }
- }
Add Comment
Please, Sign In to add comment