diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..0cbf9cd --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore index a17f1cf..f3212c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,96 @@ -bin/*.class -.classpath -.project -.settings \ No newline at end of file +# Created by https://www.toptal.com/developers/gitignore/api/eclipse,java +# Edit at https://www.toptal.com/developers/gitignore?templates=eclipse,java + +### Eclipse ### +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ +.apt_generated_test/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +# Uncomment this line if you wish to ignore the project description file. +# Typically, this file would be tracked if it contains build/dependency configurations: +#.project + +### Eclipse Patch ### +# Spring Boot Tooling +.sts4-cache/ + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* + +# End of https://www.toptal.com/developers/gitignore/api/eclipse,java diff --git a/.project b/.project new file mode 100644 index 0000000..b4815a7 --- /dev/null +++ b/.project @@ -0,0 +1,28 @@ + + + VOTACAO-JAVA-RMI + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + + + 1666374968243 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..322e409 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM openjdk:8 + +WORKDIR /app + +COPY /src /app/src + +RUN cd /app/src/ && javac Servidor.java + +EXPOSE 8899 \ No newline at end of file diff --git a/bin/implementacoes/IsEvenImpl.class b/bin/implementacoes/IsEvenImpl.class deleted file mode 100644 index d771697..0000000 Binary files a/bin/implementacoes/IsEvenImpl.class and /dev/null differ diff --git a/bin/implementacoes/VotacaoImpl.class b/bin/implementacoes/VotacaoImpl.class index 914d019..b30fc73 100644 Binary files a/bin/implementacoes/VotacaoImpl.class and b/bin/implementacoes/VotacaoImpl.class differ diff --git a/bin/implementacoes/VotoImpl.class b/bin/implementacoes/VotoImpl.class index 08a86e8..99420f5 100644 Binary files a/bin/implementacoes/VotoImpl.class and b/bin/implementacoes/VotoImpl.class differ diff --git a/bin/interfaces/IsEvenInterface.class b/bin/interfaces/IsEvenInterface.class deleted file mode 100644 index 54046b0..0000000 Binary files a/bin/interfaces/IsEvenInterface.class and /dev/null differ diff --git a/bin/interfaces/VotacaoInterface.class b/bin/interfaces/VotacaoInterface.class index f17d39d..67429c6 100644 Binary files a/bin/interfaces/VotacaoInterface.class and b/bin/interfaces/VotacaoInterface.class differ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6c3fe66 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3.8' +services: + servidor: + build: + context: . + container_name: votation_server_tcp + ports: + - 8899:8899 + command: java -classpath /app/src Servidor \ No newline at end of file diff --git a/.DS_Store b/src/.DS_Store similarity index 79% rename from .DS_Store rename to src/.DS_Store index a358587..2ac3215 100644 Binary files a/.DS_Store and b/src/.DS_Store differ diff --git a/src/Cliente.java b/src/Cliente.java index 450772b..31663cd 100644 --- a/src/Cliente.java +++ b/src/Cliente.java @@ -7,10 +7,10 @@ import interfaces.VotacaoInterface; public class Cliente { - static int porta = 1099; + static int porta = 8898; public static void main(String[] args) throws Exception { - String objName = "rmi://localhost:" + porta + "/server"; + String objName = "//localhost:" + porta + "/server"; VotacaoInterface votacao = (VotacaoInterface) Naming.lookup(objName); List candidatos = votacao.listarCandidatos(); @@ -19,12 +19,12 @@ public class Cliente { System.out.println(x.getNome() + " " + x.getNumero()); }); + @SuppressWarnings("resource") Scanner entrada = new Scanner(System.in); System.out.println("Digite seu voto"); String numero_candidato = entrada.nextLine(); int posicao = votacao.buscarCandidato(numero_candidato); System.out.println("Seu voto foi: " + candidatos.get(posicao).getNome()); - votacao.apuracao(); } } \ No newline at end of file diff --git a/src/ServidorApuracao.java b/src/ServidorApuracao.java new file mode 100644 index 0000000..c8b8ba7 --- /dev/null +++ b/src/ServidorApuracao.java @@ -0,0 +1,33 @@ +import java.rmi.Naming; +import java.rmi.registry.LocateRegistry; +import java.util.List; + +import implementacoes.ApuracaoImpl; +import implementacoes.CandidatoImpl; +import implementacoes.VotacaoImpl; +import interfaces.ApuracaoInterface; +import interfaces.VotacaoInterface; + + +public class ServidorApuracao { + + static List candidatos; + static ApuracaoInterface apuracao; + static int porta = 8899; + + public static void main(String[] args) { + try { + System.out.println("Registrando o objeto no RMIRegistry..."); + LocateRegistry.createRegistry(porta); + + //votacaoImpl = new VotacaoImpl(candidatos); + apuracao = new ApuracaoImpl(); + + Naming.rebind("//localhost:" + porta + "/server", apuracao); + + System.out.println("Servidor de Apuração. Aguardando Clientes na porta " + porta + "!"); + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/Servidor.java b/src/ServidorVotacao.java similarity index 52% rename from src/Servidor.java rename to src/ServidorVotacao.java index 3a31120..ad66847 100644 --- a/src/Servidor.java +++ b/src/ServidorVotacao.java @@ -1,27 +1,30 @@ import java.rmi.Naming; -import java.rmi.Remote; import java.rmi.registry.LocateRegistry; import java.util.ArrayList; import java.util.List; +import java.util.Scanner; import implementacoes.CandidatoImpl; -import implementacoes.IsEvenImpl; import implementacoes.VotacaoImpl; +import interfaces.ApuracaoInterface; import interfaces.VotacaoInterface; -public class Servidor { + +public class ServidorVotacao { static List candidatos; static VotacaoInterface votacaoImpl; - static int porta = 1099; + static int porta_servidor_votacao = 8898; + static int porta_servidor_apuracao = 8899; + static List votos = new ArrayList<>(); public static void main(String[] args) { try { - //IsEvenImpl x = new IsEvenImpl(); - String objName = "rmi://localhost:" + porta + "/server"; - System.out.println("Registrando o objeto no RMIRegistry..."); - LocateRegistry.createRegistry(porta); + + LocateRegistry.createRegistry(porta_servidor_votacao); + + ApuracaoInterface apuracao = (ApuracaoInterface) Naming.lookup("//localhost:" + porta_servidor_apuracao + "/server"); // Lista de Candidatos candidatos = new ArrayList<>(); @@ -35,10 +38,23 @@ public class Servidor { votacaoImpl = new VotacaoImpl(candidatos); - Naming.rebind(objName, votacaoImpl); - //Naming.rebind(objName, x); + Naming.rebind("//localhost:" + porta_servidor_votacao + "/server", votacaoImpl); - System.out.println("Aguardando Clientes na porta " + porta + "!"); + System.out.println("Servidor de votação. Aguardando Clientes na porta " + porta_servidor_votacao + "!"); + + while (true) { + System.out.println("Aperte Enter para ver a apuração"); + Scanner entrada = new Scanner(System.in); + entrada.nextLine(); + + votos.clear(); + + votacaoImpl.getVotos().forEach(x -> { + votos.add(x.getCandidato().getNumero()); + }); + + votacaoImpl.mostrar_apuracao(apuracao.calcularVotos(votos)); + } } catch (Exception e) { e.printStackTrace(); } diff --git a/src/implementacoes/ApuracaoImpl.java b/src/implementacoes/ApuracaoImpl.java new file mode 100644 index 0000000..0f1ca65 --- /dev/null +++ b/src/implementacoes/ApuracaoImpl.java @@ -0,0 +1,30 @@ +package implementacoes; + +import java.rmi.RemoteException; +import java.rmi.server.UnicastRemoteObject; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import interfaces.ApuracaoInterface; + +public class ApuracaoImpl extends UnicastRemoteObject implements ApuracaoInterface { + + private static final long serialVersionUID = 1L; + + public ApuracaoImpl() throws RemoteException { + super(); + } + + private Map quantidade = new HashMap<>(); + + @Override + public Map calcularVotos(List votos) { + votos.forEach(voto -> { + int count = (int) votos.stream().filter(p -> p.equals(voto)).count(); + quantidade.put(voto, ((100 * count) / votos.size())); + }); + + return quantidade; + } +} diff --git a/src/implementacoes/IsEvenImpl.java b/src/implementacoes/IsEvenImpl.java deleted file mode 100644 index ce4f19e..0000000 --- a/src/implementacoes/IsEvenImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package implementacoes; - -import java.rmi.server.UnicastRemoteObject; -import interfaces.IsEvenInterface; -import java.rmi.RemoteException; - -public class IsEvenImpl extends UnicastRemoteObject implements IsEvenInterface { - - public IsEvenImpl() throws RemoteException { - super(); - } - - private static final long serialVersionUID = 1L; - - @Override - public boolean is_even(int x) throws RemoteException { - System.out.println("Conectado. Parâmetro enviado é " + x); - if((x & 1) == 0) { - return true; - } else { - return false; - } - } -} \ No newline at end of file diff --git a/src/implementacoes/VotacaoImpl.java b/src/implementacoes/VotacaoImpl.java index b9f513c..872ccd7 100644 --- a/src/implementacoes/VotacaoImpl.java +++ b/src/implementacoes/VotacaoImpl.java @@ -26,6 +26,11 @@ public class VotacaoImpl extends UnicastRemoteObject implements VotacaoInterface public List listarCandidatos() throws RemoteException { return this.candidatos; } + + @Override + public List getVotos(){ + return this.votos; + } @Override public int salvarVoto(int posicao) throws RemoteException { @@ -48,6 +53,16 @@ public class VotacaoImpl extends UnicastRemoteObject implements VotacaoInterface } } + @Override + public void mostrar_apuracao(Map quantidade) { + final String format = "O candidato %d possui %d votos"; + final Set chaves = quantidade.keySet(); + System.out.println("Apuração dos votos"); + for (final Integer chave : chaves) { + System.out.println(String.format(format, chave, quantidade.get(chave))); + } + } + @Override public int buscarCandidato(String numero) throws RemoteException { for(int i = 0; i < this.candidatos.size(); i++) { diff --git a/src/implementacoes/VotoImpl.java b/src/implementacoes/VotoImpl.java index d50024c..94cea87 100644 --- a/src/implementacoes/VotoImpl.java +++ b/src/implementacoes/VotoImpl.java @@ -1,9 +1,12 @@ package implementacoes; +import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; -public class VotoImpl extends UnicastRemoteObject { +import interfaces.VotoInterface; + +public class VotoImpl extends UnicastRemoteObject implements Serializable, VotoInterface { private static final long serialVersionUID = 1L; @@ -16,10 +19,12 @@ public class VotoImpl extends UnicastRemoteObject { this.identificador = identificador; } + @Override public CandidatoImpl getCandidato() { return candidato; } + @Override public String getIdentificador() { return identificador; } diff --git a/src/interfaces/ApuracaoInterface.java b/src/interfaces/ApuracaoInterface.java new file mode 100644 index 0000000..c4d379d --- /dev/null +++ b/src/interfaces/ApuracaoInterface.java @@ -0,0 +1,10 @@ +package interfaces; + +import java.rmi.Remote; +import java.rmi.RemoteException; +import java.util.List; +import java.util.Map; + +public interface ApuracaoInterface extends Remote { + public Map calcularVotos(List votos) throws RemoteException; +} diff --git a/src/interfaces/IsEvenInterface.java b/src/interfaces/IsEvenInterface.java deleted file mode 100644 index 3dca6d5..0000000 --- a/src/interfaces/IsEvenInterface.java +++ /dev/null @@ -1,7 +0,0 @@ -package interfaces; -import java.rmi.Remote; -import java.rmi.RemoteException; - -public interface IsEvenInterface extends Remote { - public boolean is_even(int x) throws RemoteException; -} \ No newline at end of file diff --git a/src/interfaces/VotacaoInterface.java b/src/interfaces/VotacaoInterface.java index 56495f6..b2b2716 100644 --- a/src/interfaces/VotacaoInterface.java +++ b/src/interfaces/VotacaoInterface.java @@ -3,12 +3,16 @@ package interfaces; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.List; +import java.util.Map; import implementacoes.CandidatoImpl; +import implementacoes.VotoImpl; public interface VotacaoInterface extends Remote { public List listarCandidatos() throws RemoteException; public int salvarVoto(int posicao) throws RemoteException; public int buscarCandidato(String numero) throws RemoteException; public void apuracao() throws RemoteException; + public List getVotos() throws RemoteException; + public void mostrar_apuracao(Map apuracao) throws RemoteException; } diff --git a/src/interfaces/VotoInterface.java b/src/interfaces/VotoInterface.java new file mode 100644 index 0000000..00e19e8 --- /dev/null +++ b/src/interfaces/VotoInterface.java @@ -0,0 +1,10 @@ +package interfaces; + +import java.rmi.RemoteException; + +import implementacoes.CandidatoImpl; + +public interface VotoInterface { + public CandidatoImpl getCandidato() throws RemoteException;; + public String getIdentificador() throws RemoteException;; +} \ No newline at end of file