TD2: Simplify the object serialization trough socket.
[TD_SR.git] / TD2 / IHM / SocketClient.java
1 import java.io.*;
2 import java.net.*;
3 import java.util.*;
4
5 public class SocketClient {
6 BufferedReader lecture; // pour le flot d'entrée venant du serveur
7 PrintWriter ecriture; // pour le flot de sortie vers le serveur
8 ObjectInput oLecture;
9 ObjectOutput oEcriture;
10 Socket sock; // le socket client
11
12 public SocketClient() {
13 // établie une connexion au serveur par un appel
14 // à connexionServeur()
15 attributesInit();
16 try {
17 connexionServeur("localhost", 5000);
18 }
19 catch (IOException e) {
20 System.err.println("IOException: " + e);
21 e.printStackTrace();
22 closeRWIO();
23 }
24 }
25
26 public SocketClient(String adresseIPServeur, int portServeur) {
27 // établie une connexion au serveur par un appel
28 // à connexionServeur()
29 attributesInit();
30 try {
31 connexionServeur(adresseIPServeur, portServeur);
32 }
33 catch (IOException e) {
34 System.err.println("IOException: " + e);
35 e.printStackTrace();
36 closeRWIO();
37 }
38 }
39
40 public SocketClient(boolean withoStream) {
41 // établie une connexion au serveur par un appel
42 // à connexionServeur()
43 attributesInit();
44 try {
45 connexionServeur("localhost", 5000, withoStream);
46 }
47 catch (IOException e) {
48 System.err.println("IOException: " + e);
49 e.printStackTrace();
50 closeRWIO();
51 }
52 }
53
54 private void connexionServeur(String adresseIPServeur, int portServeur, boolean hasoStream) throws IOException {
55 // créer un objet socket lié au socket serveur et l'affecte à sock
56 // puis établie les chaînages de flot nécessaires
57 // pour l'envoi et la reception de messages
58 sock = new Socket(adresseIPServeur, portServeur);
59
60 OutputStream OStream = sock.getOutputStream();
61 ecriture = new PrintWriter(OStream);
62 if (hasoStream)
63 oEcriture = new ObjectOutputStream(OStream);
64
65 InputStream IStream = sock.getInputStream();
66 InputStreamReader IMesg = new InputStreamReader(IStream);
67 lecture = new BufferedReader(IMesg);
68 if (hasoStream)
69 oLecture = new ObjectInputStream(IStream);
70 }
71
72 private void connexionServeur(String adresseIPServeur, int portServeur) throws IOException {
73 // créer un objet socket lié au socket serveur et l'affecte à sock
74 // puis établie les chaînages de flot nécessaires
75 // pour l'envoi et la reception de messages
76 connexionServeur(adresseIPServeur, portServeur, false);
77 }
78
79 private void attributesInit() {
80 sock = null;
81 lecture = null;
82 ecriture = null;
83 oLecture = null;
84 oEcriture = null;
85 }
86
87 /**
88 * Send a message on the opened client socket
89 * @param msg a string containing the message to send
90 */
91 public void sendMsg(String msg) {
92 ecriture.println(msg);
93 ecriture.flush();
94 }
95
96 /**
97 * Send an object message on the opened client socket
98 * @param msg a string containing the message to send
99 */
100 public void sendoMsg(Message oMsg) throws IOException {
101 oEcriture.writeObject(oMsg);
102 oEcriture.flush();
103 }
104
105 /**
106 * Receive a message sent on the opened client socket
107 * @return a string containing the received message
108 */
109 public String receiveMsg() throws IOException {
110 String line = new String();
111 //FIXME?: read only the line before the ending newline
112 line = lecture.readLine();
113 return line;
114 }
115
116 /**
117 * Receive an object message sent on the opened client socket
118 * @return a string containing the received message
119 */
120 public Message receiveoMsg() throws IOException, ClassNotFoundException {
121 return (Message)oLecture.readObject();
122 }
123
124 /**
125 * Close all opened I/O streams attached to this object instance
126 */
127 public void closeRWIO() {
128 try {
129 if (sock != null)
130 sock.close();
131 if (lecture != null)
132 lecture.close();
133 if (ecriture != null)
134 ecriture.close();
135 if (oLecture != null)
136 oLecture.close();
137 if (oEcriture != null) {
138 oEcriture.close();
139 }
140 }
141 catch (IOException e) {
142 System.err.println("IOException: " + e);
143 e.printStackTrace();
144 }
145 }
146
147 } // fin classe SocketClient