Guest User

Untitled

a guest
Oct 27th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.06 KB | None | 0 0
  1. import java.rmi.*;
  2. import java.rmi.server.*;
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.rmi.registry.LocateRegistry;
  7. import java.rmi.registry.Registry;
  8.  
  9. public class Client extends UnicastRemoteObject implements CClient {
  10. private static int PORT = 6060;
  11.  
  12. static RmiServer h = null;
  13.  
  14. public String username;
  15. public boolean editor;
  16.  
  17. public static String message; //mensagens do servidor
  18.  
  19. public Client() throws RemoteException {
  20. this.username = username;
  21. this.editor = false;
  22. }
  23.  
  24. public String getUsername() throws RemoteException{
  25. return username;
  26. }
  27.  
  28. public void setUsername(String user) throws RemoteException{
  29. this.username = user;
  30. }
  31.  
  32. public boolean getEditor() throws RemoteException{
  33. return editor;
  34. }
  35.  
  36. public void setEditor(boolean e) throws RemoteException{
  37. this.editor = e;
  38. }
  39.  
  40. public void print_on_client(String s) throws RemoteException {
  41. System.out.println(s);
  42. }
  43.  
  44. public void message_client(String s) throws RemoteException{
  45. message = s;
  46. }
  47.  
  48. public static void downloadSong(String songName, String ip){
  49. System.out.println("entrei download");
  50. Socket s = null;
  51. int FILE_SIZE = 97971310;
  52. try {
  53. System.out.println("try download");
  54. s = new Socket(ip, PORT);//change
  55.  
  56. DataOutputStream out = new DataOutputStream(s.getOutputStream());
  57. // DataInputStream in= new DataInputStream(s.getInputStream());
  58. String texto = "download;"+songName;
  59.  
  60. // WRITE INTO THE SOCKET
  61. out.writeUTF(texto);
  62. System.out.println("wrote download");
  63.  
  64. int bytesRead;
  65. int current = 0;
  66. FileOutputStream fos = null;
  67. BufferedOutputStream bos = null;
  68.  
  69. try {
  70. System.out.println("second try download");
  71. byte [] mybytearray = new byte [FILE_SIZE];
  72. InputStream is = s.getInputStream();
  73.  
  74. String userHomeFolder = System.getProperty("user.home")+"\\Desktop";
  75.  
  76. fos = new FileOutputStream(userHomeFolder+"\\"+songName+".wav");
  77. bos = new BufferedOutputStream(fos);
  78. System.out.println("read download");
  79. bytesRead = is.read(mybytearray,0,mybytearray.length);
  80. current = bytesRead;
  81.  
  82. System.out.println("do download");
  83. do {
  84. bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
  85. if(bytesRead > 0)
  86. current += bytesRead;
  87. } while(bytesRead > 0);
  88. System.out.println("WRITE download");
  89. bos.write(mybytearray, 0 , current);
  90. bos.flush();
  91.  
  92. }
  93. finally {
  94. if (fos != null) fos.close();
  95. if (bos != null) bos.close();
  96. if (s != null) s.close();
  97. }
  98.  
  99. System.out.println("Download feito");
  100.  
  101. } catch (EOFException e) {
  102. System.out.println("EOF:" + e.getMessage());
  103. } catch (IOException e) {
  104. System.out.println("IO:" + e.getMessage());
  105. } finally {
  106. if (s != null)
  107. try {
  108. s.close();
  109. } catch (IOException e) {
  110. System.out.println("close:" + e.getMessage());
  111. }
  112. }
  113. }
  114.  
  115. public static void uploadSong(String songName, String ip){
  116. Socket s = null;
  117. //int FILE_SIZE = 7971310;
  118. try {
  119. FileInputStream fis = null;
  120. BufferedInputStream bis = null;
  121. OutputStream os = null;
  122.  
  123. s = new Socket(ip, PORT);//change
  124. String userHomeFolder = System.getProperty("user.home")+"\\Desktop";
  125. File file = new File(userHomeFolder+"\\"+songName+".wav");
  126. DataOutputStream out = new DataOutputStream(s.getOutputStream());
  127. String texto = "upload;"+songName;
  128. out.writeUTF(texto);
  129.  
  130. byte [] mybytearray = new byte [(int) file.length()];
  131. fis = new FileInputStream(file);
  132. bis = new BufferedInputStream(fis);
  133. bis.read(mybytearray,0,mybytearray.length);
  134. os = s.getOutputStream();
  135. os.write(mybytearray,0,mybytearray.length);
  136. os.flush();
  137.  
  138. System.out.println("Upload completed");
  139.  
  140.  
  141. } catch (EOFException e) {
  142. System.out.println("EOF:" + e.getMessage());
  143. } catch (IOException e) {
  144. System.out.println("IO:" + e.getMessage());
  145. } finally {
  146. if (s != null)
  147. try {
  148. s.close();
  149. } catch (IOException e) {
  150. System.out.println("close:" + e.getMessage());
  151. }
  152. }
  153. }
  154.  
  155. public static void menu(Client c, BufferedReader reader, String name, String password, boolean editor){
  156. try{
  157. while(true){
  158. System.out.println("\n1- Search song");
  159. System.out.println("2- Details of an album");
  160. System.out.println("3- Details of an artist");
  161. System.out.println("4- Write a review of an album");
  162. System.out.println("5- Share with friends uploaded song");
  163. if (c.getEditor()) {
  164. System.out.println("6- Manage information");
  165. System.out.println("7- Make an user a new editor");
  166. }
  167. System.out.println("0- Logout");
  168. System.out.print("> ");
  169. Scanner sc = new Scanner(System.in);
  170. String num = sc.next();
  171.  
  172. switch(num){
  173. case "1":
  174. System.out.println("\n1- Trough artist");
  175. System.out.println("2- Trough album");
  176. System.out.print("3- By song name\n> ");
  177. String aux1 = sc.next();
  178. String artist11 = "";
  179. String ip = "";
  180. String ip2 = "";
  181. String multicast = "";
  182. switch(aux1){
  183. case "1":
  184. System.out.print("\nType artist's name:\n> ");
  185. artist11 = reader.readLine();
  186. String m11 = "type|partial search artist;info|" + artist11 + ";username|" + name;
  187. h.search_song(m11);
  188. if(message.equals("yes-artist")){
  189. message = "";
  190. System.out.print("\nChoose one song:\n> ");
  191. String song11 = reader.readLine();
  192. System.out.println("\n1- Duration");
  193. System.out.println("2- Composer");
  194. System.out.println("3- Lyrics");
  195. System.out.println("4- Upload");
  196. System.out.print("5- Download\n> ");
  197. String aux11 = reader.readLine();
  198.  
  199. switch(aux11){
  200. case "1":
  201. String m111 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|duration;username|" + name;
  202. h.search_song(m111);
  203. continue;
  204. case "2":
  205. String m112 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|composer;username|" + name;
  206. h.search_song(m112);
  207. continue;
  208. case "3":
  209. String m113 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|lyrics;username|" + name;
  210. h.search_song(m113);
  211. continue;
  212. case "4":
  213. String m114 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|uploadServerIP;username|" + name;
  214. h.search_song(m114);
  215. multicast = message;
  216.  
  217. if (!message.equals("message")){
  218. String m1141 = multicast + ",type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|serverIP;username|" + name;
  219. h.search_song(m1141);
  220. ip = message;
  221. ip2 = message.split("/")[1];
  222. uploadSong(song11, ip2);
  223. }
  224. continue;
  225. case "5":
  226. String m115 = "type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|downloadServerIP;username|" + name;
  227. h.search_song(m115);
  228. multicast = message;
  229.  
  230. if (!message.equals("message")){
  231. String m1151 = multicast + ",type|search music by artist;artist name|" + artist11 + ";song name|" + song11 + ";option|serverIP;username|" + name;
  232. h.search_song(m1151);
  233. ip = message;
  234. ip2 = message.split("/")[1];
  235. downloadSong(song11, ip2);
  236. }
  237. continue;
  238. default:
  239. continue;
  240. }
  241. }
  242. else{
  243. continue;
  244. }
  245. case "2":
  246. System.out.print("\nType album's name:\n> ");
  247. String album12 = reader.readLine();
  248. String m12 = "type|partial search album;info|" + album12 + ";username|" + name;
  249. h.search_song(m12);
  250. if(message.equals("yes-album")){
  251. System.out.print("\nChoose one song:\n> ");
  252. String song12 = reader.readLine();
  253. System.out.println("\n1- Duration");
  254. System.out.println("2- Composer");
  255. System.out.println("3- Lyrics");
  256. System.out.println("4- Upload");
  257. System.out.print("5- Download\n> ");
  258. String aux12 = reader.readLine();
  259.  
  260. switch(aux12){
  261. case "1":
  262. String m121 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|duration;username|" + name;
  263. h.search_song(m121);
  264. continue;
  265. case "2":
  266. String m122 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|composer;username|" + name;
  267. h.search_song(m122);
  268. continue;
  269. case "3":
  270. String m123 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|lyrics;username|" + name;
  271. h.search_song(m123);
  272. continue;
  273. case "4":
  274. String m124 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|uploadServerIP;username|" + name;
  275. h.search_song(m124);
  276. multicast = message;
  277.  
  278. if (!message.equals("message")){
  279. String m1241 = multicast + ",type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|serverIP;username|" + name;
  280. h.search_song(m1241);
  281. ip = message;
  282. ip2 = message.split("/")[1];
  283. uploadSong(song12, ip2);
  284. }
  285.  
  286. continue;
  287. case "5":
  288. String m125 = "type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|downloadServerIP;username|" + name;
  289. h.search_song(m125);
  290. multicast = message;
  291.  
  292. if (!message.equals("message")){
  293. String m1251 = multicast + ",type|search music by album;album name|" + album12 + ";song name|" + song12 + ";option|serverIP;username|" + name;
  294. h.search_song(m1251);
  295. ip = message;
  296. ip2 = message.split("/")[1];
  297. downloadSong(song12, ip2);
  298. }
  299.  
  300. continue;
  301. default:
  302. continue;
  303. }
  304. }
  305. else{
  306. continue;
  307. }
  308. case "3":
  309. System.out.print("\nType song's name:\n> ");
  310. String song13 = reader.readLine();
  311. System.out.println("\n1- Duration");
  312. System.out.println("2- Composer");
  313. System.out.println("3- Lyrics");
  314. System.out.println("4- Upload");
  315. System.out.print("5- Download\n> ");
  316. String aux13 = reader.readLine();
  317.  
  318. switch(aux13){
  319. case "1":
  320. String m131 = "type|search music;song name|" + song13 + ";option|duration;username|" + name;
  321. h.search_song(m131);
  322. continue;
  323. case "2":
  324. String m132 = "type|search music;song name|" + song13 + ";option|composer;username|" + name;
  325. h.search_song(m132);
  326. continue;
  327. case "3":
  328. String m133 = "type|search music;song name|" + song13 + ";option|lyrics;username|" + name;
  329. h.search_song(m133);
  330. continue;
  331. case "4":
  332. String m134 = "type|search music;song name|" + song13 + ";option|uploadServerIP;username|" + name;
  333. h.search_song(m134);
  334. multicast = message;
  335.  
  336. if (!message.equals("message")){
  337. String m1341 = multicast + ",type|search music;song name|" + song13 + ";option|serverIP;username|" + name;
  338. h.search_song(m1341);
  339. ip = message;
  340. ip2 = message.split("/")[1];
  341. uploadSong(song13, ip2);
  342. }
  343.  
  344. continue;
  345. case "5":
  346. String m135 = "type|search music;song name|" + song13 + ";option|downloadServerIP;username|" + name;
  347. h.search_song(m135);
  348. multicast = message;
  349.  
  350. if (!message.equals("message")){
  351. String m1351 = multicast + ",type|search music; song name|" + song13 + ";option|serverIP;username|" + name;
  352. h.search_song(m1351);
  353. ip = message;
  354. ip2 = message.split("/")[1];
  355. downloadSong(song13, ip2);
  356. }
  357.  
  358. continue;
  359. default:
  360. continue;
  361. }
  362. default:
  363. continue;
  364. }
  365. case "2":
  366. System.out.print("\nType artist's name:\n> ");
  367. String artist2 = reader.readLine();
  368. System.out.print("\nType album's name:\n> ");
  369. String album2 = reader.readLine();
  370. System.out.println("\n1- Description");
  371. System.out.println("2- List of songs");
  372. System.out.print("3- Reviews\n> ");
  373. String aux2 = sc.next();
  374. switch(aux2){
  375. case "1":
  376. h.details_album(name, artist2, album2, "description", "details album");
  377. continue;
  378. case "2":
  379. h.details_album(name, artist2, album2, "list of songs", "details album");
  380. continue;
  381. case "3":
  382. h.details_album(name, artist2, album2, "reviews", "details album");
  383. continue;
  384. default:
  385. continue;
  386. }
  387. case "3":
  388. System.out.print("\nType artist's name:\n> ");
  389. String artist3 = reader.readLine();
  390. h.details_artist(name, artist3, "details artist");
  391. continue;
  392. case "4":
  393. System.out.print("\nType album's name:\n> ");
  394. String album4 = reader.readLine();
  395.  
  396. String score4 = null;
  397. while(true){
  398. System.out.print("\nType album's score (0 to 10):\n> ");
  399. score4 = reader.readLine();
  400. try{
  401. int score41 = Integer.parseInt(score4);
  402. if(score41>=0 && score41<=10){
  403. break;
  404. }
  405. else{
  406. System.out.println("That input is invalid");
  407. continue;
  408. }
  409. }
  410. catch(NumberFormatException e){
  411. System.out.println("That input is invalid");
  412. continue;
  413. }
  414. }
  415.  
  416. System.out.print("\nWrite review (300 characters):\n> ");
  417. String review4 = reader.readLine();
  418. String review42;
  419. if(review4.length()>300)
  420. review42 = review4.substring(0,300);
  421. else
  422. review42 = review4;
  423.  
  424. h.review(name, album4, review42, score4, "review");
  425. continue;
  426. case "5":
  427. System.out.print("\nType album's name:\n> ");
  428. String album5 = reader.readLine();
  429. System.out.print("\nType song's name:\n> ");
  430. String song5 = reader.readLine();
  431. System.out.println("\nType your friends usernames (write BREAK to end):");
  432. String user_friends5 = "";
  433. while(true){
  434. System.out.print(">");
  435. String friend5 = reader.readLine();
  436. if (friend5.equals("BREAK")){
  437. break;
  438. }
  439. else{
  440. user_friends5 += friend5 + "//";
  441. continue;
  442. }
  443. }
  444. h.share(song5, album5, user_friends5, name);
  445. continue;
  446. case "6":
  447. if(c.getEditor()){
  448. System.out.println("\n1- Insert");
  449. System.out.print("2- Change\n> ");
  450. String aux8 = sc.next();
  451. switch(aux8){
  452. case "1":
  453. System.out.println("\n1- Artist");
  454. System.out.println("2- Album");
  455. System.out.print("3- Song\n> ");
  456. String aux81 = sc.next();
  457. switch(aux81){
  458. case "1":
  459. System.out.print("\nType artist's name:\n> ");
  460. String artist811 = reader.readLine();
  461. System.out.print("\nType artist's description:\n> ");
  462. String description811 = reader.readLine();
  463. System.out.print("\nType artist's working period:\n> ");
  464. String period811 = reader.readLine();
  465.  
  466. String m811 = "type|insert artist;artist name|" + artist811 + ";description|" + description811 + ";groupPeriod|" + period811 + ";username|" + name;
  467. h.manage_info(m811);
  468. continue;
  469. case "2":
  470. System.out.print("\nType artist's name:\n> ");
  471. String artist812 = reader.readLine();
  472. System.out.print("\nType album's name:\n> ");
  473. String album812 = reader.readLine();
  474. System.out.print("\nType album's genre:\n> ");
  475. String genre812 = reader.readLine();
  476. System.out.print("\nType album's description:\n> ");
  477. String description812 = reader.readLine();
  478. System.out.print("\nType album's date:\n> ");
  479. String date812 = reader.readLine();
  480.  
  481. String m812 = "type|insert album;artist name|" + artist812 + ";album name|" + album812 + ";genre|" + genre812 + ";description|" + description812 + ";date|" + date812 + ";username|" + name;
  482. h.manage_info(m812);
  483. continue;
  484. case "3":
  485. System.out.print("\nType artist's name:\n> ");
  486. String artist813 = reader.readLine();
  487. System.out.print("\nType album's name:\n> ");
  488. String album813 = reader.readLine();
  489. System.out.print("\nType song's name:\n> ");
  490. String song813 = reader.readLine();
  491. System.out.print("\nType song's duration:\n> ");
  492. String duration813 = reader.readLine();
  493. System.out.print("\nType song's composer:\n> ");
  494. String composer813 = reader.readLine();
  495. System.out.print("\nType song's lyrics:\n> ");
  496. String lyrics813 = reader.readLine();
  497.  
  498. String m813 = "type|insert song;artist name|" + artist813 + ";album name|" + album813 + ";song name|" + song813 + ";duration|" + duration813 + ";compositor|" + composer813 + ";lyrics|" + lyrics813 + ";username|" + name;
  499. h.manage_info(m813);
  500. continue;
  501. default:
  502. continue;
  503. }
  504. case "2":
  505. System.out.println("\n1- Artist");
  506. System.out.println("2- Album");
  507. System.out.print("3- Song\n> ");
  508. String aux82 = sc.next();
  509. switch(aux82){
  510. case "1":
  511. System.out.println("\n1- Artist's name");
  512. System.out.println("2- Artist's description");
  513. System.out.print("3- Artist's working period\n> ");
  514. String aux821 = reader.readLine();
  515.  
  516. System.out.print("\nType artist's name:\n> ");
  517. String artist821 = reader.readLine();
  518. switch(aux821){
  519. case "1":
  520. System.out.print("\nType artist's new name:\n> ");
  521. String newartist8211 = reader.readLine();
  522. String m8211 = "type|change artist;artist name|" + artist821 + ";option|name;new information|" + newartist8211 + ";username|" + name;
  523. h.manage_info(m8211);
  524. continue;
  525. case "2":
  526. System.out.print("\nType artist's new description:\n> ");
  527. String newd8212 = reader.readLine();
  528. String m8212 = "type|change artist;artist name|" + artist821 + ";option|description;new information|" + newd8212 + ";username|" + name;
  529. h.manage_info(m8212);
  530. continue;
  531. case "3":
  532. System.out.print("\nType artist's new working period:\n> ");
  533. String newp8213 = reader.readLine();
  534. String m8213 = "type|change artist;artist name|" + artist821 + ";option|groupPeriod;new information|" + newp8213 + ";username|" + name;
  535. h.manage_info(m8213);
  536. continue;
  537. default:
  538. continue;
  539. }
  540. case "2":
  541. System.out.println("\n1- Album's name");
  542. System.out.println("2- Album's description");
  543. System.out.println("3- Album's genre");
  544. System.out.print("4- Album's date\n> ");
  545. String aux822 = reader.readLine();
  546.  
  547. System.out.print("\nType artist's name:\n> ");
  548. String artist822 = reader.readLine();
  549. System.out.print("\nType album's name:\n> ");
  550. String album822 = reader.readLine();
  551. switch(aux822){
  552. case "1":
  553. System.out.print("\nType album's new name:\n> ");
  554. String newalbum8221 = reader.readLine();
  555. String m8221 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|name;new information|" + newalbum8221 + ";username|" + name;
  556. h.manage_info(m8221);
  557. continue;
  558. case "2":
  559. System.out.print("\nType album's new description:\n> ");
  560. String newdesc8222 = reader.readLine();
  561. String m8222 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|description;new information|" + newdesc8222 + ";username|" + name;
  562. h.manage_info(m8222);
  563. continue;
  564. case "3":
  565. System.out.print("\nType album's new genre:\n> ");
  566. String newgenre8223 = reader.readLine();
  567. String m8223 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|genre;new information|" + newgenre8223 + ";username|" + name;
  568. h.manage_info(m8223);
  569. continue;
  570. case "4":
  571. System.out.print("\nType album's new date:\n> ");
  572. String newdate8224 = reader.readLine();
  573. String m8224 = "type|change album;artist name|" + artist822 + ";album name|" + album822 +";option|date;new information|" + newdate8224 + ";username|" + name;
  574. h.manage_info(m8224);
  575. continue;
  576. default:
  577. continue;
  578. }
  579. case "3":
  580. System.out.println("\n1- Song's name");
  581. System.out.println("2- Song's duration");
  582. System.out.println("3- Songs's composer");
  583. System.out.print("4- Song's lyrics\n> ");
  584. String aux823 = reader.readLine();
  585.  
  586. System.out.print("\nType artist's name:\n> ");
  587. String artist823 = reader.readLine();
  588. System.out.print("\nType album's name:\n> ");
  589. String album823 = reader.readLine();
  590. System.out.print("\nType song's name:\n> ");
  591. String song823 = reader.readLine();
  592. switch(aux823){
  593. case "1":
  594. System.out.print("\nType song's new name:\n> ");
  595. String newsong8231 = reader.readLine();
  596. String m8231 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|name;new information|" + newsong8231 + ";username|" + name;
  597. h.manage_info(m8231);
  598. continue;
  599. case "2":
  600. System.out.print("\nType song's new duration:\n> ");
  601. String newdur8232 = reader.readLine();
  602. String m8232 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|duration;new information|" + newdur8232 + ";username|" + name;
  603. h.manage_info(m8232);
  604. continue;
  605. case "3":
  606. System.out.print("\nType song's new composer:\n> ");
  607. String newcomp8233 = reader.readLine();
  608. String m8233 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|composer;new information|" + newcomp8233 + ";username|" + name;
  609. h.manage_info(m8233);
  610. continue;
  611. case "4":
  612. System.out.print("\nType song's new lyrics:\n> ");
  613. String newlyrics8234 = reader.readLine();
  614. String m8234 = "type|change song;artist name|" + artist823 + ";album name|" + album823 + ";song name|" + song823 + ";option|composer;new lyrics|" + newlyrics8234 + ";username|" + name;
  615. h.manage_info(m8234);
  616. continue;
  617. default:
  618. continue;
  619. }
  620. default:
  621. continue;
  622. }
  623. default:
  624. continue;
  625. }
  626. }
  627. else{
  628. continue;
  629. }
  630. case "7":
  631. if(c.getEditor()){
  632. System.out.print("\nType username:\n> ");
  633. String user9 = reader.readLine();
  634. h.editor(name, user9, "change to editor");
  635. continue;
  636. }
  637. else
  638. continue;
  639. case "0":
  640. h.logout(name);
  641. System.exit(0);
  642. default:
  643. continue;
  644. }
  645. }
  646. }
  647. catch(RemoteException e){
  648. System.out.println("Something went wrong, please try again");
  649. lookup_connection(c, reader, name, password, editor);
  650. }
  651. catch(IOException e){
  652. System.out.println("Exception: " + e);
  653. }
  654. }
  655.  
  656. public static void lookup_connection(Client c, BufferedReader reader, String name, String password, boolean editor){
  657. boolean bool = false;
  658.  
  659. try{
  660. while(true){
  661. h = (RmiServer) Naming.lookup("rmi://192.168.1.78:1099/XPTO");
  662. boolean aux = h.test_server();
  663. if(aux){
  664. h.enter(c, name, password, "login");
  665. c.setUsername(name);
  666. c.setEditor(editor);
  667. menu(c, reader, name, password, editor);
  668. break;
  669. }
  670. else{
  671. continue;
  672. }
  673. }
  674. }
  675. catch (Exception e){
  676. System.out.print("");
  677. }
  678. }
  679.  
  680. public static void main(String args[]) {
  681. String a;
  682.  
  683. String password = null;
  684. String name = null;
  685. boolean editor = false;
  686. Client c = null;
  687.  
  688. System.getProperties().put("java.security.policy", "policy.all");
  689. System.setSecurityManager(new RMISecurityManager());
  690.  
  691. InputStreamReader input = new InputStreamReader(System.in);
  692. BufferedReader reader = new BufferedReader(input);
  693. try {
  694. h = (RmiServer) Naming.lookup("rmi://192.168.1.78:1099/XPTO");
  695. c = new Client();
  696.  
  697. while(true){
  698. String log;
  699. System.out.print("\nType login or register\n> ");
  700. a = reader.readLine();
  701. if(a.equals("login")){
  702. System.out.print("\nWhat is your username?\n> ");
  703. name = reader.readLine();
  704. System.out.print("\nWhat is your password?\n> ");
  705. password = reader.readLine();
  706. log = h.enter((CClient) c, name, password, "login");
  707. String aux1 [] = log.split("//");
  708.  
  709. if (aux1[0].equals("no")){
  710. System.out.println("\nWrong information, try again");
  711. continue;
  712. }
  713. else if (aux1[0].equals("yes")){
  714. System.out.println("\nStatus Logged | Welcome back to DropMusic");
  715.  
  716. if(aux1[1].equals("yes")){
  717. editor = true;
  718. c.setEditor(true);
  719. }
  720. else{
  721. editor = false;
  722. c.setEditor(false);
  723. }
  724.  
  725. if(aux1[2].equals(" "))
  726. System.out.println("Notifications while you were ofline:\n> " + aux1[2]);
  727.  
  728. c.setUsername(name);
  729. menu(c, reader, name, password, editor);
  730. }
  731. }
  732. else if(a.equals("register")){
  733. System.out.print("\nWhat is your username?\n> ");
  734. name = reader.readLine();
  735. System.out.print("\nWhat is your password? \n> ");
  736. password = reader.readLine();
  737. log = h.enter((CClient) c, name, password, "register");
  738. String aux2 [] = log.split("//");
  739.  
  740. if (aux2[0].equals("no")){
  741. System.out.println("Username already exists, try again");
  742. continue;
  743. }
  744. else if (aux2[0].equals("yes")){
  745. System.out.println("\nStatus Subscribed | Welcome to DropMusic");
  746.  
  747. c.setUsername(name);
  748. menu(c, reader, name, password, editor);
  749. }
  750. }
  751. else{
  752. System.out.println("Something went wrong, try again");
  753. continue;
  754. }
  755. }
  756. }
  757. catch (RemoteException e) {
  758. System.out.println("Something went wrong, please try again");
  759. lookup_connection(c, reader, name, password, editor);
  760. }
  761. catch (NotBoundException e) {
  762. System.out.println("Exception: " + e);
  763. }
  764. catch (IOException e){
  765. System.out.println("Exception: " + e);
  766. }
  767. }
  768. }
Add Comment
Please, Sign In to add comment