Advertisement
Guest User

Untitled

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