Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.MalformedURLException;
- import java.net.Socket;
- import java.rmi.Naming;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.server.UnicastRemoteObject;
- import java.util.Scanner;
- import static java.lang.Thread.sleep;
- public class RMI_Client extends UnicastRemoteObject implements RMI_Client_Interface{
- RMI_Client() throws RemoteException {
- super();
- }
- public static void main(String args[]) {
- System.setProperty("java.rmi.server.hostname", "10.16.2.226");
- int timeOut = 0;
- int maxTimeOut = 5;
- RMI_ServerInterface serverInterface = null;
- RMI_Client c = null;
- String username = "", password = "", choice = "", song = "", artist = "", album = "", new_song = "", old_song = "",
- old_artist = "", new_artist = "", genre = "", new_album_name = "", new_album_description = "", user_to_promote="", review="",
- music_name = "", music_artist = "";
- String message;
- String[] parts;
- String address, filepath_send, filepath_recv;
- int port;
- Scanner scan;
- double rate=0;
- boolean logged_in;
- boolean ask_input = true;
- boolean errorReconnecting = false;
- do {
- try {
- serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry("10.16.2.226", 7000).lookup("benfica");
- //serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry(7000).lookup("benfica");
- System.out.println("Connected");
- timeOut = 0;
- c = new RMI_Client();
- scan = new Scanner(System.in);
- logged_in = false;
- do {
- if (ask_input) {
- choice = scan.nextLine();
- }
- try {
- if (choice.equals("sign up")) {
- if (ask_input) {
- System.out.print("Username: ");
- username = scan.nextLine();
- System.out.print("Password: ");
- password = scan.nextLine();
- }
- message = serverInterface.singUp(username, password);
- System.out.println(message);
- if (message.equals("Sign up succesfully")) {
- serverInterface.subscribe((RMI_Client_Interface) c, username);
- logged_in = true;
- }
- } else if (choice.equals("login")) {
- if (ask_input) {
- System.out.print("Username: ");
- username = scan.nextLine();
- System.out.print("Password: ");
- password = scan.nextLine();
- }
- message = serverInterface.login(username, password);
- System.out.println(message);
- if (message.equals("Logged in")) {
- message = serverInterface.verifyNotifications(username);
- System.out.println(message);
- logged_in = true;
- }
- } else {
- System.out.println("type <sign up> or <login>");
- }
- ask_input = true;
- } catch (Exception re) {
- try {
- serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry("10.16.2.226", 7000).lookup("benfica");
- ask_input = false;
- } catch (RemoteException re2) {
- timeOut++;
- ask_input = false;
- System.out.println("reconnecting...");
- if (timeOut > maxTimeOut) {
- System.out.println("Time out.");
- System.exit(0);
- }
- }
- }
- } while (!logged_in);
- ask_input = true;
- while (true) {
- // subscreve
- serverInterface.subscribe((RMI_Client_Interface) c, username);
- try {
- if (ask_input)
- choice = scan.nextLine();
- switch (choice) {
- case "add music":
- if (ask_input) {
- System.out.print("Music name: ");
- song = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.addMusic(artist, song);
- System.out.println(message);
- ask_input = true;
- break;
- case "change music":
- if (ask_input) {
- System.out.print("Music name: ");
- old_song = scan.nextLine();
- System.out.print("Artist name: ");
- old_artist = scan.nextLine();
- System.out.print("New music name: ");
- new_song = scan.nextLine();
- System.out.print("New Artist name: ");
- new_artist = scan.nextLine();
- }
- message = serverInterface.changeMusic(old_artist, old_song, new_artist, new_song, username);
- System.out.println(message);
- ask_input = true;
- break;
- case "remove music":
- if (ask_input) {
- System.out.print("Music name: ");
- song = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.removeMusic(artist, song, username);
- System.out.println(message);
- ask_input = true;
- break;
- case "add artist":
- if (ask_input) {
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.addArtist(artist);
- System.out.println(message);
- ask_input = true;
- break;
- case "change artist":
- if (ask_input) {
- System.out.print("Artist name: ");
- old_artist = scan.nextLine();
- System.out.print("New artist name: ");
- new_artist = scan.nextLine();
- }
- message = serverInterface.changeArtist(old_artist, new_artist, username);
- System.out.println(message);
- ask_input = true;
- break;
- case "remove artist":
- if (ask_input) {
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.removeArtist(artist, username);
- System.out.println(message);
- ask_input = true;
- break;
- case "add album":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.addAlbum(album, artist);
- System.out.println(message);
- ask_input = true;
- break;
- // change album artist name
- case "change album artist":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Artist name: ");
- old_artist = scan.nextLine();
- System.out.print("New artist name: ");
- new_artist = scan.nextLine();
- }
- message = serverInterface.changeAlbumArtist(album, old_artist, new_artist, username);
- System.out.println(message);
- ask_input = true;
- break;
- // change album album name
- case "change album name":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Artist name: ");
- old_artist = scan.nextLine();
- System.out.print("New album name: ");
- new_album_name = scan.nextLine();
- }
- message = serverInterface.changeAlbumName(album, old_artist, new_album_name, username);
- System.out.println(message);
- ask_input = true;
- break;
- // change album description
- case "change album description":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Artist name: ");
- old_artist = scan.nextLine();
- System.out.print("New description: ");
- new_album_description = scan.nextLine();
- }
- message = serverInterface.changeAlbumDescription(album, old_artist, new_album_description, username);
- System.out.println(message);
- ask_input = true;
- break;
- case "remove album":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.removeAlbum(album, artist, username);
- System.out.println(message);
- ask_input = true;
- break;
- case "remove music from album":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Album artist name: ");
- artist = scan.nextLine();
- System.out.print("Music artist name: ");
- music_artist = scan.nextLine();
- System.out.print("Music name: ");
- music_name = scan.nextLine();
- message = serverInterface.removeAlbumMusic(album, artist, music_name, music_artist);
- System.out.println(message);
- ask_input = true;
- }
- break;
- case "add music to album":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Album artist name: ");
- artist = scan.nextLine();
- System.out.print("Music artist name: ");
- music_artist = scan.nextLine();
- System.out.print("Music name: ");
- music_name = scan.nextLine();
- }
- message = serverInterface.addMusicToAlbum(album, artist, music_name, music_artist);
- System.out.println(message);
- ask_input = true;
- break;
- case "search by artist":
- if (ask_input) {
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.searchMusicbyartist(artist);
- System.out.println(message);
- ask_input = true;
- break;
- case "search by album":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- }
- message = serverInterface.searchMusicbyalbum(album);
- System.out.println(message);
- ask_input = true;
- break;
- case "search by genre":
- if (ask_input) {
- System.out.print("Genre: ");
- genre = scan.nextLine();
- }
- message = serverInterface.searchMusicbygenre(genre);
- System.out.println(message);
- ask_input = true;
- break;
- case "album details":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.albumDetails(album, artist);
- System.out.println(message);
- ask_input = true;
- break;
- case "artist details":
- if (ask_input) {
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.artistDetails(artist);
- System.out.println(message);
- ask_input = true;
- break;
- case "promote user":
- if (ask_input) {
- System.out.print("Username to promote: ");
- user_to_promote = scan.nextLine();
- }
- message = serverInterface.giveEditorToClient(user_to_promote, username);
- System.out.println(message);
- ask_input = true;
- break;
- case "write review":
- if (ask_input) {
- System.out.print("Album name: ");
- album = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- //username = scan.nextLine();
- System.out.print("Review: ");
- review = scan.nextLine();
- System.out.print("Rate: ");
- rate = scan.nextDouble();
- }
- message = serverInterface.writeReview(album, artist, username, review, rate);
- System.out.println(message);
- ask_input = true;
- break;
- case "upload music":
- if (ask_input) {
- System.out.print("Music name: ");
- music_name = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.uploadMusic(artist, music_name, username);
- System.out.println(message);
- parts = message.split("[|;]");
- if(!message.equals("Music not found")){
- address = parts[9];
- port = Integer.parseInt(parts[11]);
- filepath_send = parts[13];
- filepath_recv = parts[15];
- try {
- Socket socket = new Socket(address, port);
- File file = new File(filepath_send);
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- OutputStream os = socket.getOutputStream();
- byte b[] = new byte[(int) file.length()];
- bis.read(b, 0, b.length);
- os.write(b, 0, b.length);
- os.flush();
- socket.close();
- System.out.println("File sent succesfully!");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- ask_input = true;
- break;
- case "permission to upload":
- if (ask_input) {
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- System.out.print("Music name: ");
- music_name = scan.nextLine();
- System.out.print("New user: ");
- }
- String new_user = scan.nextLine();
- message = serverInterface.givePermissiontoUpload(username, artist,music_name,new_user);
- System.out.println(message);
- ask_input = true;
- break;
- case "download music":
- message = serverInterface.getAllPermitedUploads(username);
- System.out.println(message);
- if (!message.equals("No musics to download")) {
- if (ask_input) {
- System.out.print("Music name: ");
- music_name = scan.nextLine();
- System.out.print("Artist name: ");
- artist = scan.nextLine();
- }
- message = serverInterface.getUploadsWithPermissions(username, music_name, artist);
- System.out.println(message);
- if (!message.equals("You don't have permissions to download")) {
- message = serverInterface.downloadMusic(artist, music_name, username);
- System.out.println(message);
- if (!message.equals("music not found for download")) {
- parts = message.split("[|;]");
- address = parts[3];
- port = Integer.parseInt(parts[5]);
- filepath_send = parts[7];
- filepath_recv = parts[9];
- Socket socket = new Socket(address, port);
- byte[] b = new byte[10000];
- File file = new File(filepath_recv);
- FileOutputStream fos = new FileOutputStream(file);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- InputStream is = socket.getInputStream();
- int bytesRead;
- while ((bytesRead = is.read(b)) > 0)
- bos.write(b, 0, bytesRead);
- bos.flush();
- socket.close();
- System.out.println("File downloaded successfully!");
- }
- }
- }
- ask_input = true;
- break;
- default:
- System.out.println("commands: add music, change music, remove music, add artist, change artist, remove artist\n" +
- "add album, change album, remove album, search by artist, search by album, search by genre\n" +
- "artist details, write review, upload music, download music\n");
- }
- } catch (Exception e) {
- ask_input = false;
- System.out.println("??????");
- e.printStackTrace();
- try {
- serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry("10.16.2.226", 7000).lookup("benfica");
- } catch (RemoteException re2) {
- System.out.println("reconnecting...");
- }
- }
- }
- } catch (Exception e) {
- // tentar dar reconnect
- // time out 5 vezes
- timeOut++;
- System.out.println("Attempt "+timeOut+" to connect to server...");
- try {
- sleep(500);
- } catch (InterruptedException e1) {
- e1.printStackTrace();
- }
- }
- } while(timeOut < maxTimeOut);
- System.out.println("Time Out.");
- System.exit(0);
- }
- public void print_on_client(String s) {
- System.out.println(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement