Skip to content

Commit 259fbc5

Browse files
committed
Write the first test case
This includes dummy classes and other small changes in the uprotocol API
1 parent 26fdb59 commit 259fbc5

20 files changed

+551
-82
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/target
2+
/.idea
3+
/projectFilesBackup
4+
*.iml
5+
local.properties
6+
*.jar
7+
settings.xml

src/main/java/org/monora/uprotocol/core/CommunicationBridge.java

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
import java.net.SocketException;
4141
import 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,

src/main/java/org/monora/uprotocol/core/DeviceLoader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
import org.monora.uprotocol.core.network.DeviceAddress;
77
import org.monora.uprotocol.core.persistence.PersistenceException;
88
import org.monora.uprotocol.core.persistence.PersistenceProvider;
9-
import org.monora.uprotocol.core.protocol.ConnectionProvider;
10-
import org.monora.uprotocol.core.protocol.DeviceBlockedException;
11-
import org.monora.uprotocol.core.protocol.DeviceInsecureException;
12-
import org.monora.uprotocol.core.protocol.DeviceVerificationException;
9+
import org.monora.uprotocol.core.protocol.*;
1310
import org.monora.uprotocol.core.spec.alpha.Keyword;
1411

1512
import java.net.InetAddress;
1613
import java.util.Base64;
1714

15+
import static org.monora.uprotocol.core.spec.alpha.Config.LENGTH_DEVICE_USERNAME;
16+
1817
public class DeviceLoader
1918
{
2019
public static void loadAsClient(PersistenceProvider persistenceProvider, JSONObject object, Device device)
@@ -64,14 +63,15 @@ private static void loadFrom(PersistenceProvider persistenceProvider, JSONObject
6463
device.brand = object.getString(Keyword.DEVICE_BRAND);
6564
device.model = object.getString(Keyword.DEVICE_MODEL);
6665
device.username = object.getString(Keyword.DEVICE_USERNAME);
66+
device.clientType = object.getEnum(ClientType.class, Keyword.DEVICE_CLIENT_TYPE);
6767
device.lastUsageTime = System.currentTimeMillis();
6868
device.versionCode = object.getInt(Keyword.DEVICE_VERSION_CODE);
6969
device.versionName = object.getString(Keyword.DEVICE_VERSION_NAME);
7070
device.protocolVersion = object.getInt(Keyword.DEVICE_PROTOCOL_VERSION);
7171
device.protocolVersionMin = object.getInt(Keyword.DEVICE_PROTOCOL_VERSION_MIN);
7272

73-
if (device.username.length() > CommunicationBridge.LENGTH_DEVICE_NAME)
74-
device.username = device.username.substring(0, CommunicationBridge.LENGTH_DEVICE_NAME);
73+
if (device.username.length() > LENGTH_DEVICE_USERNAME)
74+
device.username = device.username.substring(0, LENGTH_DEVICE_USERNAME);
7575

7676
persistenceProvider.save(device);
7777

src/main/java/org/monora/uprotocol/core/TransportSession.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class TransportSession extends CoolSocket
2424

2525
public TransportSession(PersistenceProvider persistenceProvider, TransportSeat transportSeat)
2626
{
27-
super(Config.SERVER_PORT_COMMUNICATION);
27+
super(Config.PORT_UPROTOCOL);
2828

29-
getConfigFactory().setReadTimeout(CommunicationBridge.TIMEOUT_SOCKET_DEFAULT);
29+
getConfigFactory().setReadTimeout(Config.TIMEOUT_SOCKET_DEFAULT);
3030
this.persistenceProvider = persistenceProvider;
3131
this.transportSeat = transportSeat;
3232
}

src/main/java/org/monora/uprotocol/core/network/Device.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.monora.uprotocol.core.network;
22

3+
import org.monora.uprotocol.core.protocol.ClientType;
4+
35
/**
46
* A device is a representation of a client using the latest information it has provided in a previous communication.
57
*/
@@ -20,11 +22,6 @@ public abstract class Device
2022
*/
2123
public String versionName;
2224

23-
/**
24-
* The OS that the client is running on.
25-
*/
26-
public String clientOs;
27-
2825
/**
2926
* The brand/manufacturer of the device.
3027
*/
@@ -35,6 +32,11 @@ public abstract class Device
3532
*/
3633
public String model;
3734

35+
/**
36+
* The client type.
37+
*/
38+
public ClientType clientType;
39+
3840
/**
3941
* The key that we will send to the remote when we are the one who is initiating the communication.
4042
*
@@ -78,7 +80,7 @@ public abstract class Device
7880
public boolean isTrusted;
7981

8082
/**
81-
* This represents whether this devices is blocked on this client. When devices are blocked they
83+
* This represents whether this device is blocked on this client. When devices are blocked they
8284
* cannot access unless the user for this client unblocks it.
8385
*
8486
* @see #isTrusted
@@ -89,4 +91,35 @@ public abstract class Device
8991
* Determine whether this device instance belongs to this client (us).
9092
*/
9193
public boolean isLocal;
94+
95+
@Override
96+
public boolean equals(Object obj)
97+
{
98+
if (obj instanceof Device) {
99+
return uid != null && uid.equals(((Device) obj).uid);
100+
}
101+
return super.equals(obj);
102+
}
103+
104+
public void from(Device device)
105+
{
106+
from(device.username, device.senderKey, device.receiverKey, device.brand, device.model, device.clientType,
107+
device.versionName, device.versionCode, device.protocolVersion, device.protocolVersionMin);
108+
}
109+
110+
protected void from(String username, int senderKey, int receiverKey, String brand, String model,
111+
ClientType clientType, String versionName, int versionCode, int protocolVersion,
112+
int protocolVersionMin)
113+
{
114+
this.username = username;
115+
this.senderKey = senderKey;
116+
this.receiverKey = receiverKey;
117+
this.brand = brand;
118+
this.model = model;
119+
this.clientType = clientType;
120+
this.versionName = versionName;
121+
this.versionCode = versionCode;
122+
this.protocolVersion = protocolVersion;
123+
this.protocolVersionMin = protocolVersionMin;
124+
}
92125
}

src/main/java/org/monora/uprotocol/core/network/DeviceAddress.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ public abstract class DeviceAddress
1818
/**
1919
* The {@link Device#uid} that specifies who owns this address.
2020
*/
21-
public String deviceId;
21+
public String deviceUid;
2222

2323
/**
2424
* The last time that a communication was started with this address.
2525
*
2626
* @see System#currentTimeMillis()
2727
*/
28-
public long lastCheckedDate;
28+
public long lastUsageTime;
29+
30+
@Override
31+
public boolean equals(Object obj)
32+
{
33+
if (obj instanceof DeviceAddress) {
34+
return inetAddress != null && inetAddress.equals(((DeviceAddress) obj).inetAddress);
35+
}
36+
return super.equals(obj);
37+
}
2938
}

src/main/java/org/monora/uprotocol/core/network/TransferItem.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,15 @@ public enum Type
7878
*/
7979
OUTGOING
8080
}
81+
82+
@Override
83+
public boolean equals(Object obj)
84+
{
85+
if (obj instanceof TransferItem) {
86+
TransferItem other = (TransferItem) obj;
87+
return type != null && type.equals(other.type) && transferId == other.transferId && id == other.id;
88+
}
89+
90+
return super.equals(obj);
91+
}
8192
}

src/main/java/org/monora/uprotocol/core/persistence/PersistenceProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ default JSONObject toJson(int senderKey, int pin) throws JSONException
222222
.put(Keyword.DEVICE_BRAND, device.brand)
223223
.put(Keyword.DEVICE_MODEL, device.model)
224224
.put(Keyword.DEVICE_USERNAME, device.username)
225+
.put(Keyword.DEVICE_CLIENT_TYPE, device.clientType)
225226
.put(Keyword.DEVICE_VERSION_CODE, device.versionCode)
226227
.put(Keyword.DEVICE_VERSION_NAME, device.versionName)
227228
.put(Keyword.DEVICE_PROTOCOL_VERSION, device.protocolVersion)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.monora.uprotocol.core.protocol;
2+
3+
// TODO: 9/11/20 Mention this devices on other platforms too.
4+
5+
/**
6+
* This enum determines the type of the client.
7+
*/
8+
public enum ClientType
9+
{
10+
Any,
11+
Mobile,
12+
Web,
13+
Desktop,
14+
IoT
15+
}

src/main/java/org/monora/uprotocol/core/protocol/ConnectionProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import java.net.InetAddress;
77

88
/**
9-
* This factory class is for opening connections in order to bypass the NAT/firewall. What this does is communicate with
10-
* the appropriate system resources before opening a connection.
9+
* This factory class is responsible for opening connections in order to bypass the NAT/firewall. What this does is
10+
* communicate with the appropriate system resources before opening a connection.
1111
*/
1212
public interface ConnectionProvider
1313
{

0 commit comments

Comments
 (0)