4040import java .net .SocketException ;
4141import java .util .List ;
4242
43+ import static org .monora .uprotocol .core .spec .alpha .Config .PORT_UPROTOCOL ;
44+ import static org .monora .uprotocol .core .spec .alpha .Config .TIMEOUT_SOCKET_DEFAULT ;
45+
4346/**
4447 * created by: Veli
4548 * date: 11.02.2018 15:07
@@ -49,12 +52,6 @@ public class CommunicationBridge implements Closeable
4952{
5053 public static final String TAG = CommunicationBridge .class .getSimpleName ();
5154
52- public static final int TIMEOUT_SOCKET_DEFAULT = 5000 ;
53-
54- public static final int PORT_UPROTOCOL = 1128 ;
55-
56- public static final int LENGTH_DEVICE_NAME = 32 ;
57-
5855
5956 private final PersistenceProvider persistenceProvider ;
6057
@@ -103,15 +100,15 @@ public static CommunicationBridge connect(ConnectionProvider connectionProvider,
103100 throws IOException , JSONException , CommunicationException
104101 {
105102 ActiveConnection activeConnection = connectionProvider .openConnection (deviceAddress .inetAddress );
106- String remoteDeviceId = activeConnection .receive ().getAsString ();
103+ String remoteDeviceUid = activeConnection .receive ().getAsString ();
107104
108- if (device != null && device .uid != null && !device .uid .equals (remoteDeviceId )) {
105+ if (device != null && device .uid != null && !device .uid .equals (remoteDeviceUid )) {
109106 activeConnection .closeSafely ();
110- throw new DifferentClientException (device , remoteDeviceId );
107+ throw new DifferentClientException (device , remoteDeviceUid );
111108 }
112109
113110 if (device == null )
114- device = persistenceProvider .createDeviceFor (remoteDeviceId );
111+ device = persistenceProvider .createDeviceFor (remoteDeviceUid );
115112
116113 try {
117114 persistenceProvider .sync (device );
@@ -160,12 +157,15 @@ public static ActiveConnection openConnection(InetAddress inetAddress) throws IO
160157 * request in that timespan, this will invoke {@link TransportSeat#handleAcquaintanceRequest(Device, DeviceAddress)}
161158 * method on the remote and it will choose you.
162159 *
163- * @throws JSONException If something goes wrong when creating the JSON object.
164- * @throws IOException If an IO error occurs.
160+ * @return True if successful.
161+ * @throws JSONException If something goes wrong when creating JSON object.
162+ * @throws IOException If an IO error occurs.
163+ * @throws CommunicationException When there is a communication error due to misconfiguration.
165164 */
166- public void requestAcquaintance () throws JSONException , IOException
165+ public boolean requestAcquaintance () throws JSONException , IOException , CommunicationException
167166 {
168167 getActiveConnection ().reply (new JSONObject ().put (Keyword .REQUEST , Keyword .REQUEST_ACQUAINTANCE ));
168+ return receiveResult ();
169169 }
170170
171171 /**
@@ -178,48 +178,77 @@ public void requestAcquaintance() throws JSONException, IOException
178178 *
179179 * @param transferId That ties a group of {@link TransferItem} as in {@link TransferItem#transferId}.
180180 * @param files That has been generated using {@link PersistenceProvider#toJson(List)}.
181- * @throws JSONException If something goes wrong when creating the JSON object.
182- * @throws IOException If an IO error occurs.
181+ * @return True if successful.
182+ * @throws JSONException If something goes wrong when creating JSON object.
183+ * @throws IOException If an IO error occurs.
184+ * @throws CommunicationException When there is a communication error due to misconfiguration.
183185 */
184- public void requestFileTransfer (long transferId , JSONArray files ) throws JSONException , IOException
186+ public boolean requestFileTransfer (long transferId , JSONArray files ) throws JSONException , IOException ,
187+ CommunicationException
185188 {
186189 getActiveConnection ().reply (new JSONObject ()
187190 .put (Keyword .REQUEST , Keyword .REQUEST_TRANSFER )
188191 .put (Keyword .TRANSFER_ID , transferId )
189192 .put (Keyword .INDEX , files ));
193+ return receiveResult ();
190194 }
191195
192196 /**
193- * Ask the remote to start the transfer job.
197+ * Ask remote to start a transfer job.
194198 * <p>
195- * The transfer request has already been sent {@link #requestFileTransfer(long, JSONArray)}.
199+ * The transfer request, in this case, has already been sent {@link #requestFileTransfer(long, JSONArray)}.
196200 *
197201 * @param transferId That ties a group of {@link TransferItem} as in {@link TransferItem#transferId}.
198202 * @param type Of the transfer as in {@link TransferItem#type}.
199- * @throws JSONException If something goes wrong when creating the JSON object.
200- * @throws IOException If an IO error occurs.
203+ * @return True if successful.
204+ * @throws JSONException If something goes wrong when creating JSON object.
205+ * @throws IOException If an IO error occurs.
206+ * @throws CommunicationException When there is a communication error due to misconfiguration.
201207 */
202- public void requestFileTransferStart (long transferId , TransferItem .Type type ) throws JSONException , IOException
208+ public boolean requestFileTransferStart (long transferId , TransferItem .Type type ) throws JSONException , IOException ,
209+ CommunicationException
203210 {
204211 getActiveConnection ().reply (new JSONObject ()
205212 .put (Keyword .REQUEST , Keyword .REQUEST_TRANSFER_JOB )
206213 .put (Keyword .TRANSFER_ID , transferId )
207214 .put (Keyword .TRANSFER_TYPE , type ));
215+ return receiveResult ();
208216 }
209217
210- public void requestNotifyTransferState (long transferId , boolean accepted ) throws JSONException , IOException
218+ /**
219+ * Inform remote about the state of a transfer request it sent previously.
220+ *
221+ * @param transferId The transfer id that you are informing about.
222+ * @param accepted True if the transfer request was accepted.
223+ * @return True if the request was processed successfully.
224+ * @throws JSONException If something goes wrong when creating JSON object.
225+ * @throws IOException If an IO error occurs.
226+ * @throws CommunicationException When there is a communication error due to misconfiguration.
227+ */
228+ public boolean requestNotifyTransferState (long transferId , boolean accepted ) throws JSONException , IOException ,
229+ CommunicationException
211230 {
212231 getActiveConnection ().reply (new JSONObject ()
213232 .put (Keyword .REQUEST , Keyword .REQUEST_NOTIFY_TRANSFER_STATE )
214233 .put (Keyword .TRANSFER_ID , transferId )
215234 .put (Keyword .TRANSFER_IS_ACCEPTED , accepted ));
235+ return receiveResult ();
216236 }
217237
218- public void requestTextTransfer (String text ) throws JSONException , IOException
238+ /**
239+ * Request a text transfer.
240+ *
241+ * @param text To send.
242+ * @throws JSONException If something goes wrong when creating JSON object.
243+ * @throws IOException If an IO error occurs.
244+ * @throws CommunicationException When there is a communication error due to misconfiguration.
245+ */
246+ public boolean requestTextTransfer (String text ) throws JSONException , IOException , CommunicationException
219247 {
220248 getActiveConnection ().reply (new JSONObject ()
221249 .put (Keyword .REQUEST , Keyword .REQUEST_TRANSFER_TEXT )
222250 .put (Keyword .TRANSFER_TEXT , text ));
251+ return receiveResult ();
223252 }
224253
225254 public static JSONObject receiveSecure (ActiveConnection activeConnection , Device device ) throws IOException ,
0 commit comments