· 8 years ago · Dec 19, 2017, 06:40 PM
1package client;
2
3import com.google.gson.JsonElement;
4import com.google.gson.JsonObject;
5import com.google.gson.JsonParser;
6import com.google.gson.stream.JsonReader;
7import java.io.DataInputStream;
8import java.io.FileNotFoundException;
9import java.io.FileOutputStream;
10import java.io.InputStreamReader;
11import java.net.Socket;
12
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.OutputStream;
16import java.nio.charset.StandardCharsets;
17import java.security.KeyPairGenerator;
18import java.security.NoSuchAlgorithmException;
19import java.security.Security;
20import java.util.Scanner;
21import javax.crypto.KeyGenerator;
22import javax.crypto.SecretKey;
23
24public class Client {
25
26 JsonReader in;
27 OutputStream out;
28 Socket s;
29 SecretKey key;
30
31
32 public void aplicacao() throws IOException {
33
34 //CONEXÃO AO SERVIDOR
35 s = new Socket("localhost", 2000);
36 in = new JsonReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
37 out = s.getOutputStream();
38
39 //DECLARAÇÃO DE VARIAVEIS
40 String msg;
41 String msgDestinatario;
42 String copiaMsg = "";
43 String AssinaturaMsg;
44 String idMsg;
45 int uuid;
46 int id;
47 int idDestinatario;
48 int tipo;
49
50 do {
51
52 //MENU
53 System.out.println("0 - exit");
54 System.out.println("1 - List");
55 System.out.println("2 - New");
56 System.out.println("3 - Create");
57 System.out.println("4 - All");
58 System.out.println("5 - Send");
59 System.out.println("6 - Recv");
60 System.out.println("7 - Receipt");
61 System.out.println("8 - Status");
62 Scanner sc = new Scanner(System.in);
63 tipo = sc.nextInt();
64
65 switch (tipo) {
66 case 1: //LIST
67 System.out.println("Introduza o id do utilizador");
68 Scanner sc1 = new Scanner(System.in);
69 id = sc1.nextInt();
70 msg = ("\"type\":\"list\",\"id\":\"" + id + "\"");
71 sendResult(msg, null);
72 break;
73
74 case 2: //NEW
75 System.out.println("Introduza o id do utilizador");
76 Scanner sc2 = new Scanner(System.in);
77 id = sc2.nextInt();
78 msg = ("\"type\":\"new\",\"id\":\"" + id + "\"");
79 sendResult(msg, null);
80 break;
81
82 case 3: //CREATE
83 System.out.println("Introduza o id do novo utilizador");
84 Scanner sc3 = new Scanner(System.in);
85 uuid = sc3.nextInt();
86 msg = ("\"type\":\"create\",\"uuid\":\"" + uuid + "\"");
87 sendResult(msg, null);
88 break;
89
90 case 4: //ALL
91 System.out.println("Introduza o id para Listar todas as mensagens");
92 Scanner sc4 = new Scanner(System.in);
93 id = sc4.nextInt();
94 msg = ("\"type\":\"all\",\"id\":\"" + id + "\"");
95 sendResult(msg, null);
96 break;
97
98 case 5: //SEND
99 System.out.println("Introduza o seu id de utilizador");
100 Scanner sc5 = new Scanner(System.in);
101 id = sc5.nextInt();
102 System.out.println("Introduza o id do Destinatario da mensagem");
103 Scanner sc5_1 = new Scanner(System.in);
104 idDestinatario = sc5_1.nextInt();
105 System.out.println("Escreva a sua mensagem");
106 Scanner sc5_2 = new Scanner(System.in);
107 msgDestinatario = sc5_2.nextLine();
108 copiaMsg = msgDestinatario;
109 msg = ("\"type\":\"send\",\"src\":\"" + id + "\",\"dst\":\"" + idDestinatario + "\",\"msg\":\"" + msgDestinatario + "\",\"copy\":\"" + copiaMsg + "\"");
110 sendResult(msg, null);
111 break;
112
113 case 6: //RECV
114 System.out.println("Introduza o id");
115 Scanner sc6 = new Scanner(System.in);
116 id = sc6.nextInt();
117 System.out.println("Introduza o id da Mensagem");
118 Scanner sc6_1 = new Scanner(System.in);
119 idMsg = sc6_1.nextLine();
120 msg = ("\"type\":\"recv\",\"id\":\"" + id + "\",\"msg\":\"" + idMsg + "\"");
121 sendResult(msg, null);
122 break;
123
124 case 7: //RECEIPT
125 System.out.println("Introduza o id");
126 Scanner sc7 = new Scanner(System.in);
127 id = sc7.nextInt();
128 System.out.println("Introduza o id da mensagem");
129 Scanner sc7_1 = new Scanner(System.in);
130 idMsg = sc7_1.nextLine();
131 System.out.println("Introduza a assinatura da msg");
132 Scanner sc7_2 = new Scanner(System.in);
133 AssinaturaMsg = sc7_2.nextLine();
134 msg = ("\"type\":\"receipt\",\"id\":\"" + id + "\",\"msg\":\"" + idMsg + "\",\"receipt\":\"" + AssinaturaMsg + "");
135 sendResult(msg, null);
136 break;
137
138 case 8: //STATUS
139 System.out.println("Introduza o id");
140 Scanner sc8 = new Scanner(System.in);
141 id = sc8.nextInt();
142 System.out.println("Introduza o id da mensagem enviada");
143 Scanner sc8_1 = new Scanner(System.in);
144 idMsg = sc8_1.nextLine();
145 msg = ("\"type\":\"status\",\"id\":\"" + id + "\",\"msg\"" + idMsg + "");
146 sendResult(msg, null);
147 break;
148
149 default:
150 System.out.println("Por favor introduza uma opcao existente");
151 break;
152 }
153 } while (tipo != 0);
154 s.close();
155 }
156
157 //MENSAGEM QUE ENVIA AO SERVIDOR
158 void sendResult(String result, String error) {
159 String msg = "{";
160 // Usefull result
161 if (result != null) {
162 msg += result;
163 }
164 // error message
165 if (error != null) {
166 msg += "\"error\":" + error;
167 }
168 msg += "}\n";
169 try {
170 System.out.print("Envio ao servidor: " + msg);
171 out.write(msg.getBytes(StandardCharsets.UTF_8));
172 readResult();
173 } catch (Exception e) {
174 }
175 }
176
177 //MENSAGEM QUE RECEBE DO SERVIDOR
178 JsonObject readResult() {
179 try {
180 JsonElement data = new JsonParser().parse(in);
181 if (data.isJsonObject()) {
182 System.out.println("Resultado do servidor: " + data.getAsJsonObject());
183 return data.getAsJsonObject();
184 }
185 System.err.print("Nenhum resultado do servidor\n");
186 return null;
187 } catch (Exception e) {
188 System.err.print("Error while reading JSON command from socket, connection will be shutdown\n");
189 return null;
190 }
191
192 }
193
194 //GERACAO DE CHAVE DE SESSAO
195 public void sessionKey() throws NoSuchAlgorithmException, FileNotFoundException, IOException{
196
197
198 //Gerar Key
199 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
200 key = keyGen.generateKey();
201
202 //Guardar Key num ficheiro
203// FileOutputStream fos = new FileOutputStream(this.filename);
204// byte[] b = key.getEncoded();
205// fos.write(b);
206// fos.close();
207 }
208
209
210 /**
211 * APLICACAO
212 */
213 public static void main(String[] args) throws Exception {
214 Client client = new Client();
215 client.aplicacao();
216 }
217
218}