Advertisement
Guest User

Untitled

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