Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.61 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- Logout");
  175.  
  176. }
  177.  
  178. private static String menu() {
  179. int opt = 0;
  180. String msg = "";
  181. String name, mail, user, pass, historia, musicas, critica;
  182. String editora, letra, musico, descricao, genero, pont;
  183. String dia, mes, ano, minutos, hora;
  184. System.out.println();
  185. printMenu();
  186.  
  187. Scanner scan = new Scanner(System.in);
  188. try {
  189. opt = scan.nextInt();
  190. } catch (Exception e) {
  191. }
  192. switch (opt) {
  193. case 1:
  194. scan.nextLine();
  195. System.out.println("Username: ");
  196. user = scan.nextLine();
  197. System.out.println("Password: ");
  198. pass = scan.nextLine();
  199. msg = "type / login ; username / " + user + " ; password / " + pass;
  200. break;
  201.  
  202. case 2:
  203. scan.nextLine();
  204. System.out.println("Nome: ");
  205. name = scan.nextLine();
  206. System.out.println("Username: ");
  207. user = scan.nextLine();
  208. System.out.println("Password: ");
  209. pass = scan.nextLine();
  210. System.out.println("Email: ");
  211. mail = scan.nextLine();
  212. msg = "type / sign up ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / " + pass
  213. + " ; mail / " + mail;
  214. break;
  215. case 3:
  216. scan.nextLine();
  217. System.out.println("Nome: ");
  218. name = scan.nextLine();
  219. System.out.println("Username: ");
  220. user = scan.nextLine();
  221. System.out.println("Password: ");
  222. pass = scan.nextLine();
  223. System.out.println("Email: ");
  224. mail = scan.nextLine();
  225. msg = "type / add artist ; item_count / 3 ; name / " + name + " ; username / " + user + " ; password / "
  226. + pass + " ; mail / " + mail;
  227.  
  228. break;
  229.  
  230. case 4:
  231. scan.nextLine();
  232. System.out.println("Nome: ");
  233. name = scan.nextLine();
  234. msg = "type / remove_artist ; item_count / 3 ; name / " + name;
  235.  
  236. break;
  237. case 5:
  238. scan.nextLine();
  239. System.out.println("Name: ");
  240. name = scan.nextLine();
  241. msg = "type / search_artist ; item_count / 1 ; name / " + name;
  242.  
  243. break;
  244. case 6:
  245. scan.nextLine();
  246. System.out.println("Nome: ");
  247. name = scan.nextLine();
  248. System.out.println("Historia: ");
  249. historia = scan.nextLine();
  250. System.out.println("Editora: ");
  251. editora = scan.nextLine();
  252. System.out.println("Letra: ");
  253. letra = scan.nextLine();
  254. System.out.println("Musico: ");
  255. musico = scan.nextLine();
  256. System.out.println("Género: ");
  257. genero = scan.nextLine();
  258. msg = "type / add_music ; item_count / 5 ; name / " + name + " ; historia / " + historia + " ; editora / "
  259. + editora + " ; letra / " + letra + " ; musico / " + musico + " ; genero / " + genero;
  260.  
  261. break;
  262.  
  263. case 7:
  264. scan.nextLine();
  265. System.out.println("Nome: ");
  266. name = scan.nextLine();
  267. msg = "type / remove_music ; item_count / 1 ; name / " + name;
  268.  
  269. break;
  270.  
  271. case 8:
  272. scan.nextLine();
  273. System.out.println("Nome: ");
  274. name = scan.nextLine();
  275. msg = "type / search_music ; item_count / 1 ; name / " + name;
  276.  
  277. break;
  278.  
  279. case 9:
  280. scan.nextLine();
  281. System.out.println("Nome: ");
  282. name = scan.nextLine();
  283. System.out.println("Descrição: ");
  284. descricao = scan.nextLine();
  285. System.out.println("Género: ");
  286. genero = scan.nextLine();
  287. System.out.println("Data de lançamento");
  288. System.out.println("Dia:");
  289. dia=scan.nextLine();
  290. System.out.println("Mes:");
  291. mes=scan.nextLine();
  292. System.out.println("Ano");
  293. ano=scan.nextLine();
  294. System.out.println("Hora");
  295. hora=scan.nextLine();
  296. System.out.println("Minutos");
  297. minutos=scan.nextLine();
  298. msg = "type / add_album ; item_count / 8 ; name / " + name +" ; descricao / "
  299. + descricao +" ; genero / " + genero + " ; dia / "+dia+" ; mes / "+mes+" ; ano / "+ano+" ; hora / "+hora+" ; minuto / "+minutos;
  300.  
  301. break;
  302.  
  303. case 10:
  304. scan.nextLine();
  305. System.out.println("Nome: ");
  306. name = scan.nextLine();
  307. msg = "type / search_album ; item_count / 1 ; name / " + name;
  308.  
  309. break;
  310.  
  311. case 11:
  312. scan.nextLine();
  313. System.out.println("Nome: ");
  314. name = scan.nextLine();
  315. System.out.println("Nova descrição: ");
  316. descricao = scan.nextLine();
  317. msg = "type / change_desc_album ; item_count / 2 ; name / " + name + " ; descicao / " + descricao;
  318.  
  319. break;
  320.  
  321. case 12:
  322. scan.nextLine();
  323. System.out.println("Nome: ");
  324. name = scan.nextLine();
  325. System.out.println("Critica: ");
  326. critica = scan.nextLine();
  327. System.out.println("Pontuação: ");
  328. pont = scan.nextLine();
  329. msg = "type / write_crit ; item_count / 3 ; name / " + name + " ; critica / " + critica
  330. + " ; pontuacao / " + pont;
  331.  
  332. break;
  333.  
  334. case 13:
  335. scan.nextLine();
  336. System.out.println("Nome: ");
  337. name = scan.nextLine();
  338. msg = "type / download ; item_count / 1 ; name / " + name;
  339.  
  340. break;
  341.  
  342. case 14:
  343. scan.nextLine();
  344. System.out.println("Nome: ");
  345. name = scan.nextLine();
  346. msg = "type / download ; item_count / 1 ; username / " + name;
  347.  
  348. break;
  349.  
  350. case 15:
  351. scan.nextLine();
  352. System.out.println("Username: ");
  353. name = scan.nextLine();
  354. msg = "type / logout ; item_count / 1 ; username / " + name;
  355.  
  356. break;
  357. default:
  358. System.out.println("Invalid option");
  359. break;
  360.  
  361. }
  362. System.out.println(msg);
  363.  
  364. return msg;
  365.  
  366. }
  367.  
  368. public static void main(String[] args)
  369. throws MalformedURLException, RemoteException, NotBoundException, IOException {
  370.  
  371. String line = "";
  372.  
  373. System.getProperties().put("java.security.policy", "out/production/projeto2/policy.all");
  374.  
  375. ServerRmiInt si = null;
  376.  
  377. ClientRmi c = new ClientRmi();
  378.  
  379. c.setNome(args[0]);
  380.  
  381. si = (ServerRmiInt) Naming.lookup(nomeServer);
  382. si.subscribe(args[0], c);
  383.  
  384. System.out.println("Request sent to Server");
  385.  
  386. InputStreamReader input = new InputStreamReader(System.in);
  387. BufferedReader reader = new BufferedReader(input);
  388. //printMenu();
  389. // entra em ciclo para comunnicacao com o server rmi
  390. while (true) {
  391. try {
  392.  
  393. line = menu();
  394. si.sendMulti(line, args[0], c);
  395.  
  396. if (line.equals("type / share")) {// comando share para partilhar up/download
  397. uploadDownloadTCP();
  398. }
  399. } catch (java.rmi.ConnectException e) {// trata da falha no servidor principal para passar ao backup
  400. si = (ServerRmiInt) Naming.lookup(nomeServer);
  401. si.subscribe(args[0], c);
  402. System.out.println("aqui");
  403. si.sendMulti(line, args[0], c);
  404. } catch (RemoteException re) {
  405. System.out.println("Exception remote in main " + re);
  406. } catch (IOException e) {
  407. System.out.println("Exception in main " + e);
  408. }
  409. }
  410. }
  411.  
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement