Advertisement
Guest User

Untitled

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