Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.rmi.*;
- import java.rmi.server.*;
- import java.net.*;
- import java.io.*;
- import java.util.*;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- public class Client extends UnicastRemoteObject implements CClient {
- private static int PORT = 6060;
- static RmiServer h = null;
- public String username;
- public boolean editor;
- public static String message; //mensagens do servidor
- public Client() throws RemoteException {
- this.username = username;
- this.editor = false;
- }
- public String getUsername() throws RemoteException{
- return username;
- }
- public void setUsername(String user) throws RemoteException{
- this.username = user;
- }
- public boolean getEditor() throws RemoteException{
- return editor;
- }
- public void setEditor(boolean e) throws RemoteException{
- this.editor = e;
- }
- public void print_on_client(String s) throws RemoteException {
- System.out.println(s);
- }
- public void message_client(String s) throws RemoteException{
- message = s;
- }
- public static void downloadSong(String songName, String ip){
- System.out.println("entrei download");
- Socket s = null;
- int FILE_SIZE = 97971310;
- try {
- System.out.println("try download");
- s = new Socket(ip, PORT);//change
- DataOutputStream out = new DataOutputStream(s.getOutputStream());
- // DataInputStream in= new DataInputStream(s.getInputStream());
- String texto = "download;"+songName;
- // WRITE INTO THE SOCKET
- out.writeUTF(texto);
- System.out.println("wrote download");
- int bytesRead;
- int current = 0;
- FileOutputStream fos = null;
- BufferedOutputStream bos = null;
- try {
- System.out.println("second try download");
- byte [] mybytearray = new byte [FILE_SIZE];
- InputStream is = s.getInputStream();
- String userHomeFolder = System.getProperty("user.home")+"\\Desktop";
- fos = new FileOutputStream(userHomeFolder+"\\"+songName+".wav");
- bos = new BufferedOutputStream(fos);
- System.out.println("read download");
- bytesRead = is.read(mybytearray,0,mybytearray.length);
- current = bytesRead;
- System.out.println("do download");
- do {
- bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
- if(bytesRead > 0)
- current += bytesRead;
- } while(bytesRead > 0);
- System.out.println("WRITE download");
- bos.write(mybytearray, 0 , current);
- bos.flush();
- }
- finally {
- if (fos != null) fos.close();
- if (bos != null) bos.close();
- if (s != null) s.close();
- }
- System.out.println("Download feito");
- } catch (EOFException e) {
- System.out.println("EOF:" + e.getMessage());
- } catch (IOException e) {
- System.out.println("IO:" + e.getMessage());
- } finally {
- if (s != null)
- try {
- s.close();
- } catch (IOException e) {
- System.out.println("close:" + e.getMessage());
- }
- }
- }
- public static void uploadSong(String songName, String ip){
- Socket s = null;
- //int FILE_SIZE = 7971310;
- try {
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- OutputStream os = null;
- s = new Socket(ip, PORT);//change
- String userHomeFolder = System.getProperty("user.home")+"\\Desktop";
- File file = new File(userHomeFolder+"\\"+songName+".wav");
- DataOutputStream out = new DataOutputStream(s.getOutputStream());
- String texto = "upload;"+songName;
- out.writeUTF(texto);
- byte [] mybytearray = new byte [(int) file.length()];
- fis = new FileInputStream(file);
- bis = new BufferedInputStream(fis);
- bis.read(mybytearray,0,mybytearray.length);
- os = s.getOutputStream();
- os.write(mybytearray,0,mybytearray.length);
- os.flush();
- System.out.println("Upload completed");
- } catch (EOFException e) {
- System.out.println("EOF:" + e.getMessage());
- } catch (IOException e) {
- System.out.println("IO:" + e.getMessage());
- } finally {
- if (s != null)
- try {
- s.close();
- } catch (IOException e) {
- System.out.println("close:" + e.getMessage());
- }
- }
- }
- public static void menu(Client c, BufferedReader reader, String name, String password, boolean editor){
- try{
- while(true){
- System.out.println("\n1- Search song");
- System.out.println("2- Details of an album");
- System.out.println("3- Details of an artist");
- System.out.println("4- Write a review of an album");
- System.out.println("5- Share with friends uploaded song");
- if (c.getEditor()) {
- System.out.println("6- Manage information");
- System.out.println("7- Make an user a new editor");
- }
- System.out.println("0- Logout");
- System.out.print("> ");
- Scanner sc = new Scanner(System.in);
- String num = sc.next();
- switch(num){
- case "1":
- System.out.println("\n1- Trough artist");
- System.out.println("2- Trough album");
- System.out.print("3- By song name\n> ");
- String aux1 = sc.next();
- String artist11 = "";
- String ip = "";
- String ip2 = "";
- String multicast = "";
- switch(aux1){
- case "1":
- System.out.print("\nType artist's name:\n> ");
- artist11 = reader.readLine();
- String m11 = "type|partial search artist;info|" + artist11 + ";username|" + name;
- h.search_song(m11);
- if(message.equals("yes-artist")){
- message = "";
- System.out.print("\nChoose one song:\n> ");
- String song11 = reader.readLine();
- System.out.println("\n1- Duration");
- System.out.println("2- Composer");
- System.out.println("3- Lyrics");
- System.out.println("4- Upload");
- System.out.print("5- Download\n> ");
- String aux11 = reader.readLine();
- switch(aux11){
- case "1":
- String m111 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|duration;username|" + name;
- h.search_song(m111);
- continue;
- case "2":
- String m112 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|composer;username|" + name;
- h.search_song(m112);
- continue;
- case "3":
- String m113 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|lyrics;username|" + name;
- h.search_song(m113);
- continue;
- case "4":
- String m114 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|uploadServerIP;username|" + name;
- h.search_song(m114);
- multicast = message;
- if (!message.equals("message")){
- String m1141 = multicast + ",type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|serverIP;username|" + name;
- h.search_song(m1141);
- ip = message;
- ip2 = message.split("/")[1];
- uploadSong(song11, ip2);
- }
- continue;
- case "5":
- String m115 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|downloadServerIP;username|" + name;
- h.search_song(m115);
- multicast = message;
- if (!message.equals("message")){
- String m1151 = multicast + ",type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|serverIP;username|" + name;
- h.search_song(m1151);
- ip = message;
- ip2 = message.split("/")[1];
- downloadSong(song11, ip2);
- }
- continue;
- default:
- continue;
- }
- }
- else{
- continue;
- }
- case "2":
- System.out.print("\nType album's name:\n> ");
- String album12 = reader.readLine();
- String m12 = "type|partial search album;info|" + album12 + ";username|" + name;
- h.search_song(m12);
- if(message.equals("yes-album")){
- System.out.print("\nChoose one song:\n> ");
- String song12 = reader.readLine();
- System.out.println("\n1- Duration");
- System.out.println("2- Composer");
- System.out.println("3- Lyrics");
- System.out.println("4- Upload");
- System.out.print("5- Download\n> ");
- String aux12 = reader.readLine();
- switch(aux12){
- case "1":
- String m121 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|duration;username|" + name;
- h.search_song(m121);
- continue;
- case "2":
- String m122 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|composer;username|" + name;
- h.search_song(m122);
- continue;
- case "3":
- String m123 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|lyrics;username|" + name;
- h.search_song(m123);
- continue;
- case "4":
- String m124 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|uploadServerIP;username|" + name;
- h.search_song(m124);
- multicast = message;
- if (!message.equals("message")){
- String m1241 = multicast + ",type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|serverIP;username|" + name;
- h.search_song(m1241);
- ip = message;
- ip2 = message.split("/")[1];
- uploadSong(song12, ip2);
- }
- continue;
- case "5":
- String m125 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|downloadServerIP;username|" + name;
- h.search_song(m125);
- multicast = message;
- if (!message.equals("message")){
- String m1251 = multicast + ",type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|serverIP;username|" + name;
- h.search_song(m1251);
- ip = message;
- ip2 = message.split("/")[1];
- downloadSong(song12, ip2);
- }
- continue;
- default:
- continue;
- }
- }
- else{
- continue;
- }
- case "3":
- System.out.print("\nType song's name:\n> ");
- String song13 = reader.readLine();
- System.out.println("\n1- Duration");
- System.out.println("2- Composer");
- System.out.println("3- Lyrics");
- System.out.println("4- Upload");
- System.out.print("5- Download\n> ");
- String aux13 = reader.readLine();
- switch(aux13){
- case "1":
- String m131 = "type|search music;song name|" + song13 + ";option|duration;username|" + name;
- h.search_song(m131);
- continue;
- case "2":
- String m132 = "type|search music;song name|" + song13 + ";option|composer;username|" + name;
- h.search_song(m132);
- continue;
- case "3":
- String m133 = "type|search music;song name|" + song13 + ";option|lyrics;username|" + name;
- h.search_song(m133);
- continue;
- case "4":
- String m134 = "type|search music;song name|" + song13 + ";option|uploadServerIP;username|" + name;
- h.search_song(m134);
- multicast = message;
- if (!message.equals("message")){
- String m1341 = multicast + ",type|search music;song name|" + song13 + ";option|serverIP;username|" + name;
- h.search_song(m1341);
- ip = message;
- ip2 = message.split("/")[1];
- uploadSong(song13, ip2);
- }
- continue;
- case "5":
- String m135 = "type|search music;song name|" + song13 + ";option|downloadServerIP;username|" + name;
- h.search_song(m135);
- multicast = message;
- if (!message.equals("message")){
- String m1351 = multicast + ",type|search music; song name|" + song13 + ";option|serverIP;username|" + name;
- h.search_song(m1351);
- ip = message;
- ip2 = message.split("/")[1];
- downloadSong(song13, ip2);
- }
- continue;
- default:
- continue;
- }
- default:
- continue;
- }
- case "2":
- System.out.print("\nType artist's name:\n> ");
- String artist2 = reader.readLine();
- System.out.print("\nType album's name:\n> ");
- String album2 = reader.readLine();
- System.out.println("\n1- Description");
- System.out.println("2- List of songs");
- System.out.print("3- Reviews\n> ");
- String aux2 = sc.next();
- switch(aux2){
- case "1":
- h.details_album(name, artist2, album2, "description", "details album");
- continue;
- case "2":
- h.details_album(name, artist2, album2, "list of songs", "details album");
- continue;
- case "3":
- h.details_album(name, artist2, album2, "reviews", "details album");
- continue;
- default:
- continue;
- }
- case "3":
- System.out.print("\nType artist's name:\n> ");
- String artist3 = reader.readLine();
- h.details_artist(name, artist3, "details artist");
- continue;
- case "4":
- System.out.print("\nType album's name:\n> ");
- String album4 = reader.readLine();
- String score4 = null;
- while(true){
- System.out.print("\nType album's score (0 to 10):\n> ");
- score4 = reader.readLine();
- try{
- int score41 = Integer.parseInt(score4);
- if(score41>=0 && score41<=10){
- break;
- }
- else{
- System.out.println("That input is invalid");
- continue;
- }
- }
- catch(NumberFormatException e){
- System.out.println("That input is invalid");
- continue;
- }
- }
- System.out.print("\nWrite review (300 characters):\n> ");
- String review4 = reader.readLine();
- String review42;
- if(review4.length()>300)
- review42 = review4.substring(0,300);
- else
- review42 = review4;
- h.review(name, album4, review42, score4, "review");
- continue;
- case "5":
- System.out.print("\nType album's name:\n> ");
- String album5 = reader.readLine();
- System.out.print("\nType song's name:\n> ");
- String song5 = reader.readLine();
- System.out.println("\nType your friends usernames (write BREAK to end):");
- String user_friends5 = "";
- while(true){
- System.out.print(">");
- String friend5 = reader.readLine();
- if (friend5.equals("BREAK")){
- break;
- }
- else{
- user_friends5 += friend5 + "//";
- continue;
- }
- }
- h.share(song5, album5, user_friends5, name);
- continue;
- case "6":
- if(c.getEditor()){
- System.out.println("\n1- Insert");
- System.out.print("2- Change\n> ");
- String aux8 = sc.next();
- switch(aux8){
- case "1":
- System.out.println("\n1- Artist");
- System.out.println("2- Album");
- System.out.print("3- Song\n> ");
- String aux81 = sc.next();
- switch(aux81){
- case "1":
- System.out.print("\nType artist's name:\n> ");
- String artist811 = reader.readLine();
- System.out.print("\nType artist's description:\n> ");
- String description811 = reader.readLine();
- System.out.print("\nType artist's working period:\n> ");
- String period811 = reader.readLine();
- String m811 = "type|insert artist;artist name|" + artist811 + ";description|" + description811 + ";groupPeriod|" + period811 + ";username|" + name;
- h.manage_info(m811);
- continue;
- case "2":
- System.out.print("\nType artist's name:\n> ");
- String artist812 = reader.readLine();
- System.out.print("\nType album's name:\n> ");
- String album812 = reader.readLine();
- System.out.print("\nType album's genre:\n> ");
- String genre812 = reader.readLine();
- System.out.print("\nType album's description:\n> ");
- String description812 = reader.readLine();
- System.out.print("\nType album's date:\n> ");
- String date812 = reader.readLine();
- String m812 = "type|insert album;artist name|" + artist812 + ";album name|" + album812 + ";genre|" + genre812 + ";description|" + description812 + ";date|" + date812 + ";username|" + name;
- h.manage_info(m812);
- continue;
- case "3":
- System.out.print("\nType artist's name:\n> ");
- String artist813 = reader.readLine();
- System.out.print("\nType album's name:\n> ");
- String album813 = reader.readLine();
- System.out.print("\nType song's name:\n> ");
- String song813 = reader.readLine();
- System.out.print("\nType song's duration:\n> ");
- String duration813 = reader.readLine();
- System.out.print("\nType song's composer:\n> ");
- String composer813 = reader.readLine();
- System.out.print("\nType song's lyrics:\n> ");
- String lyrics813 = reader.readLine();
- String m813 = "type|insert song;artist name|" + artist813 + ";album name|" + album813 + ";song name|" + song813 + ";duration|" + duration813 + ";compositor|" + composer813 + ";lyrics|" + lyrics813 + ";username|" + name;
- h.manage_info(m813);
- continue;
- default:
- continue;
- }
- case "2":
- System.out.println("\n1- Artist");
- System.out.println("2- Album");
- System.out.print("3- Song\n> ");
- String aux82 = sc.next();
- switch(aux82){
- case "1":
- System.out.println("\n1- Artist's name");
- System.out.println("2- Artist's description");
- System.out.print("3- Artist's working period\n> ");
- String aux821 = reader.readLine();
- System.out.print("\nType artist's name:\n> ");
- String artist821 = reader.readLine();
- switch(aux821){
- case "1":
- System.out.print("\nType artist's new name:\n> ");
- String newartist8211 = reader.readLine();
- String m8211 = "type|change artist;artist name|" + artist821 + ";option|name;new information|" + newartist8211 + ";username|" + name;
- h.manage_info(m8211);
- continue;
- case "2":
- System.out.print("\nType artist's new description:\n> ");
- String newd8212 = reader.readLine();
- String m8212 = "type|change artist;artist name|" + artist821 + ";option|description;new information|" + newd8212 + ";username|" + name;
- h.manage_info(m8212);
- continue;
- case "3":
- System.out.print("\nType artist's new working period:\n> ");
- String newp8213 = reader.readLine();
- String m8213 = "type|change artist;artist name|" + artist821 + ";option|groupPeriod;new information|" + newp8213 + ";username|" + name;
- h.manage_info(m8213);
- continue;
- default:
- continue;
- }
- case "2":
- System.out.println("\n1- Album's name");
- System.out.println("2- Album's description");
- System.out.println("3- Album's genre");
- System.out.print("4- Album's date\n> ");
- String aux822 = reader.readLine();
- System.out.print("\nType artist's name:\n> ");
- String artist822 = reader.readLine();
- System.out.print("\nType album's name:\n> ");
- String album822 = reader.readLine();
- switch(aux822){
- case "1":
- System.out.print("\nType album's new name:\n> ");
- String newalbum8221 = reader.readLine();
- String m8221 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|name;new information|" + newalbum8221 + ";username|" + name;
- h.manage_info(m8221);
- continue;
- case "2":
- System.out.print("\nType album's new description:\n> ");
- String newdesc8222 = reader.readLine();
- String m8222 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|description;new information|" + newdesc8222 + ";username|" + name;
- h.manage_info(m8222);
- continue;
- case "3":
- System.out.print("\nType album's new genre:\n> ");
- String newgenre8223 = reader.readLine();
- String m8223 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|genre;new information|" + newgenre8223 + ";username|" + name;
- h.manage_info(m8223);
- continue;
- case "4":
- System.out.print("\nType album's new date:\n> ");
- String newdate8224 = reader.readLine();
- String m8224 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|date;new information|" + newdate8224 + ";username|" + name;
- h.manage_info(m8224);
- continue;
- default:
- continue;
- }
- case "3":
- System.out.println("\n1- Song's name");
- System.out.println("2- Song's duration");
- System.out.println("3- Songs's composer");
- System.out.print("4- Song's lyrics\n> ");
- String aux823 = reader.readLine();
- System.out.print("\nType artist's name:\n> ");
- String artist823 = reader.readLine();
- System.out.print("\nType album's name:\n> ");
- String album823 = reader.readLine();
- System.out.print("\nType song's name:\n> ");
- String song823 = reader.readLine();
- switch(aux823){
- case "1":
- System.out.print("\nType song's new name:\n> ");
- String newsong8231 = reader.readLine();
- String m8231 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|name;new information|" + newsong8231 + ";username|" + name;
- h.manage_info(m8231);
- continue;
- case "2":
- System.out.print("\nType song's new duration:\n> ");
- String newdur8232 = reader.readLine();
- String m8232 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|duration;new information|" + newdur8232 + ";username|" + name;
- h.manage_info(m8232);
- continue;
- case "3":
- System.out.print("\nType song's new composer:\n> ");
- String newcomp8233 = reader.readLine();
- String m8233 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|composer;new information|" + newcomp8233 + ";username|" + name;
- h.manage_info(m8233);
- continue;
- case "4":
- System.out.print("\nType song's new lyrics:\n> ");
- String newlyrics8234 = reader.readLine();
- String m8234 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|composer;new lyrics|" + newlyrics8234 + ";username|" + name;
- h.manage_info(m8234);
- continue;
- default:
- continue;
- }
- default:
- continue;
- }
- default:
- continue;
- }
- }
- else{
- continue;
- }
- case "7":
- if(c.getEditor()){
- System.out.print("\nType username:\n> ");
- String user9 = reader.readLine();
- h.editor(name, user9, "change to editor");
- continue;
- }
- else
- continue;
- case "0":
- h.logout(name);
- System.exit(0);
- default:
- continue;
- }
- }
- }
- catch(RemoteException e){
- System.out.println("Something went wrong, please try again");
- lookup_connection(c, reader, name, password, editor);
- }
- catch(IOException e){
- System.out.println("Exception: " + e);
- }
- }
- public static void lookup_connection(Client c, BufferedReader reader, String name, String password, boolean editor){
- boolean bool = false;
- try{
- while(true){
- h = (RmiServer) Naming.lookup("rmi://192.168.1.78:1099/XPTO");
- boolean aux = h.test_server();
- if(aux){
- h.enter(c, name, password, "login");
- c.setUsername(name);
- c.setEditor(editor);
- menu(c, reader, name, password, editor);
- break;
- }
- else{
- continue;
- }
- }
- }
- catch (Exception e){
- System.out.print("");
- }
- }
- public static void main(String args[]) {
- String a;
- String password = null;
- String name = null;
- boolean editor = false;
- Client c = null;
- System.getProperties().put("java.security.policy", "policy.all");
- System.setSecurityManager(new RMISecurityManager());
- InputStreamReader input = new InputStreamReader(System.in);
- BufferedReader reader = new BufferedReader(input);
- try {
- h = (RmiServer) Naming.lookup("rmi://192.168.1.78:1099/XPTO");
- c = new Client();
- while(true){
- String log;
- System.out.print("\nType login or register\n> ");
- a = reader.readLine();
- if(a.equals("login")){
- System.out.print("\nWhat is your username?\n> ");
- name = reader.readLine();
- System.out.print("\nWhat is your password?\n> ");
- password = reader.readLine();
- log = h.enter((CClient) c, name, password, "login");
- String aux1 [] = log.split("//");
- if (aux1[0].equals("no")){
- System.out.println("\nWrong information, try again");
- continue;
- }
- else if (aux1[0].equals("yes")){
- System.out.println("\nStatus Logged | Welcome back to DropMusic");
- if(aux1[1].equals("yes")){
- editor = true;
- c.setEditor(true);
- }
- else{
- editor = false;
- c.setEditor(false);
- }
- if(aux1[2].equals(" "))
- System.out.println("Notifications while you were ofline:\n> " + aux1[2]);
- c.setUsername(name);
- menu(c, reader, name, password, editor);
- }
- }
- else if(a.equals("register")){
- System.out.print("\nWhat is your username?\n> ");
- name = reader.readLine();
- System.out.print("\nWhat is your password? \n> ");
- password = reader.readLine();
- log = h.enter((CClient) c, name, password, "register");
- String aux2 [] = log.split("//");
- if (aux2[0].equals("no")){
- System.out.println("Username already exists, try again");
- continue;
- }
- else if (aux2[0].equals("yes")){
- System.out.println("\nStatus Subscribed | Welcome to DropMusic");
- c.setUsername(name);
- menu(c, reader, name, password, editor);
- }
- }
- else{
- System.out.println("Something went wrong, try again");
- continue;
- }
- }
- }
- catch (RemoteException e) {
- System.out.println("Something went wrong, please try again");
- lookup_connection(c, reader, name, password, editor);
- }
- catch (NotBoundException e) {
- System.out.println("Exception: " + e);
- }
- catch (IOException e){
- System.out.println("Exception: " + e);
- }
- }
- }
Add Comment
Please, Sign In to add comment