Update presence server interface to match new API.

We now use a different port for the presence server and the proxy, each set in the config file
This commit is contained in:
Arnaud Vergnet 2021-01-07 10:43:46 +01:00
parent 20e341c190
commit 2929211c85
6 changed files with 77 additions and 29 deletions

View file

@ -2,6 +2,10 @@
"serveur": { "serveur": {
"actif": 1, "actif": 1,
"uri": "test", "uri": "test",
"type": "INSA" "type": "INSA",
"ports": {
"presence": 123456789,
"proxy": 987654321
}
} }
} }

View file

@ -25,7 +25,6 @@ public class Config {
return serverConfig; return serverConfig;
} }
@SuppressWarnings("FieldCanBeLocal") @SuppressWarnings("FieldCanBeLocal")
public static class ServerConfig { public static class ServerConfig {
@ -34,10 +33,15 @@ public class Config {
private boolean enabled; private boolean enabled;
private String uri; private String uri;
private PresenceType type; private PresenceType type;
private int presencePort;
private int proxyPort;
private final String ENABLED_KEY = "actif"; private final String ENABLED_KEY = "actif";
private final String URI_KEY = "uri"; private final String URI_KEY = "uri";
private final String TYPE_KEY = "type"; private final String TYPE_KEY = "type";
private final String PORTS_KEY = "ports";
private final String PORT_PRESENCE_KEY = "presence";
private final String PORT_PROXY_KEY = "proxy";
/** /**
* Basic constructor setting the default server configuration * Basic constructor setting the default server configuration
@ -63,6 +67,7 @@ public class Config {
enabled = false; enabled = false;
} else { } else {
readServerType(); readServerType();
readServerPorts();
} }
} }
} }
@ -103,6 +108,17 @@ public class Config {
} }
} }
private void readServerPorts() {
try {
JSONObject portsObj = obj.getJSONObject(PORTS_KEY);
presencePort = portsObj.getInt(PORT_PRESENCE_KEY);
proxyPort = portsObj.getInt(PORT_PROXY_KEY);
} catch (JSONException e) {
presencePort = 30000; // TODO set default ports
proxyPort = 300001;
}
}
/** /**
* Checks if the presence server is enabled. * Checks if the presence server is enabled.
* If this returns false, all other data returned by this object can be ignored. * If this returns false, all other data returned by this object can be ignored.
@ -120,5 +136,13 @@ public class Config {
public PresenceType getType() { public PresenceType getType() {
return type; return type;
} }
public int getPresencePort() {
return presencePort;
}
public int getProxyPort() {
return proxyPort;
}
} }
} }

View file

@ -1,14 +1,35 @@
package fr.insa.clavardator.server; package fr.insa.clavardator.server;
import fr.insa.clavardator.network.PeerConnection;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
/**
* Handles connection to the default INSA Clavardator presence server.
* <br/>
* This server does not uses HTTP, everything is done using sockets.
* The presence server and the proxy each live on a different port.
* <br/>
* The client needs to initiate a TCP connection on each port.
* <br/>
* On the presence server port, the client will be able to send presence messages
* such as subscribe, unsubscribe, publish, and receive notifications.
* <br/>
* On the proxy port, the client will be able to send regular
* {@link fr.insa.clavardator.chat.Message messages} like on the local network.
* The proxy will forward the message to the appropriate recipient using the provided id.
*/
public class InsaPresence implements Presence { public class InsaPresence implements Presence {
private final String path; private final String path;
private final int presencePort;
private final int proxyPort;
public InsaPresence(String path) { public InsaPresence(String path, int presencePort, int proxyPort) {
this.path = path; this.path = path;
this.presencePort = presencePort;
this.proxyPort = proxyPort;
} }
@Override @Override
@ -16,23 +37,18 @@ public class InsaPresence implements Presence {
return null; return null;
} }
@Override
public void unsubscribe() {
}
@Override @Override
public void publish(boolean connected) { public void publish(boolean connected) {
} }
@Override @Override
public void acceptNotifications() { public PeerConnection getProxyConnection() {
}
@Override
public void stopNotifications() {
}
@Override
public InetAddress getInetAddress() {
return null; return null;
} }
} }

View file

@ -1,5 +1,7 @@
package fr.insa.clavardator.server; package fr.insa.clavardator.server;
import fr.insa.clavardator.network.PeerConnection;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
@ -13,6 +15,12 @@ public interface Presence {
*/ */
ArrayList<String> subscribe(); ArrayList<String> subscribe();
/**
* Stops subscription to the presence server.
* This will stop notifications.
*/
void unsubscribe();
/** /**
* Updates the current user state on the server. * Updates the current user state on the server.
* This function must be called on app exit, * This function must be called on app exit,
@ -22,20 +30,14 @@ public interface Presence {
*/ */
void publish(boolean connected); void publish(boolean connected);
/**
* Starts listening to presence server update notifications.
*/
void acceptNotifications();
/** /**
* Stops listening to presence server update notifications. * Gets a connection to the proxy.
*/ * This can be used to initialize a
void stopNotifications(); * {@link fr.insa.clavardator.users.PeerUser Peeruser}
* and send messages like on a local network.
/**
* Gets the presence server InetAddress
* *
* @return The server address * @return The server address
*/ */
InetAddress getInetAddress(); PeerConnection getProxyConnection();
} }

View file

@ -1,10 +1,10 @@
package fr.insa.clavardator.server; package fr.insa.clavardator.server;
public class PresenceFactory { public class PresenceFactory {
public static Presence create(PresenceType type, String uri) throws UnknownPresenceException { public static Presence create(PresenceType type, String uri, int serverPort, int proxyPort) throws UnknownPresenceException {
switch (type) { switch (type) {
case INSA: case INSA:
return new InsaPresence(uri); return new InsaPresence(uri, serverPort, proxyPort);
default: default:
throw new UnknownPresenceException(); throw new UnknownPresenceException();
} }

View file

@ -270,8 +270,10 @@ public class MainController implements Initializable {
try { try {
final PresenceType type = serverConfig.getType(); final PresenceType type = serverConfig.getType();
final String uri = serverConfig.getUri(); final String uri = serverConfig.getUri();
presenceServer = PresenceFactory.create(type, uri); final int presencePort = serverConfig.getPresencePort();
Log.v("INIT", "Presence server support enabled: " + type + " @ " + uri); final int proxyPort = serverConfig.getProxyPort();
presenceServer = PresenceFactory.create(type, uri, presencePort, proxyPort);
Log.v("INIT", "Presence server support enabled: " + type + "@" + uri + ':' + presencePort + " / proxy:" + proxyPort);
} catch (UnknownPresenceException e) { } catch (UnknownPresenceException e) {
Log.e("INIT", "Presence server type not found", e); Log.e("INIT", "Presence server type not found", e);
presenceServer = null; presenceServer = null;