Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package calculator;
- import java.net.MalformedURLException;
- import java.rmi.Naming;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.*;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.server.*;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- import com.sun.org.apache.xpath.internal.SourceTree;
- import java.net.*;
- public class ClientRmi extends UnicastRemoteObject implements ClientRmiInt {
- private static ArrayList<Pessoa> bdPessoas;
- private String nome = new String();// este nome vai servir para identificar o cliente que deve receber as respostas
- // aos pedidos feitos (e recebido como um id de parametro)
- private int log = 0;
- public static Config config = new Config("configServer.cfg");
- private static String nomeServer = config.getNomeRMI();
- public ClientRmi() throws RemoteException {
- super();
- }
- public void print_on_client(String msg, String nome) throws RemoteException {
- if (this.nome.equals(nome) && this.log == 1
- || msg.equals("type / status ; logged / on ; msg / Welcome to DropMusic")
- || msg.equals("Registado com sucesso") || msg.equals("Nao foi possivel registar")
- || msg.equals("Utilizador nao registado!") || msg.equals("logout feito"))
- System.out.println("> " + msg);
- if (msg.equals("type / status ; logged / on ; msg / Welcome to DropMusic"))
- this.log = 1;
- if (msg.equals("logout feito"))
- this.log = 0;
- }
- public String getNome() throws RemoteException {
- return this.nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- // funcao para comunicar os downloads e uploads ao multicast
- public static void uploadDownloadTCP() {
- Socket socket;
- int bytesRead;
- int port = 0;
- DataOutputStream outToServer;
- BufferedReader inFromServer = null;
- Scanner keyboardScanner = new Scanner(System.in);
- System.out.println("Qual a porta:");
- while (true) {
- try {
- port = keyboardScanner.nextInt();
- break;
- } catch (java.util.InputMismatchException e) {
- System.out.println("Opcao Errada");
- keyboardScanner.nextLine();
- continue;
- }
- }
- System.out.println("Qual o host:");
- String host = keyboardScanner.nextLine();
- host = keyboardScanner.nextLine();
- try {
- // connect to the specified address:port
- socket = new Socket(host, port);
- System.out.println("SOCKET CRIADO \n\n");
- DataInputStream in = new DataInputStream(socket.getInputStream());
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- // para introduzir a opcao pretendida
- String texto = "";
- InputStreamReader input = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(input);
- System.out.println("Introduza opcao: (download ou upload)");
- // le string do teclado
- try {
- texto = reader.readLine();
- } catch (Exception e) {
- }
- if (texto.equals("upload")) {
- // WRITE INTO THE SOCKET
- out.writeUTF(texto);// escreve a opcao para ser lida do outro lado
- texto = "";
- input = new InputStreamReader(System.in);
- reader = new BufferedReader(input);
- System.out.println("Introduza nome da musica:");
- texto = reader.readLine();// le o nome do ficheiro a enviar
- out.writeUTF(texto);// escreve o nome do ficheiro do outro lado para sair correto no output
- // sendfile
- File myFile = new File(texto);
- byte[] mybytearray = new byte[(int) myFile.length()];
- FileInputStream fis = new FileInputStream(myFile);
- BufferedInputStream bis = new BufferedInputStream(fis);
- bis.read(mybytearray, 0, mybytearray.length);
- OutputStream os = socket.getOutputStream();
- os.write(mybytearray, 0, mybytearray.length);
- os.flush();
- System.out.println("Enviado!");
- socket.close();
- } else if (texto.equals("download")) {// aguarda pela chegada do ficheiro
- try {
- out.writeUTF(texto);// escreve a opcao para o outro lado saber
- System.out.println("Nome do ficheiro que quer fazer download: ");
- texto = reader.readLine();
- out.writeUTF(texto);// escreve o nome do ficheiro a fazer download para o outro lado
- InputStream in2 = socket.getInputStream();
- // escreve para o disco
- OutputStream output = new FileOutputStream(texto);
- byte[] buffer = new byte[1024];
- while ((bytesRead = in2.read(buffer)) != -1) {
- output.write(buffer, 0, bytesRead);
- }
- // fecha o fileoutputstream
- output.close();
- System.out.println("Recebido!");
- } catch (IOException e) {
- System.out.println("Exception " + e);
- }
- }
- } catch (EOFException e) {
- System.out.println("EOF:" + e.getMessage());
- } catch (IOException e) {
- System.out.println("IO:" + e.getMessage());
- }
- }
- private static void printMenu() {
- System.out.println("Welcome to dropMusic");
- System.out.println("1- Login");
- System.out.println("2- Sign up");
- System.out.println("3- Adicionar musico");
- System.out.println("4- Remover musico");
- System.out.println("5- Pesquisar musico");
- System.out.println("6- Adicionar musica");
- System.out.println("7- Remover musica");
- System.out.println("8- Procurar musica");
- System.out.println("9- Adicionar album");
- System.out.println("10- Procurar album");
- System.out.println("11- Alterar descrição de album");
- System.out.println("12- Escrever critica a um album");
- System.out.println("13- Download de musica");
- System.out.println("14- Tornar utilizador editor");
- System.out.println("15- Cria/Adiciona à Playlist");
- System.out.println("16- Remover playlist");
- System.out.println("17- Associar musica a um album");
- System.out.println("18- Logout");
- }
- private static String menu() {
- int opt = 0;
- String msg = "";
- String name, mail, user, pass, historia, musicas, critica;
- String editora, letra, musico, descricao, genero, pont;
- String dia, mes, ano, minutos, hora;
- System.out.println();
- printMenu();
- Scanner scan = new Scanner(System.in);
- try {
- opt = scan.nextInt();
- } catch (Exception e) {
- }
- switch (opt) {
- case 1:
- scan.nextLine();
- System.out.println("Username: ");
- user = scan.nextLine();
- System.out.println("Password: ");
- pass = scan.nextLine();
- msg = "type / login ; username / " + user + " ; password / " + pass;
- break;
- case 2:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- System.out.println("Username: ");
- user = scan.nextLine();
- System.out.println("Password: ");
- pass = scan.nextLine();
- System.out.println("Email: ");
- mail = scan.nextLine();
- msg = "type / sign up ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / " + pass
- + " ; mail / " + mail;
- break;
- case 3:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- System.out.println("Username: ");
- user = scan.nextLine();
- System.out.println("Password: ");
- pass = scan.nextLine();
- System.out.println("Email: ");
- mail = scan.nextLine();
- msg = "type / add artist ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / "
- + pass + " ; mail / " + mail;
- break;
- case 4:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- msg = "type / remove_artist ; item_count / 3 ; name / " + name;
- break;
- case 5:
- scan.nextLine();
- System.out.println("Name: ");
- name = scan.nextLine();
- msg = "type / search_artist ; item_count / 1 ; name / " + name;
- break;
- case 6:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- System.out.println("Historia: ");
- historia = scan.nextLine();
- System.out.println("Editora: ");
- editora = scan.nextLine();
- System.out.println("Letra: ");
- letra = scan.nextLine();
- System.out.println("Musico: ");
- musico = scan.nextLine();
- System.out.println("Género: ");
- genero = scan.nextLine();
- msg = "type / add_music ; item_count / 5 ; name / " + name + " ; historia / " + historia + " ; editora / "
- + editora + " ; letra / " + letra + " ; musico / " + musico + " ; genero / " + genero;
- break;
- case 7:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- msg = "type / remove_music ; item_count / 1 ; name / " + name;
- break;
- case 8:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- msg = "type / search_music ; item_count / 1 ; name / " + name;
- break;
- case 9:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- System.out.println("Descrição: ");
- descricao = scan.nextLine();
- System.out.println("Género: ");
- genero = scan.nextLine();
- System.out.println("Data de lançamento");
- System.out.println("Dia:");
- dia = scan.nextLine();
- System.out.println("Mes:");
- mes = scan.nextLine();
- System.out.println("Ano");
- ano = scan.nextLine();
- System.out.println("Hora");
- hora = scan.nextLine();
- System.out.println("Minutos");
- minutos = scan.nextLine();
- msg = "type / add_album ; item_count / 8 ; name / " + name + " ; descricao / " + descricao + " ; genero / "
- + genero + " ; dia / " + dia + " ; mes / " + mes + " ; ano / " + ano + " ; hora / " + hora
- + " ; minuto / " + minutos;
- break;
- case 10:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- msg = "type / search_album ; item_count / 1 ; name / " + name;
- break;
- case 11:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- System.out.println("Nova descrição: ");
- descricao = scan.nextLine();
- msg = "type / change_desc_album ; item_count / 2 ; name / " + name + " ; descicao / " + descricao;
- break;
- case 12:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- System.out.println("Critica: ");
- critica = scan.nextLine();
- System.out.println("Pontuação: ");
- pont = scan.nextLine();
- msg = "type / write_crit ; item_count / 3 ; name / " + name + " ; critica / " + critica + " ; pontuacao / "
- + pont;
- break;
- case 13:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- msg = "type / download ; item_count / 1 ; name / " + name;
- break;
- case 14:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- msg = "type / download ; item_count / 1 ; username / " + name;
- break;
- case 15:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- System.out.println("Nome da musica: ");
- musicas = scan.nextLine();
- System.out.println("Data de criação");
- System.out.println("Dia:");
- dia = scan.nextLine();
- System.out.println("Mes:");
- mes = scan.nextLine();
- System.out.println("Ano");
- ano = scan.nextLine();
- System.out.println("Hora");
- hora = scan.nextLine();
- System.out.println("Minutos");
- minutos = scan.nextLine();
- msg = "type / add_playlist ; item_count / 7 ; name / " + name + " ; musica / " + musicas + " ; dia / " + dia
- + " ; mes / " + mes + " ; ano / " + ano + " ; hora / " + hora + " ; minuto / " + minutos;
- break;
- case 16:
- scan.nextLine();
- System.out.println("Nome: ");
- name = scan.nextLine();
- msg = "type / remove_playlist ; item_count / 1 ; name / " + name;
- break;
- case 17:
- scan.nextLine();
- System.out.println("Nome da muisca: ");
- musicas = scan.nextLine();
- System.out.println("Nome do album: ");
- name = scan.nextLine();
- msg = "type / remove_playlist ; item_count / 2 ; musica / " + musicas + " ; album / " + name;
- break;
- case 18:
- scan.nextLine();
- System.out.println("Username: ");
- name = scan.nextLine();
- msg = "type / logout ; username / " + name;
- break;
- default:
- System.out.println("Invalid option");
- break;
- }
- System.out.println(msg);
- return msg;
- }
- public static void main(String[] args)
- throws MalformedURLException, RemoteException, NotBoundException, IOException {
- String line = "";
- System.getProperties().put("java.security.policy", "out/production/projeto2/policy.all");
- ServerRmiInt si = null;
- ClientRmi c = new ClientRmi();
- c.setNome(args[0]);
- si = (ServerRmiInt) Naming.lookup(nomeServer);
- si.subscribe(args[0], c);
- System.out.println("Request sent to Server");
- InputStreamReader input = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(input);
- // printMenu();
- // entra em ciclo para comunnicacao com o server rmi
- while (true) {
- try {
- line = menu();
- si.sendMulti(line, args[0], c);
- if (line.equals("type / share")) {// comando share para partilhar up/download
- uploadDownloadTCP();
- }
- } catch (java.rmi.ConnectException e) {// trata da falha no servidor principal para passar ao backup
- si = (ServerRmiInt) Naming.lookup(nomeServer);
- si.subscribe(args[0], c);
- System.out.println("aqui");
- si.sendMulti(line, args[0], c);
- } catch (RemoteException re) {
- System.out.println("Exception remote in main " + re);
- } catch (IOException e) {
- System.out.println("Exception in main " + e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement