Stub for servicing connection to ProtectBabyMonitor service
If a client connects to the advertised ProtectBabyMonitor service, attempt to service the connection. For now, simply close the connection instead of sending data.
This commit is contained in:
@@ -18,6 +18,7 @@ package protect.babymonitor;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.nsd.NsdManager;
|
import android.net.nsd.NsdManager;
|
||||||
@@ -37,6 +38,22 @@ public class MonitorActivity extends Activity
|
|||||||
NsdManager.RegistrationListener _registrationListener;
|
NsdManager.RegistrationListener _registrationListener;
|
||||||
|
|
||||||
ServerSocket _serverSocket;
|
ServerSocket _serverSocket;
|
||||||
|
Thread _serviceThread;
|
||||||
|
|
||||||
|
private void serviceConnection(Socket socket) throws IOException
|
||||||
|
{
|
||||||
|
MonitorActivity.this.runOnUiThread(new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
final TextView statusText = (TextView) findViewById(R.id.textStatus);
|
||||||
|
statusText.setText("Streaming...");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
@@ -59,6 +76,43 @@ public class MonitorActivity extends Activity
|
|||||||
int localPort = _serverSocket.getLocalPort();
|
int localPort = _serverSocket.getLocalPort();
|
||||||
|
|
||||||
registerService(localPort);
|
registerService(localPort);
|
||||||
|
|
||||||
|
_serviceThread = new Thread(new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Socket socket = _serverSocket.accept();
|
||||||
|
serviceConnection(socket);
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
Log.e(TAG, "Failed when serving connection", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_serverSocket.close();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
MonitorActivity.this.runOnUiThread(new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
final TextView statusText = (TextView) findViewById(R.id.textStatus);
|
||||||
|
statusText.setText("Stopped");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
_serviceThread.start();
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
@@ -79,6 +133,12 @@ public class MonitorActivity extends Activity
|
|||||||
_registrationListener = null;
|
_registrationListener = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(_serviceThread != null)
|
||||||
|
{
|
||||||
|
_serviceThread.interrupt();
|
||||||
|
_serviceThread = null;
|
||||||
|
}
|
||||||
|
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user