Advertisement
Guest User

Untitled

a guest
Oct 25th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.77 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.MalformedURLException;
  3. import java.net.Socket;
  4. import java.rmi.Naming;
  5. import java.rmi.NotBoundException;
  6. import java.rmi.RemoteException;
  7. import java.rmi.registry.LocateRegistry;
  8. import java.rmi.server.UnicastRemoteObject;
  9. import java.util.Scanner;
  10.  
  11. import static java.lang.Thread.sleep;
  12.  
  13. public class RMI_Client extends UnicastRemoteObject implements RMI_Client_Interface{
  14.  
  15. RMI_Client() throws RemoteException {
  16. super();
  17. }
  18.  
  19. public static void main(String args[]) {
  20. System.setProperty("java.rmi.server.hostname", "10.16.2.226");
  21. int timeOut = 0;
  22. int maxTimeOut = 5;
  23. RMI_ServerInterface serverInterface = null;
  24. RMI_Client c = null;
  25. String username = "", password = "", choice = "", song = "", artist = "", album = "", new_song = "", old_song = "",
  26. old_artist = "", new_artist = "", genre = "", new_album_name = "", new_album_description = "", user_to_promote="", review="",
  27. music_name = "", music_artist = "";
  28.  
  29. String message;
  30. String[] parts;
  31. String address, filepath_send, filepath_recv;
  32. int port;
  33. Scanner scan;
  34. double rate=0;
  35. boolean logged_in;
  36. boolean ask_input = true;
  37. boolean errorReconnecting = false;
  38.  
  39. do {
  40. try {
  41. serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry("10.16.2.226", 7000).lookup("benfica");
  42. //serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry(7000).lookup("benfica");
  43. System.out.println("Connected");
  44. timeOut = 0;
  45. c = new RMI_Client();
  46. scan = new Scanner(System.in);
  47. logged_in = false;
  48.  
  49. do {
  50. if (ask_input) {
  51. choice = scan.nextLine();
  52. }
  53. try {
  54. if (choice.equals("sign up")) {
  55. if (ask_input) {
  56. System.out.print("Username: ");
  57. username = scan.nextLine();
  58. System.out.print("Password: ");
  59. password = scan.nextLine();
  60. }
  61. message = serverInterface.singUp(username, password);
  62. System.out.println(message);
  63.  
  64.  
  65. if (message.equals("Sign up succesfully")) {
  66. serverInterface.subscribe((RMI_Client_Interface) c, username);
  67. logged_in = true;
  68. }
  69.  
  70. } else if (choice.equals("login")) {
  71.  
  72. if (ask_input) {
  73. System.out.print("Username: ");
  74. username = scan.nextLine();
  75. System.out.print("Password: ");
  76. password = scan.nextLine();
  77. }
  78.  
  79. message = serverInterface.login(username, password);
  80. System.out.println(message);
  81.  
  82. if (message.equals("Logged in")) {
  83. message = serverInterface.verifyNotifications(username);
  84. System.out.println(message);
  85.  
  86. logged_in = true;
  87. }
  88.  
  89. } else {
  90. System.out.println("type <sign up> or <login>");
  91. }
  92. ask_input = true;
  93. } catch (Exception re) {
  94. try {
  95. serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry("10.16.2.226", 7000).lookup("benfica");
  96. ask_input = false;
  97. } catch (RemoteException re2) {
  98. timeOut++;
  99. ask_input = false;
  100. System.out.println("reconnecting...");
  101.  
  102. if (timeOut > maxTimeOut) {
  103. System.out.println("Time out.");
  104. System.exit(0);
  105. }
  106. }
  107. }
  108. } while (!logged_in);
  109.  
  110. ask_input = true;
  111. while (true) {
  112. // subscreve
  113. serverInterface.subscribe((RMI_Client_Interface) c, username);
  114.  
  115. try {
  116. if (ask_input)
  117. choice = scan.nextLine();
  118.  
  119. switch (choice) {
  120. case "add music":
  121. if (ask_input) {
  122. System.out.print("Music name: ");
  123. song = scan.nextLine();
  124. System.out.print("Artist name: ");
  125. artist = scan.nextLine();
  126. }
  127. message = serverInterface.addMusic(artist, song);
  128. System.out.println(message);
  129. ask_input = true;
  130. break;
  131. case "change music":
  132. if (ask_input) {
  133. System.out.print("Music name: ");
  134. old_song = scan.nextLine();
  135. System.out.print("Artist name: ");
  136. old_artist = scan.nextLine();
  137. System.out.print("New music name: ");
  138. new_song = scan.nextLine();
  139. System.out.print("New Artist name: ");
  140. new_artist = scan.nextLine();
  141. }
  142. message = serverInterface.changeMusic(old_artist, old_song, new_artist, new_song, username);
  143. System.out.println(message);
  144. ask_input = true;
  145. break;
  146. case "remove music":
  147. if (ask_input) {
  148. System.out.print("Music name: ");
  149. song = scan.nextLine();
  150. System.out.print("Artist name: ");
  151. artist = scan.nextLine();
  152. }
  153. message = serverInterface.removeMusic(artist, song, username);
  154. System.out.println(message);
  155. ask_input = true;
  156. break;
  157. case "add artist":
  158. if (ask_input) {
  159. System.out.print("Artist name: ");
  160. artist = scan.nextLine();
  161. }
  162. message = serverInterface.addArtist(artist);
  163. System.out.println(message);
  164. ask_input = true;
  165. break;
  166. case "change artist":
  167. if (ask_input) {
  168. System.out.print("Artist name: ");
  169. old_artist = scan.nextLine();
  170. System.out.print("New artist name: ");
  171. new_artist = scan.nextLine();
  172. }
  173. message = serverInterface.changeArtist(old_artist, new_artist, username);
  174. System.out.println(message);
  175. ask_input = true;
  176. break;
  177. case "remove artist":
  178. if (ask_input) {
  179. System.out.print("Artist name: ");
  180. artist = scan.nextLine();
  181. }
  182. message = serverInterface.removeArtist(artist, username);
  183. System.out.println(message);
  184. ask_input = true;
  185. break;
  186. case "add album":
  187. if (ask_input) {
  188. System.out.print("Album name: ");
  189. album = scan.nextLine();
  190. System.out.print("Artist name: ");
  191. artist = scan.nextLine();
  192. }
  193. message = serverInterface.addAlbum(album, artist);
  194. System.out.println(message);
  195. ask_input = true;
  196. break;
  197. // change album artist name
  198. case "change album artist":
  199. if (ask_input) {
  200. System.out.print("Album name: ");
  201. album = scan.nextLine();
  202. System.out.print("Artist name: ");
  203. old_artist = scan.nextLine();
  204. System.out.print("New artist name: ");
  205. new_artist = scan.nextLine();
  206. }
  207. message = serverInterface.changeAlbumArtist(album, old_artist, new_artist, username);
  208. System.out.println(message);
  209. ask_input = true;
  210. break;
  211. // change album album name
  212. case "change album name":
  213. if (ask_input) {
  214. System.out.print("Album name: ");
  215. album = scan.nextLine();
  216. System.out.print("Artist name: ");
  217. old_artist = scan.nextLine();
  218. System.out.print("New album name: ");
  219. new_album_name = scan.nextLine();
  220. }
  221. message = serverInterface.changeAlbumName(album, old_artist, new_album_name, username);
  222. System.out.println(message);
  223. ask_input = true;
  224. break;
  225. // change album description
  226. case "change album description":
  227. if (ask_input) {
  228. System.out.print("Album name: ");
  229. album = scan.nextLine();
  230. System.out.print("Artist name: ");
  231. old_artist = scan.nextLine();
  232. System.out.print("New description: ");
  233. new_album_description = scan.nextLine();
  234. }
  235. message = serverInterface.changeAlbumDescription(album, old_artist, new_album_description, username);
  236. System.out.println(message);
  237. ask_input = true;
  238. break;
  239. case "remove album":
  240. if (ask_input) {
  241. System.out.print("Album name: ");
  242. album = scan.nextLine();
  243. System.out.print("Artist name: ");
  244. artist = scan.nextLine();
  245. }
  246. message = serverInterface.removeAlbum(album, artist, username);
  247. System.out.println(message);
  248. ask_input = true;
  249. break;
  250. case "remove music from album":
  251. if (ask_input) {
  252. System.out.print("Album name: ");
  253. album = scan.nextLine();
  254. System.out.print("Album artist name: ");
  255. artist = scan.nextLine();
  256. System.out.print("Music artist name: ");
  257. music_artist = scan.nextLine();
  258. System.out.print("Music name: ");
  259. music_name = scan.nextLine();
  260.  
  261. message = serverInterface.removeAlbumMusic(album, artist, music_name, music_artist);
  262. System.out.println(message);
  263. ask_input = true;
  264. }
  265. break;
  266. case "add music to album":
  267. if (ask_input) {
  268. System.out.print("Album name: ");
  269. album = scan.nextLine();
  270. System.out.print("Album artist name: ");
  271. artist = scan.nextLine();
  272. System.out.print("Music artist name: ");
  273. music_artist = scan.nextLine();
  274. System.out.print("Music name: ");
  275. music_name = scan.nextLine();
  276. }
  277. message = serverInterface.addMusicToAlbum(album, artist, music_name, music_artist);
  278. System.out.println(message);
  279. ask_input = true;
  280. break;
  281. case "search by artist":
  282. if (ask_input) {
  283. System.out.print("Artist name: ");
  284. artist = scan.nextLine();
  285. }
  286. message = serverInterface.searchMusicbyartist(artist);
  287. System.out.println(message);
  288. ask_input = true;
  289. break;
  290. case "search by album":
  291. if (ask_input) {
  292. System.out.print("Album name: ");
  293. album = scan.nextLine();
  294. }
  295. message = serverInterface.searchMusicbyalbum(album);
  296. System.out.println(message);
  297. ask_input = true;
  298. break;
  299. case "search by genre":
  300. if (ask_input) {
  301. System.out.print("Genre: ");
  302. genre = scan.nextLine();
  303. }
  304. message = serverInterface.searchMusicbygenre(genre);
  305. System.out.println(message);
  306. ask_input = true;
  307. break;
  308. case "album details":
  309. if (ask_input) {
  310. System.out.print("Album name: ");
  311. album = scan.nextLine();
  312. System.out.print("Artist name: ");
  313. artist = scan.nextLine();
  314. }
  315. message = serverInterface.albumDetails(album, artist);
  316. System.out.println(message);
  317. ask_input = true;
  318. break;
  319. case "artist details":
  320. if (ask_input) {
  321. System.out.print("Artist name: ");
  322. artist = scan.nextLine();
  323. }
  324. message = serverInterface.artistDetails(artist);
  325. System.out.println(message);
  326. ask_input = true;
  327. break;
  328. case "promote user":
  329. if (ask_input) {
  330. System.out.print("Username to promote: ");
  331. user_to_promote = scan.nextLine();
  332. }
  333. message = serverInterface.giveEditorToClient(user_to_promote, username);
  334. System.out.println(message);
  335. ask_input = true;
  336. break;
  337. case "write review":
  338. if (ask_input) {
  339. System.out.print("Album name: ");
  340. album = scan.nextLine();
  341. System.out.print("Artist name: ");
  342. artist = scan.nextLine();
  343. //username = scan.nextLine();
  344. System.out.print("Review: ");
  345. review = scan.nextLine();
  346. System.out.print("Rate: ");
  347. rate = scan.nextDouble();
  348. }
  349. message = serverInterface.writeReview(album, artist, username, review, rate);
  350. System.out.println(message);
  351. ask_input = true;
  352. break;
  353. case "upload music":
  354. if (ask_input) {
  355. System.out.print("Music name: ");
  356. music_name = scan.nextLine();
  357. System.out.print("Artist name: ");
  358. artist = scan.nextLine();
  359. }
  360. message = serverInterface.uploadMusic(artist, music_name, username);
  361. System.out.println(message);
  362. parts = message.split("[|;]");
  363.  
  364. if(!message.equals("Music not found")){
  365. address = parts[9];
  366. port = Integer.parseInt(parts[11]);
  367. filepath_send = parts[13];
  368. filepath_recv = parts[15];
  369.  
  370. try {
  371. Socket socket = new Socket(address, port);
  372.  
  373. File file = new File(filepath_send);
  374. FileInputStream fis = new FileInputStream(file);
  375. BufferedInputStream bis = new BufferedInputStream(fis);
  376. OutputStream os = socket.getOutputStream();
  377. byte b[] = new byte[(int) file.length()];
  378.  
  379. bis.read(b, 0, b.length);
  380. os.write(b, 0, b.length);
  381.  
  382. os.flush();
  383. socket.close();
  384. System.out.println("File sent succesfully!");
  385. } catch (IOException e) {
  386. e.printStackTrace();
  387. }
  388. }
  389. ask_input = true;
  390. break;
  391. case "permission to upload":
  392. if (ask_input) {
  393. System.out.print("Artist name: ");
  394. artist = scan.nextLine();
  395. System.out.print("Music name: ");
  396. music_name = scan.nextLine();
  397. System.out.print("New user: ");
  398. }
  399. String new_user = scan.nextLine();
  400. message = serverInterface.givePermissiontoUpload(username, artist,music_name,new_user);
  401. System.out.println(message);
  402. ask_input = true;
  403. break;
  404.  
  405. case "download music":
  406. message = serverInterface.getAllPermitedUploads(username);
  407. System.out.println(message);
  408.  
  409. if (!message.equals("No musics to download")) {
  410. if (ask_input) {
  411. System.out.print("Music name: ");
  412. music_name = scan.nextLine();
  413. System.out.print("Artist name: ");
  414. artist = scan.nextLine();
  415. }
  416. message = serverInterface.getUploadsWithPermissions(username, music_name, artist);
  417. System.out.println(message);
  418.  
  419. if (!message.equals("You don't have permissions to download")) {
  420.  
  421. message = serverInterface.downloadMusic(artist, music_name, username);
  422. System.out.println(message);
  423.  
  424. if (!message.equals("music not found for download")) {
  425. parts = message.split("[|;]");
  426. address = parts[3];
  427. port = Integer.parseInt(parts[5]);
  428. filepath_send = parts[7];
  429. filepath_recv = parts[9];
  430.  
  431. Socket socket = new Socket(address, port);
  432. byte[] b = new byte[10000];
  433.  
  434. File file = new File(filepath_recv);
  435. FileOutputStream fos = new FileOutputStream(file);
  436. BufferedOutputStream bos = new BufferedOutputStream(fos);
  437. InputStream is = socket.getInputStream();
  438.  
  439. int bytesRead;
  440. while ((bytesRead = is.read(b)) > 0)
  441. bos.write(b, 0, bytesRead);
  442. bos.flush();
  443. socket.close();
  444. System.out.println("File downloaded successfully!");
  445. }
  446. }
  447. }
  448. ask_input = true;
  449. break;
  450. default:
  451. System.out.println("commands: add music, change music, remove music, add artist, change artist, remove artist\n" +
  452. "add album, change album, remove album, search by artist, search by album, search by genre\n" +
  453. "artist details, write review, upload music, download music\n");
  454. }
  455. } catch (Exception e) {
  456. ask_input = false;
  457. System.out.println("??????");
  458. e.printStackTrace();
  459. try {
  460. serverInterface = (RMI_ServerInterface) LocateRegistry.getRegistry("10.16.2.226", 7000).lookup("benfica");
  461. } catch (RemoteException re2) {
  462. System.out.println("reconnecting...");
  463. }
  464. }
  465. }
  466. } catch (Exception e) {
  467. // tentar dar reconnect
  468. // time out 5 vezes
  469. timeOut++;
  470. System.out.println("Attempt "+timeOut+" to connect to server...");
  471. try {
  472. sleep(500);
  473. } catch (InterruptedException e1) {
  474. e1.printStackTrace();
  475. }
  476. }
  477. } while(timeOut < maxTimeOut);
  478.  
  479. System.out.println("Time Out.");
  480. System.exit(0);
  481. }
  482.  
  483. public void print_on_client(String s) {
  484. System.out.println(s);
  485. }
  486.  
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement