Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javasrc;
- import java.io.IOException;
- import java.rmi.ConnectException;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.server.UnicastRemoteObject;
- import java.util.HashMap;
- import java.util.Scanner;
- public class RMIClient extends UnicastRemoteObject implements RMIInterfaceClient{
- private static Scanner sc = new Scanner(System.in);
- private static RMIInterfaceServer server;
- private static RMIClient c;
- private static String username = null;
- public static int portaDownload;
- public RMIClient() throws RemoteException {
- super();
- }
- //##### MENUS #####
- public static void inicio() throws IOException { //Dar clear da consola
- System.out.println("\n####### BEM VINDO #######\n\n" +
- "1- Registar\n" +
- "2- Login\n");
- int x = sc.nextInt();
- sc.nextLine(); //clear buffer
- if (x==1 || x==2) {
- System.out.print("\nUsername: ");
- String a = sc.nextLine();
- System.out.print("Password: ");
- String b = sc.nextLine();
- if (containForbiddenChars(a) || containForbiddenChars(b)) {
- inicio();
- }
- HashMap<String, String> hLogin;
- boolean bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hLogin = server.login(x, a, b);
- break;
- } catch (NotBoundException | ConnectException e ) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hLogin.get("success").compareTo("true")==0) {
- username = a;
- server.addUser(a, c);
- String sN = server.searchForNotifications(a);
- if (sN != null) {
- System.out.println(sN);
- }
- menu();
- }
- else {
- System.out.println("Erro");
- }
- }
- }
- private static void menu() throws IOException {
- int x = 1;
- while (x!=0) {
- System.out.println("0- Exit\n" +
- "1- Procurar Artistas/Álbuns/Músicas\n" +
- "2- Consultar Detalhes\n" +
- "3- Escrever Critica\n" +
- "4- Transferir Música -> Servidor\n" +
- "5- Partilhar Música\n" +
- "6- Transferir Música <- Servidor\n" +
- "EXTRAS ADMISTRADOR:\n" +
- "7- Gerir Artistas Álbuns e Músicas\n" +
- "8- Dar Privilégios");
- x = sc.nextInt();
- sc.nextLine(); //Clear buffer
- if (x==1) {
- menuUm();
- }
- else if (x==2) {
- menuDois();
- }
- else if (x==4) {
- menuQuatro();
- }
- else if (x==3) {
- menuTres();
- }
- else if (x==7) {
- menuSete();
- }
- else if(x==6){
- menuSeis();
- }
- else if (x==8) {
- menuOito();
- }
- }
- }
- private static void menuUm() throws IOException {
- System.out.println("Name: ");
- String name = sc.nextLine();
- if (containForbiddenChars(name)) {
- return;
- }
- HashMap<String, String> hSearch;
- boolean bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hSearch = server.search(name);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hSearch.get("success").compareTo("false")==0) {
- System.out.println("Not found!");
- return;
- }
- int nrArtists = Integer.parseInt(hSearch.get("num_artists"));
- for (int i=0; i<nrArtists; i++) {
- String temp = "artist_" + i + "_name";
- System.out.println(hSearch.get(temp));
- }
- int nrAlbums = Integer.parseInt(hSearch.get("num_albuns"));
- for (int i=0; i<nrAlbums; i++) {
- String temp = "album_" + i + "_name";
- System.out.println(hSearch.get(temp));
- }
- int nrMusics = Integer.parseInt(hSearch.get("num_musics"));
- for (int i=0; i<nrMusics; i++) {
- String temp = "music_" + i + "_name";
- System.out.println(hSearch.get(temp));
- }
- }
- private static void menuDois() throws IOException {
- System.out.println("\n1- Artista\n" +
- "2- Album\n" +
- "3- Musica\n");
- int x = sc.nextInt();
- sc.nextLine();
- System.out.println("Artist name: ");
- String artistName = sc.nextLine();
- if (containForbiddenChars(artistName)) {
- return;
- }
- //Artista
- if (x==1) {
- HashMap<String, String> hArtistDetails;
- boolean bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hArtistDetails = server.artistDetails(artistName);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hArtistDetails.get("success").compareTo("false")==0) {
- return;
- }
- System.out.println("Artist: " + artistName);
- System.out.println("Description: " + hArtistDetails.get("desc"));
- System.out.println("---Albuns---");
- int nrAlbums = Integer.parseInt(hArtistDetails.get("num_albuns"));
- if (nrAlbums == 0) {
- System.out.println("Sem albuns");
- }
- for (int i=0; i<nrAlbums; i++) {
- String temp = "alb_" + i + "_name";
- System.out.println(hArtistDetails.get(temp));
- }
- System.out.println("---Musicas---");
- int nrMusics = Integer.parseInt(hArtistDetails.get("num_mus"));
- if (nrMusics == 0) {
- System.out.println("Sem musicas");
- }
- for (int i=0; i<nrMusics; i++) {
- String temp = "mus_" + i + "_name";
- System.out.println(hArtistDetails.get(temp));
- }
- }
- //Album
- else if (x==2) {
- System.out.println("Album name: ");
- String albumName = sc.nextLine();
- if (containForbiddenChars(albumName)) {
- return;
- }
- HashMap<String, String> hAlbumDetails;
- boolean bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hAlbumDetails = server.albumDetails(artistName, albumName);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hAlbumDetails.get("success").compareTo("false")==0) {
- return;
- }
- System.out.println("Artist name: " + artistName);
- System.out.println("Album name: " + hAlbumDetails.get("album_name"));
- System.out.println("Description: " + hAlbumDetails.get("desc"));
- System.out.println("Rating: " + hAlbumDetails.get("avg"));
- int nrCritics = Integer.parseInt(hAlbumDetails.get("num_crits"));
- System.out.println("---Criticas---");
- if (nrCritics == 0) {
- System.out.println("Sem criticas");
- }
- for (int i=0; i<nrCritics; i++) {
- System.out.println(hAlbumDetails.get("crit_" + i));
- }
- System.out.println("---Musicas---");
- int nrMusics = Integer.parseInt(hAlbumDetails.get("num_mus"));
- if (nrMusics == 0 ) {
- System.out.println("Sem musicas");
- }
- if (nrMusics > 0) {
- for (int i=0; i<nrMusics; i++) {
- System.out.println(hAlbumDetails.get("num_mus" + i) + " Duração: " + hAlbumDetails.get("dur_" + i));
- }
- }
- }
- else {
- System.out.println("Erro");
- }
- }
- private static void menuTres() throws IOException {
- System.out.println("Album name: ");
- String albumName = sc.nextLine();
- if (containForbiddenChars(albumName)){
- return;
- }
- System.out.println("Artist name: ");
- String artistName = sc.nextLine();
- if (containForbiddenChars(artistName)){
- return;
- }
- System.out.println("Pontuação (1-5): ");
- int score = sc.nextInt();
- sc.nextLine(); //Clear buffer
- if (score<1 || score>5) {
- System.out.println("Pontuação 1-5");
- return;
- }
- System.out.println("Critica (300chars max): ");
- String critic = sc.nextLine();
- if (containForbiddenChars(critic)){
- return;
- }
- if (critic.length()>300) {
- System.out.println("Max 300 chars");
- return;
- }
- HashMap<String, String> hWriteCritic;
- boolean bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hWriteCritic = server.writeCritic(username, artistName, albumName, score, critic);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hWriteCritic.get("success").compareTo("true")==0) {
- System.out.println("Critica escrita com sucesso");
- server.showCritics(albumName,artistName);
- }
- else {
- System.out.println("Erro");
- }
- }
- private static void menuQuatro() throws IOException {
- String nrServer = server.nrMulticastServer();
- System.out.println("Nome do ficheiro: ");
- String nomeFile = sc.nextLine();
- new ClienteSV(Integer.parseInt(nrServer),nomeFile);
- }
- private static void menuSeis() throws IOException {
- System.out.println("Song name: ");
- String songName = sc.nextLine();
- server.downloadMusic(songName,Integer.toString(portaDownload));
- }
- private static void menuSete() throws IOException {
- HashMap<String, String> hIsAdmin;
- boolean bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hIsAdmin = server.isAdmin(username);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hIsAdmin.get("check_admin").compareTo("true")!=0) {
- System.out.println("Permissões insuficientes");
- return;
- }
- System.out.println("\n1- Artista\n" +
- "2- Album\n" +
- "3- Musica\n");
- int x = sc.nextInt();
- sc.nextLine(); //Clear buffer
- if (x<1 || x>3) {
- System.out.println("Inválido");
- return;
- }
- System.out.println("1- Add\n" +
- "2- Edit (not avaiable for music)\n" +
- "3- Remove\n");
- int y = sc.nextInt();
- sc.nextLine(); //Clear buffer
- if (y<1 || y>3) {
- System.out.println("Inválido");
- return;
- }
- //ARTISTA
- if (x==1) {
- System.out.println("Artist name: ");
- String artistName = sc.nextLine();
- if (containForbiddenChars(artistName)) {
- return;
- }
- //ADD
- if (y==1) {
- System.out.println("Description: ");
- String desc = sc.nextLine();
- if (containForbiddenChars(desc)) {
- return;
- }
- HashMap<String, String> hAddArtist;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hAddArtist = server.addArtist(artistName, desc);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hAddArtist.get("success").compareTo("true")==0) {
- System.out.println("Artista adicionado com sucesso");
- return;
- }
- }
- //EDIT
- else if (y==2) {
- System.out.println("New name: ");
- String newName = sc.nextLine();
- System.out.println("New desc: ");
- String newDesc = sc.nextLine();
- if (containForbiddenChars(newName) || containForbiddenChars(newDesc)) {
- return;
- }
- HashMap<String, String> hEditArtist;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hEditArtist = server.editArtist(artistName, newName, newDesc);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hEditArtist.get("success").compareTo("true")==0) {
- System.out.println("Artista editado com sucesso");
- return;
- }
- }
- //REMOVE
- else {
- HashMap<String, String> hRemoveArtist;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hRemoveArtist = server.removeArtist(artistName);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hRemoveArtist.get("success").compareTo("true")==0) {
- System.out.println("Artista removido com sucesso");
- return;
- }
- }
- }
- //ALBUM
- else if (x==2) {
- System.out.println("Artist name: ");
- String artistName = sc.nextLine();
- System.out.println("Album name: ");
- String albumName = sc.nextLine();
- if (containForbiddenChars(artistName) || containForbiddenChars(albumName)) {
- return;
- }
- if (y==1 || y==2) {
- System.out.println("Description: ");
- String desc = sc.nextLine();
- if (containForbiddenChars(desc)) {
- return;
- }
- //ADD
- if (y==1) {
- HashMap<String, String> hAddAlbum;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hAddAlbum = server.addAlbum(username, artistName, albumName, desc);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hAddAlbum.get("success").compareTo("true")==0) {
- System.out.println("Album adicionado com sucesso");
- return;
- }
- }
- //EDIT
- else {
- System.out.println("New name: ");
- String newName = sc.nextLine();
- if (containForbiddenChars(newName)) {
- return;
- }
- HashMap<String, String> hEditAlbum;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hEditAlbum = server.editAlbum(username, artistName, albumName, newName, desc);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hEditAlbum.get("success").compareTo("true")==0) {
- System.out.println("Album editado com sucesso");
- int nrEditores = Integer.parseInt(hEditAlbum.get("num_editors"));
- for (int i=0; i<nrEditores; i++) {
- String adminToBeNotified = hEditAlbum.get("editor_" + i);
- if (adminToBeNotified.compareTo(username)!=0) {
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- server.notifyUserPromotion(adminToBeNotified, "ALTERAÇÃO NO ALBUM " + albumName ,"edit_album");
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- return;
- }
- }
- }
- //REMOVE
- else {
- HashMap<String, String> hRemoveAlbum;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hRemoveAlbum = server.removeAlbum(artistName, albumName);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hRemoveAlbum.get("success").compareTo("true")==0) {
- System.out.println("Album removido com sucesso");
- return;
- }
- }
- }
- //MUSICA
- else {
- if (y==2) {
- System.out.println("Erro!");
- return;
- }
- System.out.println("Artist name: ");
- String artistName = sc.nextLine();
- System.out.println("Album name: ");
- String albumName = sc.nextLine();
- System.out.println("Music name: ");
- String musicName = sc.nextLine();
- if (containForbiddenChars(musicName) || containForbiddenChars(artistName) || containForbiddenChars(albumName)) {
- return;
- }
- //ADD
- if (y==1) {
- System.out.println("Duração: ");
- String duration = sc.nextLine();
- HashMap<String, String> hAddSong;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hAddSong = server.addSong(artistName, albumName, musicName, duration);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hAddSong.get("success").compareTo("true")==0) {
- System.out.println("Musica adicionado com sucesso");
- return;
- }
- }
- //REMOVE
- else if (y==3){
- HashMap<String, String> hRemoveSong;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hRemoveSong = server.removeSong(artistName, albumName, musicName);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hRemoveSong.get("success").compareTo("true")==0) {
- System.out.println("Musica removida com sucesso");
- return;
- }
- }
- }
- System.out.println("Erro");
- }
- private static void menuOito() throws IOException {
- HashMap<String, String> hIsAdmin;
- boolean bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hIsAdmin = server.isAdmin(username);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hIsAdmin.get("check_admin").compareTo("false")==0) {
- System.out.println("Permissões insuficientes");
- return;
- }
- System.out.println("Username");
- String newAdmin = sc.nextLine();
- if (containForbiddenChars(newAdmin)) {
- return;
- }
- HashMap<String, String> hMakeAdmin;
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- hMakeAdmin = server.makeAdmin(newAdmin);
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (hMakeAdmin.get("success").compareTo("true")==0) {
- System.out.println("Admin promovido com sucesso");
- bol = false;
- while (true) {
- try {
- server = (RMIInterfaceServer) LocateRegistry.getRegistry(7000).lookup("serverName");
- server.ping();
- if (bol) {
- Thread.sleep(4000);
- }
- server.notifyUserPromotion(newAdmin, "PARABÉNS FOI PROMOVIDO!","promocao");
- break;
- } catch (NotBoundException | ConnectException e) {
- bol = true;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- else {
- System.out.println("Erro");
- }
- }
- //##### OUTROS #####
- private static boolean containForbiddenChars(String s) {
- if (s.contains("|") || s.contains(";") || s.contains("\n") || s.isEmpty()) {
- System.out.println("\n Input not allowed");
- return true;
- }
- else {
- return false;
- }
- }
- // ------------------
- public static void main(String args[]) throws IOException {
- int min =6500;
- int max = 7000;
- portaDownload = (int) Math.floor(Math.random() * ((max - min) + 1) + min);
- new TCPServerC(portaDownload,"/Users/joaomendes/Desktop/SDProjeto/ficheiros_cliente/").start();
- connectToServer();
- }
- public static void connectToServer() throws IOException {
- c = new RMIClient();
- while (true) {
- inicio();
- }
- }
- //METODOS REMOTOS
- public void displayMessage(String message) throws RemoteException {
- System.out.println(message);
- }
- // METODOS REMOTOS QUE SAO USADOS PARA WEB
- @Override
- public String printOk() throws RemoteException {
- return null;
- }
- @Override
- public String getUsername() throws RemoteException {
- return null;
- }
- @Override
- public String getAlbum() throws RemoteException {
- return null;
- }
- @Override
- public String getArtist() throws RemoteException {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement