Commit 487ae99f authored by Lily Tsai's avatar Lily Tsai
Browse files

set up nsd

parent 342b1b92
......@@ -8,7 +8,6 @@
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/ebcagent" />
<option value="$PROJECT_DIR$/ebclibrary" />
<option value="$PROJECT_DIR$/ebcmessageapp" />
<option value="$PROJECT_DIR$/ebcutils" />
......
package org.mpisws.encounters.encounterformation.simulator;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.util.Log;
import org.mpisws.helpers.Identifier;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.mDHNonces;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.mDHPubKeys;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.mSharedSecrets;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.otherDHFullKeys;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.otherDHNonces;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.otherDHPubKeys;
public class SimulationClient {
static private String TAG = SimulationClient.class.getSimpleName();
static private String mServiceName = "EbCSimulator";
static private String mServiceType = "_ebcsimulator._tcp";
private NsdManager.DiscoveryListener mDiscoveryListener;
private NsdManager mNsdManager;
private NsdManager.ResolveListener mResolveListener;
private NsdServiceInfo mService;
private InetAddress mHost;
private int mPort;
private Socket sock = null;
SimulationClient(Context context) {
mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);
initializeDiscoveryListener();
initializeResolveListener();
mNsdManager.discoverServices(mServiceType, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
public void initializeDiscoveryListener() {
// Instantiate a new DiscoveryListener
mDiscoveryListener = new NsdManager.DiscoveryListener() {
// Called as soon as service discovery begins.
@Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "Service discovery started");
}
@Override
public void onServiceFound(NsdServiceInfo service) {
// A service was found! Do something with it.
Log.d(TAG, "Service discovery success" + service);
if (!service.getServiceType().equals(mServiceType)) {
// Service type is the string containing the protocol and
// transport layer for this service.
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
} else if (service.getServiceName().contains("EbCSimulator")){
mNsdManager.resolveService(service, mResolveListener);
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
// When the network service is no longer available.
// Internal bookkeeping code goes here.
Log.e(TAG, "service lost" + service);
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed: Error code:" + errorCode);
mNsdManager.stopServiceDiscovery(this);
}
};
}
public void initializeResolveListener() {
mResolveListener = new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Called when the resolve fails. Use the error code to debug.
Log.e(TAG, "Resolve failed" + errorCode);
}
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
Log.e(TAG, "Resolve Succeeded. " + serviceInfo);
if (serviceInfo.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same IP.");
return;
}
mService = serviceInfo;
mPort = mService.getPort();
mHost = mService.getHost();
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
setupSocket();
}
};
}
public void setupSocket() {
try {
sock = new Socket(mHost, mPort);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendDHKeysOverSocket() throws IOException {
if (sock == null) return;
OutputStream os = sock.getOutputStream();
String serializedSecrets;
String serializedNonces;
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(mSharedSecrets);
so.flush();
serializedSecrets = bo.toString();
so.writeObject(otherDHNonces);
so.flush();
serializedNonces = bo.toString();
} catch (Exception e) {
System.out.println(e);
return;
}
PrintWriter pw = new PrintWriter(os, true);
pw.println(serializedSecrets);
pw.println(serializedNonces);
sock.close();
}
}
......@@ -42,6 +42,7 @@ public class SimulatorEncounterFormationCore implements Runnable {
protected static List<Identifier> otherDHPubKeys = new ArrayList<>(NUM_SIMULATED_DEVICES*NUM_SIMULATED_EPOCHS);
protected static List<Identifier> otherDHNonces = new ArrayList<>(NUM_SIMULATED_DEVICES*NUM_SIMULATED_EPOCHS);
protected static List<Identifier> otherDHFullKeys = new ArrayList<>(NUM_SIMULATED_DEVICES*NUM_SIMULATED_EPOCHS);
protected static List<Identifier> mSharedSecrets = new ArrayList<>(NUM_SIMULATED_DEVICES*NUM_SIMULATED_EPOCHS);
protected static int CURRENT_EPOCH = 0;
protected static Identifier mDHPubKey;
......@@ -98,10 +99,10 @@ public class SimulatorEncounterFormationCore implements Runnable {
otherDHFullKeys.get(j+(NUM_SIMULATED_DEVICES*i)).getBytes(),
mDHNonces.get(i).getBytes(),
mDHPubKeys.get(i).getBytes()));
topicsToCreate.add(new ImmutablePair<>(sharedSecret, sharedSecret));
mSharedSecrets.add(sharedSecret);
}
}
Log.d(TAG, "Creating " + topicsToCreate.size() + " topics");
Log.d(TAG, "Creating " + topicsToCreate.size() + " nonce topics");
ESClient.getInstance().createTopics(topicsToCreate);
}
......
include ':ebclibrary', ':testapp', ':ebcagent', ':ebcutils', ':exampleapp', ':ebcmessageapp'
include ':ebclibrary', ':testapp', ':ebcutils', ':exampleapp', ':ebcmessageapp'
package org.mpisws.testapp;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.mpisws.helpers.Identifier;
import org.mpisws.helpers.Utils;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class SimulationServer {
static private int port;
static private String mServiceName = "EbCSimulator";
static private String mServiceType = "_ebcsimulator._tcp";
private ServerSocket mServerSocket;
private NsdManager.RegistrationListener mRegistrationListener;
private NsdManager mNsdManager;
SimulationServer(Context context) {
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
// Initialize a server socket on the next available port.
List<Identifier> nonces = null;
List<Identifier> sharedSecrets = null;
try {
mServerSocket = new ServerSocket(0);
// Store the chosen port.
port = mServerSocket.getLocalPort();
registerService(port);
Socket sock = mServerSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
List<String> lines = new ArrayList<>();
String line;
while ((line = in.readLine()) != null) {
lines.add(line);
}
Utils.myAssert(lines.size() == 2);
sharedSecrets = deserializeIDList(lines.get(0));
nonces = deserializeIDList(lines.get(1));
sock.close();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
private List<Identifier> deserializeIDList(String serializedObject) throws IOException, ClassNotFoundException {
byte b[] = serializedObject.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
List<Identifier> obj = (List<Identifier>) si.readObject();
return obj;
}
public void registerService(int port) {
// Create the NsdServiceInfo object, and populate it.
NsdServiceInfo serviceInfo = new NsdServiceInfo();
// The name is subject to change based on conflicts
// with other services advertised on the same network.
serviceInfo.setServiceName(mServiceName);
serviceInfo.setServiceType(mServiceType);
serviceInfo.setPort(port);
initializeRegistrationListener();
mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
public void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
// Save the service name. Android may have changed it in order to
// resolve a conflict, so update the name you initially requested
// with the name Android actually used.
mServiceName = NsdServiceInfo.getServiceName();
}
@Override
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Registration failed! Put debugging code here to determine why.
}
@Override
public void onServiceUnregistered(NsdServiceInfo arg0) {
// Service has been unregistered. This only happens when you call
// NsdManager.unregisterService() and pass in this listener.
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
// Unregistration failed. Put debugging code here to determine why.
}
};
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment