
Instruções de Compilação em Linux
Em seu terminal, vá para local/da/pasta/ e execute javac -d build/classes src/rmi/*.java
para
compilar as classes Java. Para obter a stub do ServiceImple, vá para local/da/pasta/build/classes/ e execute rmic rmi.ServiceImple
. Quando quiser
executar o servidor, basta ir para local/da/pasta/build/classes/ e executar java rmi.Server
. O cliente encontra-se neste mesmo local, sendo necessário
executar java rmi.Client
para iniciá-lo.
Service.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package rmi; import java.rmi.Remote; import java.rmi.RemoteException; import java.util.List; public interface Service extends Remote { //Type 1 functions: //Returns vector with elements powered to the pot-th: public float[] power(float[] vetor, int vSize, int pot) throws RemoteException; //Return log in parameter base of all elements in vector: public float[] log(float[] vetor, int vSize, int base) throws RemoteException; //Adds an offset to all elements in vector: public float[] addOffset(float[] vetor, int vSize, int offset) throws RemoteException; //Type 2 functions: //Function that return arithmetic mean of vector: public Double mean(float[] vetor, int vSize) throws RemoteException; //Return how many elements are bigger than pivot: public int biggerThan(float[] vetor, int vSize, double pivot) throws RemoteException; //Return closest element in value to pivot: public Double closestTo(float[] vetor, int vSize, double pivot) throws RemoteException; } |
ServiceImple.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package rmi; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.List; public class ServiceImple extends UnicastRemoteObject implements Service{ private static final long serialVersionUID = 1L; protected ServiceImple() throws RemoteException { super(); } @Override public float[] power(float[] vetor, int vSize, int pot) throws RemoteException { for (int i=0; i<vSize; i=i+1){ vetor[i] = (float) Math.pow(vetor[i], pot); } return vetor; } @Override public float[] log(float[] vetor, int vSize, int base) throws RemoteException { for (int i=0; i<vSize; i=i+1){ vetor[i] = (float) (Math.log(vetor[i])/Math.log(base)); } return vetor; } @Override public float[] addOffset(float[] vetor, int vSize, int offset) throws RemoteException { for (int i=0; i<vSize; i=i+1){ vetor[i] = vetor[i] + offset; } return vetor; } @Override public Double mean(float[] vetor, int vSize) throws RemoteException { double mediaParcial = 0; for (int i=0; i<vSize; i=i+1){ mediaParcial += vetor[i]; } return mediaParcial/vSize; } @Override public int biggerThan(float[] vetor, int vSize, double pivot) throws RemoteException { int count = 0; for (int i=0; i<vSize; i=i+1){ if (vetor[i] > pivot){ count++; } } return count; } @Override public Double closestTo(float[] vetor, int vSize, double pivot) throws RemoteException { double bestFind = 0; for (int i=0; i<vSize; i=i+1){ if (Math.abs(vetor[i] - pivot) < Math.abs(bestFind-pivot)){ bestFind = vetor[i]; } } return bestFind; } } |
Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package rmi; import java.rmi.Naming; import java.rmi.Remote; import java.rmi.registry.LocateRegistry; public class Server { Server(){ try { System.setProperty("java.rmi.server.hostname", "localhost"); LocateRegistry.createRegistry(1099); Service s = new ServiceImple(); Naming.bind("Servico", (Remote) s); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args){ new Server(); } } |
Client.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | package rmi; import java.rmi.Naming; import java.util.ArrayList; public class Client { private static final int N = 1000000; private static final int K = 1; private static final int iterations = 1; public static void main(String[] args) throws InterruptedException { //Variable to hold current pseudo-random number: final int a = 25173; final int b = 13849; final int m = 3276; int x = m/2; //Create a list of lists: ArrayList<float[]> vetor = new ArrayList<>(); ArrayList<Long> tempos = new ArrayList<>(); for (int i=0; i<K; i++){ vetor.add(new float[N/K]); for (int j=0; j<N/K; j++){ x = (a * x + b) % m; vetor.get(i)[j] = (float) x/m; } } long startTime = System.nanoTime(); long totalTime = 0; for (int count=0; count<iterations; count++){ //Creates the list of threads: ArrayList<ClientThread> threads = new ArrayList<>(); //Starts the threads for (int i=0; i<K; i++){ threads.add(new ClientThread(vetor.get(i), N/K)); threads.get(i).start(); } //Waits for threads to end: for (int i=0; i<K; i++){ threads.get(i).join(); } tempos.add((Long) System.nanoTime() - startTime - totalTime); totalTime = System.nanoTime() - startTime; } long estimatedTime = System.nanoTime() - startTime; estimatedTime /= iterations; System.out.println("Each loop took: "+(float)estimatedTime/1000000000); for (long tempo: tempos){ System.out.println((float)tempo/1000000000); } } } class ClientThread extends Thread{ private float[] vetor; private int vSize; public ClientThread(float[] vetorArg, int initSize){ vetor = vetorArg; vSize = initSize; } @Override public void run(){ try { Service s = (Service) Naming.lookup("rmi://localhost:1099/Servico"); //Calculates power in RMI: //vetor = (float[]) s.power(vetor, vSize, 2); //Calculates log in RMI: //vetor = (float[]) s.log(vetor, vSize, 2); //Adds an offset in RMI: vetor = (float[]) s.addOffset(vetor, vSize, 100); //Calculates mean in RMI: //s.mean(vetor, vSize); //Counts how many are bigger in RMI: //s.biggerThan(vetor, vSize, 1500); //Finds closes element in RMI: //s.closestTo(vetor, vSize, 1234); } catch (Exception e) { e.printStackTrace(); } } } |