Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.95 KB | None | 0 0
  1. //package calculator;
  2. import java.net.MalformedURLException;
  3. import java.rmi.Naming;
  4. import java.rmi.NotBoundException;
  5. import java.rmi.RemoteException;
  6. import java.rmi.*;
  7. import java.rmi.registry.LocateRegistry;
  8. import java.rmi.server.*;
  9. import java.io.*;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. import com.sun.org.apache.xpath.internal.SourceTree;
  14.  
  15. import java.net.*;
  16.  
  17. public class ClientRmi extends UnicastRemoteObject implements ClientRmiInt {
  18. private static ArrayList<Pessoa> bdPessoas;
  19. private String nome = new String();// este nome vai servir para identificar o cliente que deve receber as respostas
  20. // aos pedidos feitos (e recebido como um id de parametro)
  21. private int log = 0;
  22. public static Config config = new Config("configServer.cfg");
  23. private static String nomeServer = config.getNomeRMI();
  24.  
  25. public ClientRmi() throws RemoteException {
  26. super();
  27. }
  28.  
  29. public void print_on_client(String msg, String nome) throws RemoteException {
  30. if (this.nome.equals(nome) && this.log == 1
  31. || msg.equals("type / status ; logged / on ; msg / Welcome to DropMusic")
  32. || msg.equals("Registado com sucesso") || msg.equals("Nao foi possivel registar")
  33. || msg.equals("Utilizador nao registado!") || msg.equals("logout feito"))
  34. System.out.println("> " + msg);
  35. if (msg.equals("type / status ; logged / on ; msg / Welcome to DropMusic"))
  36. this.log = 1;
  37. if (msg.equals("logout feito"))
  38. this.log = 0;
  39. }
  40.  
  41. public String getNome() throws RemoteException {
  42. return this.nome;
  43. }
  44.  
  45. public void setNome(String nome) {
  46. this.nome = nome;
  47. }
  48.  
  49. // funcao para comunicar os downloads e uploads ao multicast
  50. public static void uploadDownloadTCP() {
  51. Socket socket;
  52. int bytesRead;
  53. int port = 0;
  54. DataOutputStream outToServer;
  55. BufferedReader inFromServer = null;
  56. Scanner keyboardScanner = new Scanner(System.in);
  57. System.out.println("Qual a porta:");
  58. while (true) {
  59. try {
  60. port = keyboardScanner.nextInt();
  61. break;
  62. } catch (java.util.InputMismatchException e) {
  63. System.out.println("Opcao Errada");
  64. keyboardScanner.nextLine();
  65. continue;
  66. }
  67. }
  68. System.out.println("Qual o host:");
  69. String host = keyboardScanner.nextLine();
  70. host = keyboardScanner.nextLine();
  71. try {
  72. // connect to the specified address:port
  73. socket = new Socket(host, port);
  74. System.out.println("SOCKET CRIADO \n\n");
  75.  
  76. DataInputStream in = new DataInputStream(socket.getInputStream());
  77. DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  78.  
  79. // para introduzir a opcao pretendida
  80. String texto = "";
  81. InputStreamReader input = new InputStreamReader(System.in);
  82. BufferedReader reader = new BufferedReader(input);
  83. System.out.println("Introduza opcao: (download ou upload)");
  84.  
  85. // le string do teclado
  86. try {
  87. texto = reader.readLine();
  88. } catch (Exception e) {
  89. }
  90.  
  91. if (texto.equals("upload")) {
  92.  
  93. // WRITE INTO THE SOCKET
  94. out.writeUTF(texto);// escreve a opcao para ser lida do outro lado
  95.  
  96. texto = "";
  97. input = new InputStreamReader(System.in);
  98. reader = new BufferedReader(input);
  99. System.out.println("Introduza nome da musica:");
  100.  
  101. texto = reader.readLine();// le o nome do ficheiro a enviar
  102.  
  103. out.writeUTF(texto);// escreve o nome do ficheiro do outro lado para sair correto no output
  104.  
  105. // sendfile
  106. File myFile = new File(texto);
  107. byte[] mybytearray = new byte[(int) myFile.length()];
  108.  
  109. FileInputStream fis = new FileInputStream(myFile);
  110. BufferedInputStream bis = new BufferedInputStream(fis);
  111. bis.read(mybytearray, 0, mybytearray.length);
  112.  
  113. OutputStream os = socket.getOutputStream();
  114.  
  115. os.write(mybytearray, 0, mybytearray.length);
  116.  
  117. os.flush();
  118.  
  119. System.out.println("Enviado!");
  120.  
  121. socket.close();
  122.  
  123. } else if (texto.equals("download")) {// aguarda pela chegada do ficheiro
  124. try {
  125. out.writeUTF(texto);// escreve a opcao para o outro lado saber
  126.  
  127. System.out.println("Nome do ficheiro que quer fazer download: ");
  128. texto = reader.readLine();
  129.  
  130. out.writeUTF(texto);// escreve o nome do ficheiro a fazer download para o outro lado
  131.  
  132. InputStream in2 = socket.getInputStream();
  133.  
  134. // escreve para o disco
  135. OutputStream output = new FileOutputStream(texto);
  136.  
  137. byte[] buffer = new byte[1024];
  138. while ((bytesRead = in2.read(buffer)) != -1) {
  139. output.write(buffer, 0, bytesRead);
  140. }
  141. // fecha o fileoutputstream
  142. output.close();
  143.  
  144. System.out.println("Recebido!");
  145. } catch (IOException e) {
  146. System.out.println("Exception " + e);
  147. }
  148. }
  149.  
  150. } catch (EOFException e) {
  151. System.out.println("EOF:" + e.getMessage());
  152. } catch (IOException e) {
  153. System.out.println("IO:" + e.getMessage());
  154. }
  155.  
  156. }
  157.  
  158. private static void printMenu() {
  159. System.out.println("Welcome to dropMusic");
  160. System.out.println("1- Login");
  161. System.out.println("2- Sign up");
  162. System.out.println("3- Adicionar musico");
  163. System.out.println("4- Remover musico");
  164. System.out.println("5- Pesquisar musico");
  165. System.out.println("6- Adicionar musica");
  166. System.out.println("7- Remover musica");
  167. System.out.println("8- Procurar musica");
  168. System.out.println("9- Adicionar album");
  169. System.out.println("10- Procurar album");
  170. System.out.println("11- Alterar descrição de album");
  171. System.out.println("12- Escrever critica a um album");
  172. System.out.println("13- Download de musica");
  173. System.out.println("14- Tornar utilizador editor");
  174. System.out.println("15- Cria/Adiciona à Playlist");
  175. System.out.println("16- Remover playlist");
  176. System.out.println("17- Associar musica a um album");
  177. System.out.println("18- Logout");
  178.  
  179. }
  180.  
  181. private static String menu() {
  182. int opt = 0;
  183. String msg = "";
  184. String name, mail, user, pass, historia, musicas, critica;
  185. String editora, letra, musico, descricao, genero, pont;
  186. String dia, mes, ano, minutos, hora;
  187. System.out.println();
  188. printMenu();
  189.  
  190. Scanner scan = new Scanner(System.in);
  191. try {
  192. opt = scan.nextInt();
  193. } catch (Exception e) {
  194. }
  195. switch (opt) {
  196. case 1:
  197. scan.nextLine();
  198. System.out.println("Username: ");
  199. user = scan.nextLine();
  200. System.out.println("Password: ");
  201. pass = scan.nextLine();
  202. msg = "type / login ; username / " + user + " ; password / " + pass;
  203. break;
  204.  
  205. case 2:
  206. scan.nextLine();
  207. System.out.println("Nome: ");
  208. name = scan.nextLine();
  209. System.out.println("Username: ");
  210. user = scan.nextLine();
  211. System.out.println("Password: ");
  212. pass = scan.nextLine();
  213. System.out.println("Email: ");
  214. mail = scan.nextLine();
  215. msg = "type / sign up ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / " + pass
  216. + " ; mail / " + mail;
  217. break;
  218. case 3:
  219. scan.nextLine();
  220. System.out.println("Nome: ");
  221. name = scan.nextLine();
  222. System.out.println("Username: ");
  223. user = scan.nextLine();
  224. System.out.println("Password: ");
  225. pass = scan.nextLine();
  226. System.out.println("Email: ");
  227. mail = scan.nextLine();
  228. msg = "type / add artist ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / "
  229. + pass + " ; mail / " + mail;
  230.  
  231. break;
  232.  
  233. case 4:
  234. scan.nextLine();
  235. System.out.println("Nome: ");
  236. name = scan.nextLine();
  237. msg = "type / remove_artist ; item_count / 3 ; name / " + name;
  238.  
  239. break;
  240. case 5:
  241. scan.nextLine();
  242. System.out.println("Name: ");
  243. name = scan.nextLine();
  244. msg = "type / search_artist ; item_count / 1 ; name / " + name;
  245.  
  246. break;
  247. case 6:
  248. scan.nextLine();
  249. System.out.println("Nome: ");
  250. name = scan.nextLine();
  251. System.out.println("Historia: ");
  252. historia = scan.nextLine();
  253. System.out.println("Editora: ");
  254. editora = scan.nextLine();
  255. System.out.println("Letra: ");
  256. letra = scan.nextLine();
  257. System.out.println("Musico: ");
  258. musico = scan.nextLine();
  259. System.out.println("Género: ");
  260. genero = scan.nextLine();
  261. msg = "type / add_music ; item_count / 5 ; name / " + name + " ; historia / " + historia + " ; editora / "
  262. + editora + " ; letra / " + letra + " ; musico / " + musico + " ; genero / " + genero;
  263.  
  264. break;
  265.  
  266. case 7:
  267. scan.nextLine();
  268. System.out.println("Nome: ");
  269. name = scan.nextLine();
  270. msg = "type / remove_music ; item_count / 1 ; name / " + name;
  271.  
  272. break;
  273.  
  274. case 8:
  275. scan.nextLine();
  276. System.out.println("Nome: ");
  277. name = scan.nextLine();
  278. msg = "type / search_music ; item_count / 1 ; name / " + name;
  279.  
  280. break;
  281.  
  282. case 9:
  283. scan.nextLine();
  284. System.out.println("Nome: ");
  285. name = scan.nextLine();
  286. System.out.println("Descrição: ");
  287. descricao = scan.nextLine();
  288. System.out.println("Género: ");
  289. genero = scan.nextLine();
  290. System.out.println("Data de lançamento");
  291. System.out.println("Dia:");
  292. dia = scan.nextLine();
  293. System.out.println("Mes:");
  294. mes = scan.nextLine();
  295. System.out.println("Ano");
  296. ano = scan.nextLine();
  297. System.out.println("Hora");
  298. hora = scan.nextLine();
  299. System.out.println("Minutos");
  300. minutos = scan.nextLine();
  301. msg = "type / add_album ; item_count / 8 ; name / " + name + " ; descricao / " + descricao + " ; genero / "
  302. + genero + " ; dia / " + dia + " ; mes / " + mes + " ; ano / " + ano + " ; hora / " + hora
  303. + " ; minuto / " + minutos;
  304.  
  305. break;
  306.  
  307. case 10:
  308. scan.nextLine();
  309. System.out.println("Nome: ");
  310. name = scan.nextLine();
  311. msg = "type / search_album ; item_count / 1 ; name / " + name;
  312.  
  313. break;
  314.  
  315. case 11:
  316. scan.nextLine();
  317. System.out.println("Nome: ");
  318. name = scan.nextLine();
  319. System.out.println("Nova descrição: ");
  320. descricao = scan.nextLine();
  321. msg = "type / change_desc_album ; item_count / 2 ; name / " + name + " ; descicao / " + descricao;
  322.  
  323. break;
  324.  
  325. case 12:
  326. scan.nextLine();
  327. System.out.println("Nome: ");
  328. name = scan.nextLine();
  329. System.out.println("Critica: ");
  330. critica = scan.nextLine();
  331. System.out.println("Pontuação: ");
  332. pont = scan.nextLine();
  333. msg = "type / write_crit ; item_count / 3 ; name / " + name + " ; critica / " + critica + " ; pontuacao / "
  334. + pont;
  335.  
  336. break;
  337.  
  338. case 13:
  339. scan.nextLine();
  340. System.out.println("Nome: ");
  341. name = scan.nextLine();
  342. msg = "type / download ; item_count / 1 ; name / " + name;
  343.  
  344. break;
  345.  
  346. case 14:
  347. scan.nextLine();
  348. System.out.println("Nome: ");
  349. name = scan.nextLine();
  350. msg = "type / make_admin ; item_count / 1 ; username / " + name;
  351.  
  352. break;
  353.  
  354. case 15:
  355. scan.nextLine();
  356. System.out.println("Nome: ");
  357. name = scan.nextLine();
  358. System.out.println("Nome da musica: ");
  359. musicas = scan.nextLine();
  360. System.out.println("Data de criação");
  361. System.out.println("Dia:");
  362. dia = scan.nextLine();
  363. System.out.println("Mes:");
  364. mes = scan.nextLine();
  365. System.out.println("Ano");
  366. ano = scan.nextLine();
  367. System.out.println("Hora");
  368. hora = scan.nextLine();
  369. System.out.println("Minutos");
  370. minutos = scan.nextLine();
  371. msg = "type / add_playlist ; item_count / 7 ; name / " + name + " ; musica / " + musicas + " ; dia / " + dia
  372. + " ; mes / " + mes + " ; ano / " + ano + " ; hora / " + hora + " ; minuto / " + minutos;
  373. break;
  374.  
  375. case 16:
  376. scan.nextLine();
  377. System.out.println("Nome: ");
  378. name = scan.nextLine();
  379. msg = "type / remove_playlist ; item_count / 1 ; name / " + name;
  380. break;
  381. case 17:
  382. scan.nextLine();
  383. System.out.println("Nome da muisca: ");
  384. musicas = scan.nextLine();
  385. System.out.println("Nome do album: ");
  386. name = scan.nextLine();
  387. msg = "type / music_to_album ; item_count / 2 ; musica / " + musicas + " ; album / " + name;
  388. break;
  389. case 18:
  390. scan.nextLine();
  391. System.out.println("Username: ");
  392. name = scan.nextLine();
  393. msg = "type / logout ; username / " + name;
  394. break;
  395. default:
  396. System.out.println("Invalid option");
  397. break;
  398.  
  399. }
  400. System.out.println(msg);
  401.  
  402. return msg;
  403.  
  404. }
  405.  
  406. public static void main(String[] args)
  407. throws MalformedURLException, RemoteException, NotBoundException, IOException {
  408.  
  409. String line = "";
  410.  
  411. System.getProperties().put("java.security.policy", "out/production/projeto2/policy.all");
  412.  
  413. ServerRmiInt si = null;
  414.  
  415. ClientRmi c = new ClientRmi();
  416.  
  417. c.setNome(args[0]);
  418.  
  419. si = (ServerRmiInt) Naming.lookup(nomeServer);
  420. si.subscribe(args[0], c);
  421.  
  422. System.out.println("Request sent to Server");
  423.  
  424. InputStreamReader input = new InputStreamReader(System.in);
  425. BufferedReader reader = new BufferedReader(input);
  426. // printMenu();
  427. // entra em ciclo para comunnicacao com o server rmi
  428. while (true) {
  429. try {
  430.  
  431. line = menu();
  432. si.sendMulti(line, args[0], c);
  433.  
  434. if (line.equals("type / share")) {// comando share para partilhar up/download
  435. uploadDownloadTCP();
  436. }
  437. } catch (java.rmi.ConnectException e) {// trata da falha no servidor principal para passar ao backup
  438. si = (ServerRmiInt) Naming.lookup(nomeServer);
  439. si.subscribe(args[0], c);
  440. System.out.println("aqui");
  441. si.sendMulti(line, args[0], c);
  442. } catch (RemoteException re) {
  443. System.out.println("Exception remote in main " + re);
  444. } catch (IOException e) {
  445. System.out.println("Exception in main " + e);
  446. }
  447. }
  448. }
  449.  
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement