Skip to content

Package: SynchronizeCommunication

SynchronizeCommunication

nameinstructionbranchcomplexitylinemethod
SynchronizeCommunication()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
connect(String, int)
M: 83 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
disconnect()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
pass()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
pass(String)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
receiveFile(String, String, String)
M: 95 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
sendCommand(String, boolean)
M: 140 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 33 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package update.remote.imond;
2:
3: import java.io.BufferedInputStream;
4: import java.io.BufferedOutputStream;
5: import java.io.File;
6: import java.io.FileInputStream;
7: import java.io.IOException;
8: import java.io.OutputStreamWriter;
9: import java.io.PrintWriter;
10: import java.net.InetSocketAddress;
11: import java.net.Socket;
12: import java.net.SocketAddress;
13: import java.nio.charset.Charset;
14:
15: import basic.PrinterConstants;
16:
17: /**
18: * This is the class for the connection to the imond.
19: *
20: * @author Patrick Kriegel
21: *
22: */
23: @SuppressWarnings("PMD.CyclomaticComplexity")
24: public class SynchronizeCommunication {
25:         /**
26:          * This attribute represents the socket for the connection.
27:          */
28:         private Socket socket = null;
29:         /**
30:          * This attribute represents the buffer for the input.
31:          */
32:         private BufferedInputStream bufferedInputStream = null;
33:         /**
34:          * This attribute represents the buffer for the outgoing data.
35:          */
36:         private BufferedOutputStream bufferedOutputStream = null;
37:         /**
38:          * This attribute represents a printer for the formatting representations of objects to a
39:          * text-output stream.
40:          */
41:         private PrintWriter printWriter = null;
42:
43:         /**
44:          * This method creates a socket for the communication to the router.
45:          *
46:          * @param server
47:          * this is the ip address of the router
48:          * @param port
49:          * this is the necessary port for the connection
50:          * @throws CommunicationException
51:          * If the connection failed.
52:          *
53:          */
54:         public void connect(final String server, final int port) throws CommunicationException {
55:
56:                 try {
57:•                        if (this.socket != null) {
58:                                 try {
59:                                         this.socket.close();
60:                                 } catch (final Exception e) {
61:                                         throw new CommunicationException(ImondConstants.EME_SOCKETCLOSED + e);
62:                                 } finally {
63:                                         this.socket = null;
64:                                 }
65:                         }
66:                         this.socket = new Socket();
67:                         final SocketAddress socketAddress = new InetSocketAddress(server, port);
68:                         this.socket.connect(socketAddress, ImondConstants.FIVETHOUSAND);
69:                         this.printWriter =
70:                                         new PrintWriter(
71:                                                         new OutputStreamWriter(this.socket.getOutputStream(), "UTF8"), true);
72:                         this.bufferedInputStream = new BufferedInputStream(this.socket.getInputStream());
73:                         this.bufferedOutputStream = new BufferedOutputStream(this.socket.getOutputStream());
74:
75:                 } catch (final Exception e) {
76:                         throw new CommunicationException(ImondConstants.EME_IPORPORTWRONG + e);
77:                 }
78:         }
79:
80:         /**
81:          * This method checks, if a password exists.
82:          *
83:          * @return command
84:          * @throws CommunicationException
85:          * if there are problems with the connection
86:          */
87:         public String pass() throws CommunicationException {
88:                 return sendCommand(ImondConstants.PASSCOMMAND, true);
89:         }
90:
91:         /**
92:          * This method sends the password to the imond and gets an answer if the password is correct or
93:          * gives the information, that the imond is in the admin mode.
94:          *
95:          * @param password
96:          * this is the entered password
97:          * @return command
98:          * @throws CommunicationException
99:          * if there are problems with the connection
100:          */
101:         public String pass(final String password) throws CommunicationException {
102:                 return sendCommand(ImondConstants.PASSSPACECOMMAND + password, true);
103:         }
104:
105:         /**
106:          * This method sends a command to the imond, if necessary: with the essential information. This
107:          * method receives an answer of the imond.
108:          *
109:          * @param command
110:          * this is the command for the imond
111:          * @param expectingAnswer
112:          * if an answer of the imond is expected, then he can wait 10 seconds
113:          * @return answer
114:          * @throws CommunicationException
115:          * if there are problems with the connection
116:          *
117:          */
118:         private synchronized String sendCommand(final String command, final boolean expectingAnswer)
119:                         throws CommunicationException {
120:•                if (this.socket == null) {
121:                         throw new CommunicationException(ImondConstants.EME_NOCONNECTIONTOROUTER);
122:                 }
123:
124:                 this.printWriter.println(command.trim());
125:
126:                 String answer = ImondConstants.EMPTYSTRING;
127:                 final StringBuffer ansBuf = new StringBuffer(ImondConstants.EMPTYSTRING);
128:
129:•                if (expectingAnswer) {
130:                         int n;
131:                         int count = 0;
132:
133:                         try {
134:•                                while (this.bufferedInputStream.available() == 0) {
135:                                         try {
136:                                                 wait(ImondConstants.ONEHUNDRED);
137:                                         } catch (final InterruptedException ire) {
138:                                                 throw new CommunicationException(ImondConstants.EME_ROUTERNOANSWER + ire);
139:                                         }
140:
141:•                                        if (count == ImondConstants.ONEHUNDRED) {
142:                                                 throw new CommunicationException(ImondConstants.EME_NOANSWEROFROUTER);
143:                                         }
144:                                         count++;
145:                                 }
146:•                                while ((this.bufferedInputStream.available()) > 0) {
147:                                         n = this.bufferedInputStream.available();
148:                                         final byte[] b = new byte[n];
149:                                         @SuppressWarnings("unused")
150:                                         final int result = this.bufferedInputStream.read(b); // ToDo @ Autor: Warum wird
151:                                                                                                                                                         // das Result
152:                                                                                                                                                         // gespeichert aber
153:                                                                                                                                                         // nicht weiter benutzt?
154:                                                                                                                                                         // Kann das weg?
155:                                         ansBuf.append(new String(b, Charset.forName(PrinterConstants.ENCODING)));
156:•                                        if (this.bufferedInputStream.available() == 0) {
157:                                                 try {
158:                                                         wait(ImondConstants.TEN);
159:                                                 } catch (final Exception e) {
160:                                                         throw new CommunicationException(ImondConstants.EME_ROUTERNOANSWER
161:                                                                         + e);
162:                                                 }
163:
164:                                         }
165:                                 }
166:                         } catch (final IOException ioe) {
167:                                 throw new CommunicationException(
168:                                                 ImondConstants.EME_COLLECTANSWEROFROUTERPROBLEMPARTONE + command
169:                                                                 + ImondConstants.EME_COLLECTANSWEROFROUTERPROBLEMPARTTWO + ioe);
170:                         }
171:                 }
172:                 answer = ansBuf.toString().trim();
173:•                if (ImondConstants.ERR.equals(answer)) {
174:                         throw new CommunicationException(ImondConstants.EME_ERRISTHEANSWER);
175:                 }
176:                 return answer;
177:         }
178:
179:         /**
180:          * This method disconnects the connection to imond.
181:          *
182:          * @throws CommunicationException
183:          * if there are problems with the connection
184:          */
185:         public void disconnect() throws CommunicationException {
186:•                if (this.socket != null) {
187:                         try {
188:                                 this.socket.close();
189:                         } catch (final Exception e) {
190:                                 throw new CommunicationException(ImondConstants.EME_DISCONNECTPROBLEM + e);
191:                         }
192:                 }
193:                 this.bufferedInputStream = null;
194:                 this.socket = null;
195:         }
196:
197:         /**
198:          * This method instructs imond to receive a file.
199:          *
200:          * @param filename
201:          * path and name of file to save
202:          * @param buildPath
203:          * path of the location of the file on the fli4l and the filename
204:          * @param pass
205:          * password
206:          * @throws CommunicationException
207:          * Exception, if there are problems with sending the data.
208:          * @throws IOException
209:          * Exception, if there are problems with reading files.
210:          */
211:         public void receiveFile(final String filename, final String buildPath, final String pass)
212:                         throws CommunicationException, IOException {
213:
214:                 final File file = new File(buildPath);
215:                 this.sendCommand(ImondConstants.RECEIVE + filename + ImondConstants.SPACE + file.length()
216:                                 + ImondConstants.SPACE + pass + ImondConstants.FORRECEIVECOMMAND, true);
217:
218:                 final FileInputStream fis = new FileInputStream(buildPath);
219:                 String answer = ImondConstants.EMPTYSTRING;
220:                 final byte[] buf = new byte[ImondConstants.NUMBER_1024];
221:                 final StringBuffer ansBuf = new StringBuffer(ImondConstants.EMPTYSTRING);
222:                 while (true) {
223:                         final int len = fis.read(buf, 0, buf.length);
224:•                        if (len <= 0) {
225:                                 fis.close();
226:                                 break;
227:                         }
228:                         this.bufferedOutputStream.write(buf, 0, len);
229:                         final byte[] b = new byte[this.bufferedInputStream.available()];
230:                         ansBuf.append(new String(b, Charset.forName(PrinterConstants.ENCODING)));
231:                         answer = ansBuf.toString().trim();
232:•                        if (ImondConstants.NAK.equals(answer)) {
233:                                 fis.close();
234:                                 throw new CommunicationException(ImondConstants.EME_RECEIVEFILEPROBLEM);
235:                         }
236:                 }
237:                 fis.close();
238:
239:         }
240:
241: }