Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.62 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.         printMenu();
  181.         int opt = 0;
  182.         String msg = "";
  183.         String name, mail, user, pass, historia, musicas, critica;
  184.         String editora, letra, musico, descricao, genero, pont;
  185.         String dia, mes, ano, minutos, hora;
  186.         System.out.println();
  187.         System.out.println("option: ");
  188.  
  189.         Scanner scan = new Scanner(System.in);
  190.         try {
  191.             opt = scan.nextInt();
  192.         } catch (Exception e) {
  193.         }
  194.         switch (opt) {
  195.         case 1:
  196.             scan.nextLine();
  197.             System.out.println("Username: ");
  198.             user = scan.nextLine();
  199.             System.out.println("Password: ");
  200.             pass = scan.nextLine();
  201.             msg = "type / login ; username / " + user + " ; password / " + pass;
  202.             break;
  203.  
  204.         case 2:
  205.             scan.nextLine();
  206.             System.out.println("Nome: ");
  207.             name = scan.nextLine();
  208.             System.out.println("Username: ");
  209.             user = scan.nextLine();
  210.             System.out.println("Password: ");
  211.             pass = scan.nextLine();
  212.             System.out.println("Email: ");
  213.             mail = scan.nextLine();
  214.             msg = "type / sign up ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / " + pass
  215.                     + " ; mail / " + mail;
  216.             break;
  217.         case 3:
  218.             scan.nextLine();
  219.             System.out.println("Nome: ");
  220.             name = scan.nextLine();
  221.             System.out.println("Username: ");
  222.             user = scan.nextLine();
  223.             System.out.println("Password: ");
  224.             pass = scan.nextLine();
  225.             System.out.println("Email: ");
  226.             mail = scan.nextLine();
  227.             msg = "type / add artist ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / "
  228.                     + pass + " ; mail / " + mail;
  229.  
  230.             break;
  231.  
  232.         case 4:
  233.             scan.nextLine();
  234.             System.out.println("Nome: ");
  235.             name = scan.nextLine();
  236.             msg = "type / remove_artist ; item_count / 3 ; name / " + name;
  237.  
  238.             break;
  239.         case 5:
  240.             scan.nextLine();
  241.             System.out.println("Username: ");
  242.             name = scan.nextLine();
  243.             msg = "type / search_artist ; item_count / 1 ; name / " + name;
  244.  
  245.             break;
  246.         case 6:
  247.             scan.nextLine();
  248.             System.out.println("Nome: ");
  249.             name = scan.nextLine();
  250.             System.out.println("Historia: ");
  251.             historia = scan.nextLine();
  252.             System.out.println("Editora: ");
  253.             editora = scan.nextLine();
  254.             System.out.println("Letra: ");
  255.             letra = scan.nextLine();
  256.             System.out.println("Musico: ");
  257.             musico = scan.nextLine();
  258.             System.out.println("Género: ");
  259.             genero = scan.nextLine();
  260.             msg = "type / add_music ; item_count / 5 ; name / " + name + " ; historia / " + historia + " ; editora / "
  261.                     + editora + " ; letra / " + letra + " ; musico / " + musico+ " ; genero / " + genero;
  262.  
  263.             break;
  264.  
  265.         case 7:
  266.             scan.nextLine();
  267.             System.out.println("Nome: ");
  268.             name = scan.nextLine();
  269.             msg = "type / remove_music ; item_count / 1 ; name / " + name;
  270.  
  271.             break;
  272.  
  273.         case 8:
  274.             scan.nextLine();
  275.             System.out.println("Nome: ");
  276.             name = scan.nextLine();
  277.             msg = "type / search_music ; item_count / 1 ; name / " + name;
  278.  
  279.             break;
  280.  
  281.         case 9:
  282.             scan.nextLine();
  283.             System.out.println("Nome: ");
  284.             name = scan.nextLine();
  285.             System.out.println("Descrição: ");
  286.             descricao = scan.nextLine();
  287.             System.out.println("Género: ");
  288.             genero = scan.nextLine();
  289.             System.out.println("Data de lançamento");
  290.             System.out.println("Dia:");
  291.             dia=scan.nextLine();
  292.             System.out.println("Mes:");
  293.             mes=scan.nextLine();
  294.             System.out.println("Ano");
  295.             ano=scan.nextLine();
  296.             System.out.println("Hora");
  297.             hora=scan.nextLine();
  298.             System.out.println("Minutos");
  299.             minutos=scan.nextLine();
  300.             msg = "type / add_album ; item_count / 8 ; name / " + name +" ; descricao / "
  301.                     + descricao +" ; genero / " + genero + " ; dia / "+dia+" ; mes / "+mes+" ; ano / "+ano+" ; hora / "+hora+" ; minuto / "+minutos;
  302.  
  303.             break;
  304.  
  305.         case 10:
  306.             scan.nextLine();
  307.             System.out.println("Nome: ");
  308.             name = scan.nextLine();
  309.             msg = "type / search_album ; item_count / 1 ; name / " + name;
  310.  
  311.             break;
  312.  
  313.         case 11:
  314.             scan.nextLine();
  315.             System.out.println("Nome: ");
  316.             name = scan.nextLine();
  317.             System.out.println("Nova descrição: ");
  318.             descricao = scan.nextLine();
  319.             msg = "type / change_desc_album ; item_count / 2 ; name / " + name + " ; descicao / " + descricao;
  320.  
  321.             break;
  322.  
  323.         case 12:
  324.             scan.nextLine();
  325.             System.out.println("Nome: ");
  326.             name = scan.nextLine();
  327.             System.out.println("Critica: ");
  328.             critica = scan.nextLine();
  329.             System.out.println("Pontuação: ");
  330.             pont = scan.nextLine();
  331.             msg = "type / write_crit ; item_count / 3 ; name / " + name + " ; critica / " + critica
  332.                     + " ; pontuacao / " + pont;
  333.  
  334.             break;
  335.  
  336.         case 13:
  337.             scan.nextLine();
  338.             System.out.println("Nome: ");
  339.             name = scan.nextLine();
  340.             msg = "type / download ; item_count / 1 ; name / " + name;
  341.  
  342.             break;
  343.  
  344.         case 14:
  345.             scan.nextLine();
  346.             System.out.println("Nome: ");
  347.             name = scan.nextLine();
  348.             msg = "type / download ; item_count / 1 ; username / " + name;
  349.  
  350.             break;
  351.  
  352.         case 15:
  353.             scan.nextLine();
  354.             System.out.println("Username: ");
  355.             name = scan.nextLine();
  356.             msg = "type / logout ; item_count / 1 ; username / " + name;
  357.  
  358.             break;
  359.         default:
  360.             System.out.println("Invalid option");
  361.             break;
  362.  
  363.         }
  364.         // System.out.println(msg);
  365.  
  366.         return msg;
  367.  
  368.     }
  369.  
  370.     public static void main(String[] args)
  371.             throws MalformedURLException, RemoteException, NotBoundException, IOException {
  372.  
  373.         String line = menu();
  374.  
  375.         System.getProperties().put("java.security.policy", "out/production/projeto2/policy.all");
  376.  
  377.         ServerRmiInt si = null;
  378.  
  379.         ClientRmi c = new ClientRmi();
  380.  
  381.         c.setNome(args[0]);
  382.  
  383.         si = (ServerRmiInt) Naming.lookup(nomeServer);
  384.         si.subscribe(args[0], c);
  385.  
  386.         System.out.println("Request sent to Server");
  387.  
  388.         InputStreamReader input = new InputStreamReader(System.in);
  389.         BufferedReader reader = new BufferedReader(input);
  390.         // printMenu();
  391.         // entra em ciclo para comunnicacao com o server rmi
  392.         while (true) {
  393.             try {
  394.  
  395.                 line = menu();
  396.                 si.sendMulti(line, args[0], c);
  397.  
  398.                 if (line.equals("type / share")) {// comando share para partilhar up/download
  399.                     uploadDownloadTCP();
  400.                 }
  401.             } catch (java.rmi.ConnectException e) {// trata da falha no servidor principal para passar ao backup
  402.                 si = (ServerRmiInt) Naming.lookup(nomeServer);
  403.                 si.subscribe(args[0], c);
  404.                 si.sendMulti(line, args[0], c);
  405.             } catch (RemoteException re) {
  406.                 System.out.println("Exception remote in main " + re);
  407.             } catch (IOException e) {
  408.                 System.out.println("Exception in main " + e);
  409.             }
  410.         }
  411.     }
  412.  
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement