Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server;
- import java.net.*;
- import java.rmi.Naming;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.server.UnicastRemoteObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- import java.io.*;
- import static java.lang.System.*;
- public class TCPServer {
- public static String rmiHost;
- public static int rmiPort;
- public static int rmiPortBackUp;
- public static String ServerAHost;
- public static int ServerAPort;
- public static String ServerBHost;
- public static int ServerBPort;
- public static RMI rmiMain;
- public static void main(String[] args) throws RemoteException {
- carregaServerConfig();
- // ServerA servidor = new ServerA(serverAHost,serverAPort,rmiHost,rmiPort);
- Scanner sc1 = new Scanner(System.in);
- System.out.print("1 - ServerA\n2 - ServerB\n: ");
- String opc = sc1.nextLine();
- final ArrayList<Connection> connections = new ArrayList<Connection>();
- Server server;//TODO interface para registar mesas
- try {
- rmiMain=(RMI) Naming.lookup("rmi://" + rmiHost + ":" + rmiPort + "/rmi");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (NotBoundException e) {
- e.printStackTrace();
- System.exit(0);
- }
- if (opc.equals("1")) {
- server=new Server(ServerAHost, ServerAPort, rmiHost, rmiPort,connections);
- } else {
- server=new Server(ServerBHost, ServerBPort, rmiHost, rmiPort,connections);
- }
- try {
- rmiMain.subscribe((Server_rmi)server);
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- server.mesaVoto();
- }
- public static void carregaServerConfig() {
- String file = "TCPServerConfig.txt";
- String line;
- StringTokenizer tokenizer;
- System.out.println("Uploding Server configurations...");
- try {
- FileReader inputFile = new FileReader(file);
- BufferedReader buffer = new BufferedReader(inputFile);
- for (int i = 0; i < 7; i++) {
- line = buffer.readLine();
- tokenizer = new StringTokenizer(line, "=");
- line = tokenizer.nextToken();
- if (line.equals("Server A Host")) {
- ServerAHost = tokenizer.nextToken();
- } else if (line.equals("Server A Port")) {
- ServerAPort = Integer.parseInt(tokenizer.nextToken());
- } else if (line.equals("RMI Host")) {
- rmiHost = tokenizer.nextToken();
- } else if (line.equals("RMI Port")) {
- rmiPort = Integer.parseInt(tokenizer.nextToken());
- } else if (line.equals("RMI PortBackUp")) {
- rmiPortBackUp = Integer.parseInt(tokenizer.nextToken());
- } else if (line.equals("Server B Host")) {
- ServerBHost = tokenizer.nextToken();
- } else if (line.equals("Server B Port")) {
- ServerBPort = Integer.parseInt(tokenizer.nextToken());
- }
- }
- buffer.close();
- } catch (FileNotFoundException e) {
- System.out.println("File " + file + " not found");
- System.exit(0);
- } catch (IOException e) {
- } catch (NullPointerException e) {
- System.out.println("Error updating the data from the files.");
- }
- }
- }
- class Server extends UnicastRemoteObject implements Server_rmi{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public String myHost;
- public int myPort;
- public String rmiHost;
- public int rmiPort;
- public Receiver receiver;
- public RMI rmi = null;
- ArrayList<Connection> connections;
- ArrayList<Connection> connectionsDeleteCache;
- Mesas mesaVoto;
- ArrayList<String> buffer;
- public Server(String myHost, int myPort, String rmiHost, int rmiPort,ArrayList<Connection> connections)throws RemoteException {
- this.myHost = myHost;
- this.myPort = myPort;
- this.rmiHost = rmiHost;
- this.rmiPort = rmiPort;
- this.connections=connections;
- buffer=new ArrayList<String>();
- this.connectionsDeleteCache= new ArrayList<Connection>();
- try {
- rmi=(RMI) Naming.lookup("rmi://" + rmiHost + ":" + rmiPort + "/rmi");
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (RemoteException e) {
- e.printStackTrace();
- } catch (NotBoundException e) {
- e.printStackTrace();
- System.exit(0);
- }
- }
- public void mesaVoto() {
- String number_cc_string="";
- Scanner inputS = new Scanner(System.in);
- boolean mesa=false;
- ArrayList<Mesas> mesasVoto;
- int option=0;
- try {
- while(mesa==false) {
- try {
- out.println("Choose the representative table!");
- mesasVoto=rmi.getMesasVoto();
- for(Mesas x:mesasVoto) {
- System.out.println(++option+" - "+x.toString());
- }
- try {
- if(mesasVoto.size()!=0) {
- option=testOption(0, mesasVoto.size());
- mesaVoto=mesasVoto.get(option-1);
- }else {
- out.println("Não existem mesas criadas!");
- System.exit(0);
- }
- }catch(IndexOutOfBoundsException e) {
- System.out.println("indexoutofboundsexception");
- }
- receiver = new Receiver(myPort, rmiHost, rmiPort,connections,mesaVoto,rmi);
- mesa=true;
- } catch (RemoteException e) {
- int check=0;
- while(check==0) {
- try {
- rmi=(RMI) Naming.lookup("rmi://" + rmiHost + ":" + rmiPort + "/rmi");
- check=1;
- } catch (NotBoundException e1) {
- check=0;
- e1.printStackTrace();
- }
- }
- e.printStackTrace();
- }
- }
- while (true) {
- do {
- out.print("ID:");
- try {
- number_cc_string = inputS.nextLine();
- } catch (InputMismatchException e) {
- out.println("Invalid option");
- inputS.next();
- }
- } while (!checkNumbers(number_cc_string) || number_cc_string.length() != 8);
- addBuffer(number_cc_string);
- try {
- for(String pedido:buffer) {
- if(rmi.getATerminal(Integer.parseInt(pedido))) {
- out.println("ID aceite!");
- for(Connection x:connections) {
- if(x.isLive()) {
- if(x.available==true) {
- out.println("Terminal "+x.thread_id+" is free.");
- out.println("Go to terminal "+x.thread_id);
- x.outStream.println("Terminal unblocked of CC: "+pedido+"\n Press enter to continue your request.");
- x.cc=pedido;
- x.available=false;
- break;
- }else {
- out.println("Terminal "+x.thread_id+" occupied.");
- }
- }else {
- connectionsDeleteCache.add(x);
- }
- }
- for(Connection x:connectionsDeleteCache) {
- connections.remove(x);
- }
- connectionsDeleteCache.clear();
- }else {
- out.println("Your cc does not exist");
- }
- }
- buffer.clear();
- }catch(RemoteException e) {
- int check=0;
- while(check==0) {
- do {
- out.print("ID:");
- try {
- number_cc_string = inputS.nextLine();
- } catch (InputMismatchException e1) {
- out.println("Invalid option");
- inputS.next();
- }
- } while (!checkNumbers(number_cc_string) || number_cc_string.length() != 8);
- addBuffer(number_cc_string);
- try {
- rmi=(RMI) Naming.lookup("rmi://" + rmiHost + ":" + rmiPort + "/rmi");
- for(String pedido:buffer) {
- if(rmi.getATerminal(Integer.parseInt(pedido))) {
- out.println("ID aceite!");
- for(Connection x:connections) {
- if(x.isLive()) {
- if(x.available==true) {
- out.println("Terminal "+x.thread_id+" is free.");
- out.println("Go to terminal "+x.thread_id);
- x.outStream.println("Terminal unblocked of CC: "+pedido+"\n Press enter to continue your request.");
- x.cc=pedido;
- x.available=false;
- break;
- }else {
- out.println("Terminal "+x.thread_id+" occupied.");
- }
- }else {
- connectionsDeleteCache.add(x);
- }
- }
- for(Connection x:connectionsDeleteCache) {
- connections.remove(x);
- }
- connectionsDeleteCache.clear();
- }else {
- out.println("Your cc does not exist");
- }
- }
- buffer.clear();
- check=1;
- } catch (NotBoundException e1) {
- check=0;
- e1.printStackTrace();
- }catch(RemoteException e2) {
- check=0;
- }
- }
- }
- }
- }catch (IOException e) {
- System.out.println("IO:" + e);
- }
- }
- public void addBuffer(String pedido) {
- int check=0;
- for(String x:buffer) {
- if(x.equals(pedido)) {
- check=1;
- }
- }
- if(check==0) {
- buffer.add(pedido);
- }else {
- System.out.println("Pedido descartado(duplicado)");
- }
- }
- public static int testOption(int minimo, int maximo) {
- Scanner inputS = new Scanner(System.in);
- int opcao = 0, test;
- String numero;
- while (true) {
- test = 0;
- try {
- while (test != 1) {
- try {
- out.print("\n[" + minimo + "-" + maximo + "]Chose:");
- numero = inputS.nextLine();
- opcao = Integer.parseInt(numero);
- test = 1;
- } catch (Exception e) {
- out.print("Escolha invalida!");
- }
- }
- if (opcao < minimo || opcao > maximo) {
- out.print("Escolha invalida!");
- } else {
- return opcao;
- }
- } catch (InputMismatchException exception) {
- out.print("Escolha invalida!");
- inputS.next();
- }
- }
- }
- public static boolean checkNumbers(String num) {
- int i = 0;
- char[] array = num.toCharArray();
- while (num.length() != i) {
- if ((int) array[i] > 57 || (int) array[i] < 48) {
- out.println("Can not start or end with a space and can only ccontain numbers!");
- return false;
- }
- i++;
- }
- if (num.length() == 0) {
- out.println("Can not start or end with a space and can only ccontain numbers!");
- return false;
- }
- return true;
- }
- }
- class Receiver extends Thread {
- public int myPort;
- public String rmiHost;
- public int rmiPort;
- public int thread_id = 0;
- ArrayList<Connection> connections;
- Mesas mesaVoto;
- RMI rmi;
- public Receiver(int myPort, String rmiHost, int rmiPort,ArrayList<Connection> connections,Mesas mesaVoto,RMI rmi) {
- this.myPort = myPort;
- this.rmiHost = rmiHost;
- this.rmiPort = rmiPort;
- this.connections=connections;
- this.mesaVoto=mesaVoto;
- this.rmi=rmi;
- this.start();
- }
- public void run() {
- try {
- ServerSocket listenSocket = new ServerSocket(myPort);
- System.out.println("LISTEN SOCKET=" + listenSocket);
- while (true) {
- Socket clientSocket = listenSocket.accept(); // BLOQUEANTE
- System.out.println("CLIENT_SOCKET (created at accept())=" + clientSocket);
- thread_id++;
- connections.add(new Connection(clientSocket, connections, rmiHost, rmiPort, thread_id,mesaVoto,rmi));
- }
- }catch (EOFException e) {
- System.out.println("EOF:" + e);
- } catch (IOException e) {
- System.out.println("Listen:" + e.getMessage());
- }
- }
- }
- class Connection extends Thread {
- public BufferedReader inStream = null;
- public PrintWriter outStream;
- public Socket clientSocket;
- public String rmiHost;
- public int rmiPort;
- public int thread_id;
- public RMI rmi;
- public String cc;
- public boolean available;
- public boolean loggedIN;
- ArrayList<Connection> lista;
- Mesas mesaVoto;
- ArrayList<String> buffer;
- public Connection(Socket aClientSocket, ArrayList<Connection> connections, String rmiHost, int rmiPort,int numero,Mesas mesaVoto,RMI rmi) {
- this.thread_id = numero;
- this.lista = connections;
- this.clientSocket = aClientSocket;
- this.rmiHost = rmiHost;
- this.rmiPort = rmiPort;
- this.mesaVoto=mesaVoto;
- this.buffer=new ArrayList<String>();
- cc=null;
- available=true;
- loggedIN=false;
- try {
- //this.rmi = (RMI) Naming.lookup("rmi://" + rmiHost + ":" + rmiPort + "/rmi");
- this.rmi=rmi;
- inStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- outStream = new PrintWriter(clientSocket.getOutputStream(), true);
- this.start();
- } catch (IOException e) {
- System.out.println("Connection:" + e.getMessage());
- }/* catch (NotBoundException e) {
- e.printStackTrace();
- }*/
- }
- public boolean isLive() {
- try {
- clientSocket.sendUrgentData(1);
- } catch (IOException e) {
- return false;
- }
- return true;
- /*if(clientSocket.getRemoteSocketAddress()==null) {
- System.out.println("adeus crlh");
- return true;
- }else {
- System.out.println("ola crlh");
- return false;
- }*/
- }
- // =============================
- public void addBuffer(String s) {
- try {
- String pedido;
- HashMap<String, String> m = ProtocolParser.parse(s);
- pedido=m.get("type");
- int check=0;
- for(String x:buffer) {
- if(x.equals(pedido)) {
- check=1;
- }
- }
- if(check==0) {
- System.out.println(pedido);
- buffer.add(s);
- }else {
- System.out.println("Pedido descartado(duplicado)");
- }
- }catch (IllegalArgumentException e){
- System.out.println("type: error_message, IllegalArgumentException");
- } catch (NullPointerException e) {
- System.out.println("type: error_message,NullPointerException");
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("type: error_message, ArrayIndexOutOfBoundsException");
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void run() {
- String pedido="";
- int check=0;
- boolean state=true;
- boolean exception=true;
- while(exception) {
- if (available==false){
- try {
- clientSocket.setSoTimeout(120000);
- while(true) {
- pedido=inStream.readLine();
- try {
- HashMap<String, String> m = ProtocolParser.parse(pedido);
- switch (m.get("type")) {
- case ("login"): {
- String username = m.get("username").equals(null) ? null : m.get("username");
- String password = m.get("password").equals(null) ? null : m.get("password");
- if (rmi.login(username, password)) {
- System.out.println("Login with sucess!");
- loggedIN = true;
- rmi.loginDone(cc,mesaVoto);
- } else {
- System.out.println("Username and Password do not correspond");
- }
- break;
- }
- case ("status"): {
- boolean status;
- status = rmi.status(cc);
- if (status) {
- outStream.println("Looged on");
- } else {
- outStream.println("Looged off");
- }
- break;
- }
- case ("elections"): {
- boolean status = rmi.status(cc);
- if (status) {
- System.out.println("hello");
- outStream.println(rmi.print_elections_for_person(cc,mesaVoto));
- } else {
- outStream.println("Looged off!");
- }
- break;
- }
- case ("vote"): {
- boolean status = rmi.status(cc);
- if (status) {
- String eleicao = m.get("election").equals(null) ? null : m.get("election");
- String list = m.get("list").equals(null) ? null : m.get("list");
- outStream.println(rmi.register_vote(cc, eleicao, list,mesaVoto));
- } else {
- outStream.println("Looged off");
- }
- break;
- }
- default:{
- System.out.println("type: error_message, status: not enought arguments1");
- System.out.println("default");
- break;
- }
- }
- }catch (RemoteException er) {
- while(check==0) {
- System.out.println(pedido);
- addBuffer(pedido);
- try {
- pedido=inStream.readLine();
- rmi=(RMI) Naming.lookup("rmi://" + rmiHost + ":" + rmiPort + "/rmi");
- for(String x:buffer) {
- HashMap<String, String> m = ProtocolParser.parse(x);
- switch (m.get("type")) {
- case ("login"): {
- String username = m.get("username").equals(null) ? null : m.get("username");
- String password = m.get("password").equals(null) ? null : m.get("password");
- if (rmi.login(username, password)) {
- System.out.println("Login with sucess!");
- loggedIN = true;
- rmi.loginDone(cc,mesaVoto);
- } else {
- System.out.println("Username and Password do not correspond");
- }
- break;
- }
- case ("status"): {
- boolean status;
- status = rmi.status(cc);
- if (status) {
- outStream.println("Looged on");
- } else {
- outStream.println("Looged off");
- }
- break;
- }
- case ("elections"): {
- boolean status = rmi.status(cc);
- if (status) {
- System.out.println("hello");
- outStream.println(rmi.print_elections_for_person(cc,mesaVoto));
- } else {
- outStream.println("Looged off!");
- }
- break;
- }
- case ("vote"): {
- boolean status = rmi.status(cc);
- if (status) {
- String eleicao = m.get("election").equals(null) ? null : m.get("election");
- String list = m.get("list").equals(null) ? null : m.get("list");
- outStream.println(rmi.register_vote(cc, eleicao, list,mesaVoto));
- } else {
- outStream.println("Looged off");
- }
- break;
- }
- default:{
- System.out.println("type: error_message, status: not enought arguments1");
- System.out.println("default");
- break;
- }
- }
- }
- check=1;
- buffer.clear();
- } catch (RemoteException er1) {
- System.out.println("olaola");
- check=0;
- }catch (ArrayIndexOutOfBoundsException e) {
- check=0;
- }catch (NotBoundException e1) {
- check=0;
- }catch (IOException e1) {
- check=0;
- e1.printStackTrace();
- }
- }
- } catch (IllegalArgumentException e){
- System.out.println("type: error_message, status: wrong arguments1");
- } catch (NullPointerException e) {
- System.out.println("type: error_message, status: wrong arguments");
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("type: error_message, status: not enought arguments");
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }catch (SocketTimeoutException ste) {
- System.out.println("Terminal"+thread_id+" blocked. More then 120 sec without request");
- available = true;
- buffer.clear();
- } catch (EOFException e) {
- System.out.println("EOF:" + e);
- exception=false;
- } catch (IOException e) {
- exception=false;
- System.out.println("IO:" + e);
- try {
- clientSocket.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }else {
- try {
- inStream.readLine();
- }catch (IOException e) {}
- }
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement