Advertisement
Guest User

ClienteRmi

a guest
Nov 25th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.46 KB | None | 0 0
  1.  
  2. //package calculator;
  3. import java.net.MalformedURLException;
  4. import java.rmi.Naming;
  5. import java.rmi.NotBoundException;
  6. import java.rmi.RemoteException;
  7. import java.rmi.*;
  8. import java.rmi.registry.LocateRegistry;
  9. import java.rmi.server.*;
  10. import java.io.*;
  11. import java.util.ArrayList;
  12. import java.util.Scanner;
  13.  
  14. import com.sun.org.apache.xpath.internal.SourceTree;
  15.  
  16. import java.net.*;
  17.  
  18. public class ClientRmi extends UnicastRemoteObject implements ClientRmiInt {
  19.     private static ArrayList<Pessoa> bdPessoas;
  20.     private String nome = new String();// este nome vai servir para identificar o cliente que deve receber as respostas
  21.                                        // aos pedidos feitos (e recebido como um id de parametro)
  22.     private int log = 0;
  23.     public static Config config = new Config("configServer.cfg");
  24.     private static String nomeServer = config.getNomeRMI();
  25.  
  26.     public ClientRmi() throws RemoteException {
  27.         super();
  28.     }
  29.  
  30.     public void print_on_client(String msg, String nome) throws RemoteException {
  31.         if (this.nome.equals(nome) && this.log == 1
  32.                 || msg.equals("type / status ; logged / on ; msg / Welcome to DropMusic")
  33.                 || msg.equals("Registado com sucesso") || msg.equals("Nao foi possivel registar")
  34.                 || msg.equals("Utilizador nao registado!") || msg.equals("logout feito"))
  35.             System.out.println("> " + msg);
  36.         if (msg.equals("type / status ; logged / on ; msg / Welcome to DropMusic"))
  37.             this.log = 1;
  38.         if (msg.equals("logout feito"))
  39.             this.log = 0;
  40.     }
  41.  
  42.     public String getNome() throws RemoteException {
  43.         return this.nome;
  44.     }
  45.  
  46.     public void setNome(String nome) {
  47.         this.nome = nome;
  48.     }
  49.  
  50.     // funcao para comunicar os downloads e uploads ao multicast
  51.     public static void uploadDownloadTCP() {
  52.         Socket socket;
  53.         int bytesRead;
  54.         int port = 0;
  55.         DataOutputStream outToServer;
  56.         BufferedReader inFromServer = null;
  57.         Scanner keyboardScanner = new Scanner(System.in);
  58.         System.out.println("Qual a porta:");
  59.         while (true) {
  60.             try {
  61.                 port = keyboardScanner.nextInt();
  62.                 break;
  63.             } catch (java.util.InputMismatchException e) {
  64.                 System.out.println("Opcao Errada");
  65.                 keyboardScanner.nextLine();
  66.                 continue;
  67.             }
  68.         }
  69.         System.out.println("Qual o host:");
  70.         String host = keyboardScanner.nextLine();
  71.         host = keyboardScanner.nextLine();
  72.         try {
  73.             // connect to the specified address:port
  74.             socket = new Socket(host, port);
  75.             System.out.println("SOCKET CRIADO \n\n");
  76.  
  77.             DataInputStream in = new DataInputStream(socket.getInputStream());
  78.             DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  79.  
  80.             // para introduzir a opcao pretendida
  81.             String texto = "";
  82.             InputStreamReader input = new InputStreamReader(System.in);
  83.             BufferedReader reader = new BufferedReader(input);
  84.             System.out.println("Introduza opcao: (download ou upload)");
  85.  
  86.             // le string do teclado
  87.             try {
  88.                 texto = reader.readLine();
  89.             } catch (Exception e) {
  90.             }
  91.  
  92.             if (texto.equals("upload")) {
  93.  
  94.                 // WRITE INTO THE SOCKET
  95.                 out.writeUTF(texto);// escreve a opcao para ser lida do outro lado
  96.  
  97.                 texto = "";
  98.                 input = new InputStreamReader(System.in);
  99.                 reader = new BufferedReader(input);
  100.                 System.out.println("Introduza nome da musica:");
  101.  
  102.                 texto = reader.readLine();// le o nome do ficheiro a enviar
  103.  
  104.                 out.writeUTF(texto);// escreve o nome do ficheiro do outro lado para sair correto no output
  105.  
  106.                 // sendfile
  107.                 File myFile = new File(texto);
  108.                 byte[] mybytearray = new byte[(int) myFile.length()];
  109.  
  110.                 FileInputStream fis = new FileInputStream(myFile);
  111.                 BufferedInputStream bis = new BufferedInputStream(fis);
  112.                 bis.read(mybytearray, 0, mybytearray.length);
  113.  
  114.                 OutputStream os = socket.getOutputStream();
  115.  
  116.                 os.write(mybytearray, 0, mybytearray.length);
  117.  
  118.                 os.flush();
  119.  
  120.                 System.out.println("Enviado!");
  121.  
  122.                 socket.close();
  123.  
  124.             } else if (texto.equals("download")) {// aguarda pela chegada do ficheiro
  125.                 try {
  126.                     out.writeUTF(texto);// escreve a opcao para o outro lado saber
  127.  
  128.                     System.out.println("Nome do ficheiro que quer fazer download: ");
  129.                     texto = reader.readLine();
  130.  
  131.                     out.writeUTF(texto);// escreve o nome do ficheiro a fazer download para o outro lado
  132.  
  133.                     InputStream in2 = socket.getInputStream();
  134.  
  135.                     // escreve para o disco
  136.                     OutputStream output = new FileOutputStream(texto);
  137.  
  138.                     byte[] buffer = new byte[1024];
  139.                     while ((bytesRead = in2.read(buffer)) != -1) {
  140.                         output.write(buffer, 0, bytesRead);
  141.                     }
  142.                     // fecha o fileoutputstream
  143.                     output.close();
  144.  
  145.                     System.out.println("Recebido!");
  146.                 } catch (IOException e) {
  147.                     System.out.println("Exception " + e);
  148.                 }
  149.             }
  150.  
  151.         } catch (EOFException e) {
  152.             System.out.println("EOF:" + e.getMessage());
  153.         } catch (IOException e) {
  154.             System.out.println("IO:" + e.getMessage());
  155.         }
  156.  
  157.     }
  158.  
  159.     private static void printMenu() {
  160.         System.out.println("Welcome to dropMusic");
  161.         System.out.println("1- Login");
  162.         System.out.println("2- Sign up");
  163.         System.out.println("3- Adicionar musico");
  164.         System.out.println("4- Remover musico");
  165.         System.out.println("5- Pesquisar musico");
  166.         System.out.println("6- Adicionar musica");
  167.         System.out.println("7- Remover musica");
  168.         System.out.println("8- Procurar musica");
  169.         System.out.println("9- Adicionar album");
  170.         System.out.println("10- Procurar album");
  171.         System.out.println("11- Alterar descrição de album");
  172.         System.out.println("12- Escrever critica a um album");
  173.         System.out.println("13- Download de musica");
  174.         System.out.println("14- Tornar utilizador editor");
  175.         System.out.println("15- Logout");
  176.  
  177.     }
  178.  
  179.     private static String menu() {
  180.         int opt = 0;
  181.         String msg = "";
  182.         String name, mail, user, pass, historia, musicas, critica;
  183.         String editora, letra, musico, descricao, genero, pont;
  184.         System.out.println();
  185.         System.out.println("option: ");
  186.  
  187.         Scanner scan = new Scanner(System.in);
  188.         try {
  189.             opt = scan.nextInt();
  190.         } catch (Exception e) {
  191.         }
  192.         switch (opt) {
  193.         case 1:
  194.             scan.nextLine();
  195.             System.out.println("Username: ");
  196.             user = scan.nextLine();
  197.             System.out.println("Password: ");
  198.             pass = scan.nextLine();
  199.             msg = "type / login ; username / " + user + " ; password / " + pass;
  200.             break;
  201.  
  202.         case 2:
  203.             scan.nextLine();
  204.             System.out.println("Nome: ");
  205.             name = scan.nextLine();
  206.             System.out.println("Username: ");
  207.             user = scan.nextLine();
  208.             System.out.println("Password: ");
  209.             pass = scan.nextLine();
  210.             System.out.println("Email: ");
  211.             mail = scan.nextLine();
  212.             msg = "type / sign up ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / " + pass
  213.                     + " ; mail / " + mail;
  214.             break;
  215.         case 3:
  216.             scan.nextLine();
  217.             System.out.println("Nome: ");
  218.             name = scan.nextLine();
  219.             System.out.println("Username: ");
  220.             user = scan.nextLine();
  221.             System.out.println("Password: ");
  222.             pass = scan.nextLine();
  223.             System.out.println("Email: ");
  224.             mail = scan.nextLine();
  225.             msg = "type / add artist ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / "
  226.                     + pass + " ; mail / " + mail;
  227.  
  228.             break;
  229.  
  230.         case 4:
  231.             scan.nextLine();
  232.             System.out.println("Nome: ");
  233.             name = scan.nextLine();
  234.             System.out.println("Username: ");
  235.             user = scan.nextLine();
  236.             System.out.println("Password: ");
  237.             pass = scan.nextLine();
  238.             msg = "type / remove_artist ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / "
  239.                     + pass;
  240.  
  241.             break;
  242.         case 5:
  243.             scan.nextLine();
  244.             System.out.println("Username: ");
  245.             user = scan.nextLine();
  246.             msg = "type / search_artist ; item_count / 1 ; username / " + user;
  247.  
  248.             break;
  249.         case 6:
  250.             scan.nextLine();
  251.             System.out.println("Nome: ");
  252.             name = scan.nextLine();
  253.             System.out.println("Historia: ");
  254.             historia = scan.nextLine();
  255.             System.out.println("Editora: ");
  256.             editora = scan.nextLine();
  257.             System.out.println("Letra: ");
  258.             letra = scan.nextLine();
  259.             System.out.println("Musico: ");
  260.             musico = scan.nextLine();
  261.             msg = "type / add_music ; item_count / 5 ; name / " + name + " ; historia / " + historia + " ; editora / "
  262.                     + editora + " ; letra / " + letra + " ; musico / " + musico;
  263.  
  264.             break;
  265.  
  266.         case 7:
  267.             scan.nextLine();
  268.             System.out.println("Nome: ");
  269.             name = scan.nextLine();
  270.             msg = "type / remove_music ; item_count / 1 ; name / " + name;
  271.  
  272.             break;
  273.  
  274.         case 8:
  275.             scan.nextLine();
  276.             System.out.println("Nome: ");
  277.             name = scan.nextLine();
  278.             msg = "type / search_music ; item_count / 1 ; name / " + name;
  279.  
  280.             break;
  281.  
  282.         case 9:
  283.             scan.nextLine();
  284.             System.out.println("Nome: ");
  285.             name = scan.nextLine();
  286.             System.out.println("Nome das musicas: ");
  287.             musicas = scan.nextLine();
  288.             System.out.println("Descrição: ");
  289.             descricao = scan.nextLine();
  290.             System.out.println("Nome do musico: ");
  291.             musico = scan.nextLine();
  292.             System.out.println("Género: ");
  293.             genero = scan.nextLine();
  294.             msg = "type / add_album ; item_count / 5 ; name / " + name + " ; musicas / " + musicas + " ; descricao / "
  295.                     + descricao + " ; musico / " + musico + " ; genero / " + genero;
  296.  
  297.             break;
  298.  
  299.         case 10:
  300.             scan.nextLine();
  301.             System.out.println("Nome: ");
  302.             name = scan.nextLine();
  303.             msg = "type / search_album ; item_count / 1 ; name / " + name;
  304.  
  305.             break;
  306.  
  307.         case 11:
  308.             scan.nextLine();
  309.             System.out.println("Nome: ");
  310.             name = scan.nextLine();
  311.             System.out.println("Nova descrição: ");
  312.             descricao = scan.nextLine();
  313.             msg = "type / change_desc_album ; item_count / 2 ; name / " + name + " ; descicao / " + descricao;
  314.  
  315.             break;
  316.  
  317.         case 12:
  318.             scan.nextLine();
  319.             System.out.println("Nome: ");
  320.             name = scan.nextLine();
  321.             System.out.println("Critica: ");
  322.             critica = scan.nextLine();
  323.             System.out.println("Pontuação: ");
  324.             pont = scan.nextLine();
  325.             msg = "type / write_critic ; item_count / 3 ; name / " + name + " ; critica / " + critica
  326.                     + " ; pontuacao / " + pont;
  327.  
  328.             break;
  329.  
  330.         case 13:
  331.             scan.nextLine();
  332.             System.out.println("Nome: ");
  333.             name = scan.nextLine();
  334.             msg = "type / download ; item_count / 1 ; name / " + name;
  335.  
  336.             break;
  337.  
  338.         case 14:
  339.             scan.nextLine();
  340.             System.out.println("Nome: ");
  341.             name = scan.nextLine();
  342.             msg = "type / download ; item_count / 1 ; username / " + name;
  343.  
  344.             break;
  345.  
  346.         case 15:
  347.             scan.nextLine();
  348.             System.out.println("Username: ");
  349.             name = scan.nextLine();
  350.             msg = "type / logout ; item_count / 1 ; username / " + name;
  351.  
  352.             break;
  353.         default:
  354.             System.out.println("Invalid option");
  355.             break;
  356.  
  357.         }
  358.         System.out.println(msg);
  359.  
  360.         return msg;
  361.  
  362.     }
  363.  
  364.     public static void main(String[] args)
  365.             throws MalformedURLException, RemoteException, NotBoundException, IOException {
  366.  
  367.         String line = "";
  368.         line = "";
  369.         System.getProperties().put("java.security.policy", "out/production/projeto2/policy.all");
  370.  
  371.         ServerRmiInt si = null;
  372.  
  373.         ClientRmi c = new ClientRmi();
  374.  
  375.         c.setNome(args[0]);
  376.  
  377.         si = (ServerRmiInt) Naming.lookup(nomeServer);
  378.         si.subscribe(args[0], c);
  379.  
  380.         System.out.println("Request sent to Server");
  381.  
  382.         InputStreamReader input = new InputStreamReader(System.in);
  383.         BufferedReader reader = new BufferedReader(input);
  384.         printMenu();
  385.         // entra em ciclo para comunnicacao com o server rmi
  386.         while (true) {
  387.             try {
  388.  
  389.                 line = menu();
  390.                 si.sendMulti(line, args[0], c);
  391.  
  392.                 if (line.equals("type / share")) {// comando share para partilhar up/download
  393.                     uploadDownloadTCP();
  394.                 }
  395.             } catch (java.rmi.ConnectException e) {// trata da falha no servidor principal para passar ao backup
  396.                 si = (ServerRmiInt) Naming.lookup(nomeServer);
  397.                 si.subscribe(args[0], c);
  398.                 System.out.println("aqui");
  399.                 si.sendMulti(line, args[0], c);
  400.             } catch (RemoteException re) {
  401.                 System.out.println("Exception remote in main " + re);
  402.             } catch (IOException e) {
  403.                 System.out.println("Exception in main " + e);
  404.             }
  405.         }
  406.     }
  407.  
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement