Bluetooth connection working (Linux).
Change-Id: Iac77c3ce0ab0ea7bda3105c58859888e55e64d2d
This commit is contained in:
@@ -8,6 +8,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.libreoffice.impressremote.communication;
|
package org.libreoffice.impressremote.communication;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@@ -15,6 +17,8 @@ import android.bluetooth.BluetoothAdapter;
|
|||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothSocket;
|
import android.bluetooth.BluetoothSocket;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard Network client. Connects to a server using Sockets.
|
* Standard Network client. Connects to a server using Sockets.
|
||||||
@@ -31,27 +35,43 @@ public class BluetoothClient extends Client {
|
|||||||
super(aContext);
|
super(aContext);
|
||||||
try {
|
try {
|
||||||
BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter();
|
BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
|
System.out.println("Attemtping to connect to:" + bluetoothAddress);
|
||||||
BluetoothDevice aDevice = aAdapter
|
BluetoothDevice aDevice = aAdapter
|
||||||
.getRemoteDevice(bluetoothAddress);
|
.getRemoteDevice(bluetoothAddress);
|
||||||
|
aAdapter.cancelDiscovery();
|
||||||
BluetoothSocket aSocket = aDevice
|
BluetoothSocket aSocket = aDevice
|
||||||
.createRfcommSocketToServiceRecord(UUID
|
.createRfcommSocketToServiceRecord(UUID
|
||||||
.fromString("00001101-0000-1000-8000-00805f9b34fb"));
|
.fromString("00001101-0000-1000-8000-00805F9B34FB"));
|
||||||
aSocket.connect();
|
aSocket.connect();
|
||||||
|
// mSocket = aSocket;
|
||||||
|
System.out.println("Connected");
|
||||||
|
|
||||||
} catch (Exception e) {
|
mInputStream = aSocket.getInputStream();
|
||||||
e.printStackTrace();
|
mReader = new BufferedReader(new InputStreamReader(mInputStream,
|
||||||
}
|
CHARSET));
|
||||||
// BluetoothSocket aSocket = new BluetoothClient(bluetoothAddress, aContext)
|
mOutputStream = aSocket.getOutputStream();
|
||||||
// .createRfcommSocketToServiceRecord(UUID
|
|
||||||
// .fromString("00001101-0000-1000-8000-00805F9B34F
|
// mOutputStream.write(20);
|
||||||
|
// mOutputStream.write(20);
|
||||||
|
// mOutputStream.write(20);
|
||||||
|
// mOutputStream.flush();
|
||||||
|
// System.out.println("reading");
|
||||||
|
// while (true) {
|
||||||
|
// System.out.println(mInputStream.read());
|
||||||
// }
|
// }
|
||||||
// try {
|
String aTemp = mReader.readLine();
|
||||||
// mSocket = new Socket(ipAddress, PORT);
|
System.out.println("SF:waited");
|
||||||
// mInputStream = mSocket.getInputStream();
|
if (!aTemp.equals("LO_SERVER_SERVER_PAIRED")) {
|
||||||
// mReader = new BufferedReader(new InputStreamReader(mInputStream,
|
return;
|
||||||
// CHARSET));
|
}
|
||||||
// mOutputStream = mSocket.getOutputStream();
|
while (mReader.readLine().length() != 0) {
|
||||||
// // Pairing.
|
// Get rid of extra lines
|
||||||
|
}
|
||||||
|
Intent aIntent = new Intent(
|
||||||
|
CommunicationService.MSG_PAIRING_SUCCESSFUL);
|
||||||
|
LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
|
||||||
|
startListening();
|
||||||
|
// Pairing.
|
||||||
// Random aRandom = new Random();
|
// Random aRandom = new Random();
|
||||||
// String aPin = "" + (aRandom.nextInt(9000) + 1000);
|
// String aPin = "" + (aRandom.nextInt(9000) + 1000);
|
||||||
// while (aPin.length() < 4) {
|
// while (aPin.length() < 4) {
|
||||||
@@ -74,10 +94,7 @@ public class BluetoothClient extends Client {
|
|||||||
// if (!aTemp.equals("LO_SERVER_SERVER_PAIRED")) {
|
// if (!aTemp.equals("LO_SERVER_SERVER_PAIRED")) {
|
||||||
// return;
|
// return;
|
||||||
// } else {
|
// } else {
|
||||||
// aIntent = new Intent(
|
//
|
||||||
// CommunicationService.MSG_PAIRING_SUCCESSFUL);
|
|
||||||
// LocalBroadcastManager.getInstance(mContext).sendBroadcast(
|
|
||||||
// aIntent);
|
|
||||||
// }
|
// }
|
||||||
// while (mReader.readLine().length() != 0) {
|
// while (mReader.readLine().length() != 0) {
|
||||||
// // Get rid of extra lines
|
// // Get rid of extra lines
|
||||||
@@ -85,13 +102,10 @@ public class BluetoothClient extends Client {
|
|||||||
// }
|
// }
|
||||||
// System.out.println("SD: empty");
|
// System.out.println("SD: empty");
|
||||||
// startListening();
|
// startListening();
|
||||||
// } catch (UnknownHostException e) {
|
|
||||||
// // TODO Tell the user we have a problem
|
} catch (Exception e) {
|
||||||
// e.printStackTrace();
|
e.printStackTrace();
|
||||||
// } catch (IOException e) {
|
}
|
||||||
// // TODO As above
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -11,6 +11,7 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
|
||||||
public class BluetoothFinder {
|
public class BluetoothFinder {
|
||||||
|
|
||||||
@@ -32,9 +33,8 @@ public class BluetoothFinder {
|
|||||||
return; // No bluetooth adapter found (emulator, special devices)
|
return; // No bluetooth adapter found (emulator, special devices)
|
||||||
}
|
}
|
||||||
System.out.println("BT:Discovery starting");
|
System.out.println("BT:Discovery starting");
|
||||||
IntentFilter aFilter = new IntentFilter(
|
IntentFilter aFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
|
||||||
BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
aFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
||||||
aFilter.addAction(BluetoothDevice.ACTION_FOUND);
|
|
||||||
mContext.registerReceiver(mReceiver, aFilter);
|
mContext.registerReceiver(mReceiver, aFilter);
|
||||||
|
|
||||||
mAdapter.enable();
|
mAdapter.enable();
|
||||||
@@ -64,7 +64,7 @@ public class BluetoothFinder {
|
|||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent aIntent) {
|
public void onReceive(Context context, Intent aIntent) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
System.out.println("Received intent");
|
System.out.print("Received intent<<<");
|
||||||
System.out.println(aIntent.getAction());
|
System.out.println(aIntent.getAction());
|
||||||
if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
|
if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
|
||||||
BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras()
|
BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras()
|
||||||
@@ -77,7 +77,8 @@ public class BluetoothFinder {
|
|||||||
System.out.println("Now we have: " + mServerList.size());
|
System.out.println("Now we have: " + mServerList.size());
|
||||||
Intent aNIntent = new Intent(
|
Intent aNIntent = new Intent(
|
||||||
CommunicationService.MSG_SERVERLIST_CHANGED);
|
CommunicationService.MSG_SERVERLIST_CHANGED);
|
||||||
mContext.sendBroadcast(aNIntent);
|
LocalBroadcastManager.getInstance(mContext).sendBroadcast(
|
||||||
|
aNIntent);
|
||||||
// System.out.println("Found " + aDevice.getName());
|
// System.out.println("Found " + aDevice.getName());
|
||||||
// try {
|
// try {
|
||||||
// // "f36d0a20-e876-11e1-aff1-0800200c9a66"
|
// // "f36d0a20-e876-11e1-aff1-0800200c9a66"
|
||||||
|
@@ -35,6 +35,8 @@ public class Receiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void parseCommand(ArrayList<String> aCommand) {
|
public void parseCommand(ArrayList<String> aCommand) {
|
||||||
|
if (aCommand.size() == 0)
|
||||||
|
return; // E.g. if empty line received for whatever reason.
|
||||||
String aInstruction = aCommand.get(0);
|
String aInstruction = aCommand.get(0);
|
||||||
if (aInstruction.equals("slideshow_started")) {
|
if (aInstruction.equals("slideshow_started")) {
|
||||||
int aSlideShowlength = Integer.parseInt(aCommand.get(1));
|
int aSlideShowlength = Integer.parseInt(aCommand.get(1));
|
||||||
|
@@ -13,6 +13,7 @@ import org.libreoffice.impressremote.communication.Server.Protocol;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.support.v4.content.LocalBroadcastManager;
|
||||||
|
|
||||||
public class ServerFinder {
|
public class ServerFinder {
|
||||||
|
|
||||||
@@ -169,7 +170,7 @@ public class ServerFinder {
|
|||||||
*/
|
*/
|
||||||
private void notifyActivity() {
|
private void notifyActivity() {
|
||||||
Intent aIntent = new Intent(CommunicationService.MSG_SERVERLIST_CHANGED);
|
Intent aIntent = new Intent(CommunicationService.MSG_SERVERLIST_CHANGED);
|
||||||
mContext.sendBroadcast(aIntent);
|
LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<Server> getServerList() {
|
public Collection<Server> getServerList() {
|
||||||
|
@@ -22,29 +22,60 @@
|
|||||||
#include "bluetooth/rfcomm.h"
|
#include "bluetooth/rfcomm.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#include "Communicator.hxx"
|
||||||
|
|
||||||
using namespace sd;
|
using namespace sd;
|
||||||
|
|
||||||
BluetoothServer::BluetoothServer():
|
BluetoothServer::BluetoothServer( std::vector<Communicator*>* pCommunicators ):
|
||||||
Thread( "BluetoothServer" )
|
Thread( "BluetoothServer" ),
|
||||||
|
mpCommunicators( pCommunicators )
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothServer::~BluetoothServer()
|
BluetoothServer::~BluetoothServer()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct oslSocketImpl {
|
||||||
|
int m_Socket;
|
||||||
|
int m_nLastError;
|
||||||
|
void* m_CloseCallback;
|
||||||
|
void* m_CallbackArg;
|
||||||
|
oslInterlockedCount m_nRefCount;
|
||||||
|
#if defined(LINUX)
|
||||||
|
sal_Bool m_bIsAccepting;
|
||||||
|
sal_Bool m_bIsInShutdown;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
void BluetoothServer::execute()
|
void BluetoothServer::execute()
|
||||||
{
|
{
|
||||||
#ifdef LINUX
|
#ifdef LINUX
|
||||||
// g_type_init();
|
g_type_init();
|
||||||
// GError* aError = NULL;
|
GError* aError = NULL;
|
||||||
// GDBusConnection* aConnection = g_bus_get_sync( G_BUS_TYPE_SYSTEM, NULL, &aError );
|
GDBusConnection* aConnection = g_bus_get_sync( G_BUS_TYPE_SYSTEM, NULL, &aError );
|
||||||
// if ( aError )
|
if ( aError )
|
||||||
// {
|
{
|
||||||
// fprintf( stderr, aError->message );
|
fprintf( stderr, aError->message );
|
||||||
// g_error_free( aError );
|
g_error_free( aError );
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
GVariant *aAdapter = g_dbus_connection_call_sync( aConnection,
|
||||||
|
"org.bluez", "/", "org.bluez.Manager",
|
||||||
|
"DefaultAdapter", NULL,
|
||||||
|
G_VARIANT_TYPE_TUPLE,
|
||||||
|
G_DBUS_CALL_FLAGS_NONE, -1, NULL, &aError);
|
||||||
|
GVariant *aAdapterName = g_variant_get_child_value( aAdapter, 0 );
|
||||||
|
if ( aError )
|
||||||
|
{
|
||||||
|
fprintf( stderr, aError->message );
|
||||||
|
g_error_free( aError );
|
||||||
|
}
|
||||||
|
fprintf( stderr, (const char*) g_variant_get_string( aAdapterName, NULL ) );
|
||||||
|
|
||||||
|
|
||||||
// GDBusObjectManager* aManager = g_dbus_object_manager_client_new_sync( aConnection,
|
// GDBusObjectManager* aManager = g_dbus_object_manager_client_new_sync( aConnection,
|
||||||
// G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, "org.bluez.Manager", "/org/bluez",
|
// G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, "org.bluez.Manager", "/org/bluez",
|
||||||
// NULL, NULL, NULL, NULL, &aError );
|
// NULL, NULL, NULL, NULL, &aError );
|
||||||
@@ -53,14 +84,21 @@ void BluetoothServer::execute()
|
|||||||
// fprintf( stderr, aError->message );
|
// fprintf( stderr, aError->message );
|
||||||
// g_error_free( aError );
|
// g_error_free( aError );
|
||||||
// }
|
// }
|
||||||
//
|
|
||||||
// GVariant *aRet = g_dbus_connection_call_sync( aConnection,
|
GVariant *aRecordHandle = g_dbus_connection_call_sync( aConnection,
|
||||||
// "org.bluez", "/", "org.bluez.Database",
|
"org.bluez", g_variant_get_string( aAdapterName, NULL ), "org.bluez.Service",
|
||||||
// "AddServiceRecordFromXml",
|
"AddRecord",
|
||||||
// g_variant_new ("s",
|
g_variant_new("(s)",
|
||||||
// ""),,
|
"<?xml version='1.0' encoding= 'UTF-8' ?><record><attribute id='0x0001'><sequence><uuid value='0x1101' /></sequence></attribute><attribute id='0x0004'><sequence><sequence><uuid value='0x0100' /></sequence><sequence><uuid value='0x0003' /><uint8 value='0x05' /></sequence></sequence></attribute><attribute id='0x0005'><sequence><uuid value='0x1002' /></sequence></attribute><attribute id='0x0006'><sequence><uint16 value='0x656e' /><uint16 value='0x006a' /><uint16 value='0x0100' /></sequence></attribute><attribute id='0x0009'><sequence><sequence><uuid value='0x1101' /><uint16 value='0x0100' /></sequence></sequence></attribute><attribute id='0x0100'><text value='Serial Port' /></attribute><attribute id='0x0101'><text value='COM Port' /></attribute></record>"),
|
||||||
// G_VARIANT_TYPE_UINT32,
|
G_VARIANT_TYPE_TUPLE,
|
||||||
// G_DBUS_CALL_FLAGS_NONE, -1, NULL, &aError);
|
G_DBUS_CALL_FLAGS_NONE, -1, NULL, &aError);
|
||||||
|
if ( aError )
|
||||||
|
{
|
||||||
|
fprintf( stderr, aError->message );
|
||||||
|
g_error_free( aError );
|
||||||
|
}
|
||||||
|
(void) aRecordHandle;
|
||||||
|
// Remove handle again at some point
|
||||||
// g_variant_unref( aRet );
|
// g_variant_unref( aRet );
|
||||||
// fprintf( stderr, "Manager gotten\n" );
|
// fprintf( stderr, "Manager gotten\n" );
|
||||||
//
|
//
|
||||||
@@ -125,8 +163,8 @@ void BluetoothServer::execute()
|
|||||||
|
|
||||||
sockaddr_rc aAddr;
|
sockaddr_rc aAddr;
|
||||||
aAddr.rc_family = AF_BLUETOOTH;
|
aAddr.rc_family = AF_BLUETOOTH;
|
||||||
// aAddr.rc_bdaddr = *BDADDR_ANY;
|
aAddr.rc_bdaddr = {{0, 0, 0, 0, 0, 0}}; // BDADDR_ANY is broken
|
||||||
aAddr.rc_channel = 1;
|
aAddr.rc_channel = 5;
|
||||||
|
|
||||||
if ( bind( aSocket, (sockaddr*) &aAddr, sizeof(aAddr)) < 0 ) {
|
if ( bind( aSocket, (sockaddr*) &aAddr, sizeof(aAddr)) < 0 ) {
|
||||||
close( aSocket );
|
close( aSocket );
|
||||||
@@ -141,12 +179,18 @@ void BluetoothServer::execute()
|
|||||||
|
|
||||||
sockaddr_rc aRemoteAddr;
|
sockaddr_rc aRemoteAddr;
|
||||||
socklen_t aRemoteAddrLen = sizeof(aRemoteAddr);
|
socklen_t aRemoteAddrLen = sizeof(aRemoteAddr);
|
||||||
if ( accept(aSocket, (sockaddr*) &aRemoteAddr, &aRemoteAddrLen) < 0 )
|
int bSocket;
|
||||||
|
if ( (bSocket = accept(aSocket, (sockaddr*) &aRemoteAddr, &aRemoteAddrLen)) < 0 )
|
||||||
{
|
{
|
||||||
close( aSocket );
|
close( aSocket );
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
fprintf( stderr, "Accepted Bluetooth\n" );
|
fprintf( stderr, "Accepted Bluetooth\n" );
|
||||||
|
|
||||||
|
Communicator* pCommunicator = new Communicator( new BufferedStreamSocket( bSocket) );
|
||||||
|
mpCommunicators->push_back( pCommunicator );
|
||||||
|
pCommunicator->launch();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -163,12 +207,12 @@ void BluetoothServer::execute()
|
|||||||
|
|
||||||
BluetoothServer *sd::BluetoothServer::spServer = NULL;
|
BluetoothServer *sd::BluetoothServer::spServer = NULL;
|
||||||
|
|
||||||
void BluetoothServer::setup()
|
void BluetoothServer::setup( std::vector<Communicator*>* pCommunicators )
|
||||||
{
|
{
|
||||||
if (spServer)
|
if (spServer)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
spServer = new BluetoothServer();
|
spServer = new BluetoothServer( pCommunicators );
|
||||||
spServer->launch();
|
spServer->launch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -10,16 +10,19 @@
|
|||||||
#define _SD_IMPRESSREMOTE_BLUETOOTHSERVER_HXX
|
#define _SD_IMPRESSREMOTE_BLUETOOTHSERVER_HXX
|
||||||
|
|
||||||
#include <salhelper/thread.hxx>
|
#include <salhelper/thread.hxx>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace sd
|
namespace sd
|
||||||
{
|
{
|
||||||
|
class Communicator;
|
||||||
|
|
||||||
class BluetoothServer:
|
class BluetoothServer:
|
||||||
public salhelper::Thread
|
public salhelper::Thread
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void setup();
|
static void setup( std::vector<Communicator*>* pCommunicators );
|
||||||
private:
|
private:
|
||||||
BluetoothServer();
|
BluetoothServer( std::vector<Communicator*>* pCommunicators );
|
||||||
~BluetoothServer();
|
~BluetoothServer();
|
||||||
static BluetoothServer *spServer;
|
static BluetoothServer *spServer;
|
||||||
|
|
||||||
@@ -27,6 +30,7 @@ namespace sd
|
|||||||
public:
|
public:
|
||||||
private:
|
private:
|
||||||
void execute();
|
void execute();
|
||||||
|
std::vector<Communicator*>* mpCommunicators;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -11,6 +11,12 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
using namespace sd;
|
using namespace sd;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace osl;
|
using namespace osl;
|
||||||
@@ -19,10 +25,36 @@ BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket &aSocket ):
|
|||||||
StreamSocket( aSocket ),
|
StreamSocket( aSocket ),
|
||||||
aRet( 0 ),
|
aRet( 0 ),
|
||||||
aRead( 0 ),
|
aRead( 0 ),
|
||||||
aBuffer()
|
aBuffer(),
|
||||||
|
mSocket( 0 ),
|
||||||
|
usingCSocket( false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BufferedStreamSocket::BufferedStreamSocket( int aSocket ):
|
||||||
|
StreamSocket(),
|
||||||
|
aRet( 0 ),
|
||||||
|
aRead( 0 ),
|
||||||
|
aBuffer(),
|
||||||
|
mSocket( aSocket ),
|
||||||
|
usingCSocket( true )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void BufferedStreamSocket::getPeerAddr(osl::SocketAddr& rAddr)
|
||||||
|
{
|
||||||
|
assert ( !usingCSocket );
|
||||||
|
StreamSocket::getPeerAddr( rAddr );
|
||||||
|
}
|
||||||
|
|
||||||
|
sal_Int32 BufferedStreamSocket::write( const void* pBuffer, sal_uInt32 n )
|
||||||
|
{
|
||||||
|
if ( !usingCSocket )
|
||||||
|
return StreamSocket::write( pBuffer, n );
|
||||||
|
else
|
||||||
|
return ::write( mSocket, pBuffer, (size_t) n );
|
||||||
|
}
|
||||||
|
|
||||||
sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
|
sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
|
||||||
{
|
{
|
||||||
while ( true )
|
while ( true )
|
||||||
@@ -39,12 +71,15 @@ sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
|
|||||||
aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
|
aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
|
||||||
aRead -= (aLocation + 1);
|
aRead -= (aLocation + 1);
|
||||||
|
|
||||||
return aLine.getLength();
|
return aLine.getLength() + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Then try and receive if nothing present
|
// Then try and receive if nothing present
|
||||||
aBuffer.resize( aRead + 100 );
|
aBuffer.resize( aRead + 100 );
|
||||||
aRet = recv( &aBuffer[aRead], 100 );
|
if ( !usingCSocket)
|
||||||
|
aRet = StreamSocket::recv( &aBuffer[aRead], 100 );
|
||||||
|
else
|
||||||
|
aRet = ::read( mSocket, &aBuffer[aRead], 100 );
|
||||||
|
|
||||||
if ( aRet == 0 )
|
if ( aRet == 0 )
|
||||||
{
|
{
|
||||||
|
@@ -20,14 +20,27 @@ namespace sd
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper for an osl StreamSocket to allow reading lines.
|
* [A wrapper for an osl StreamSocket to allow reading lines.]
|
||||||
|
*
|
||||||
|
* Currently wraps either an osl StreamSocket or a standard c socket,
|
||||||
|
* allowing reading and writing for our purposes. Should eventually be
|
||||||
|
* returned to being a StreamSocket wrapper if/when Bluetooth is
|
||||||
|
* integrated into osl Sockets.
|
||||||
*/
|
*/
|
||||||
class BufferedStreamSocket :
|
class BufferedStreamSocket :
|
||||||
public ::osl::StreamSocket,
|
private ::osl::StreamSocket,
|
||||||
private ::boost::noncopyable
|
private ::boost::noncopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* Create a BufferedStreamSocket on top of an
|
||||||
|
* osl::StreamSocket.
|
||||||
|
*/
|
||||||
BufferedStreamSocket( const osl::StreamSocket &aSocket );
|
BufferedStreamSocket( const osl::StreamSocket &aSocket );
|
||||||
|
/**
|
||||||
|
* Create a BufferedStreamSocket on top of a standard c socket.
|
||||||
|
*/
|
||||||
|
BufferedStreamSocket( int aSocket );
|
||||||
BufferedStreamSocket( const BufferedStreamSocket &aSocket );
|
BufferedStreamSocket( const BufferedStreamSocket &aSocket );
|
||||||
/**
|
/**
|
||||||
* Blocks until a line is read.
|
* Blocks until a line is read.
|
||||||
@@ -35,9 +48,15 @@ namespace sd
|
|||||||
* if there was a problem in communications.
|
* if there was a problem in communications.
|
||||||
*/
|
*/
|
||||||
sal_Int32 readLine(OString& aLine);
|
sal_Int32 readLine(OString& aLine);
|
||||||
|
|
||||||
|
sal_Int32 write( const void* pBuffer, sal_uInt32 n );
|
||||||
|
|
||||||
|
void getPeerAddr(osl::SocketAddr&);
|
||||||
private:
|
private:
|
||||||
sal_Int32 aRet, aRead;
|
sal_Int32 aRet, aRead;
|
||||||
std::vector<char> aBuffer;
|
std::vector<char> aBuffer;
|
||||||
|
int mSocket;
|
||||||
|
bool usingCSocket;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,7 +37,7 @@ Communicator::~Communicator()
|
|||||||
// Run as a thread
|
// Run as a thread
|
||||||
void Communicator::execute()
|
void Communicator::execute()
|
||||||
{
|
{
|
||||||
pTransmitter = new Transmitter( *mpSocket );
|
pTransmitter = new Transmitter( mpSocket );
|
||||||
pTransmitter->launch();
|
pTransmitter->launch();
|
||||||
|
|
||||||
pTransmitter->addMessage( "LO_SERVER_SERVER_PAIRED\n\n",
|
pTransmitter->addMessage( "LO_SERVER_SERVER_PAIRED\n\n",
|
||||||
@@ -61,34 +61,25 @@ void Communicator::execute()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
sal_uInt64 aRet, aRead;
|
sal_uInt64 aRet;
|
||||||
vector<char> aBuffer;
|
|
||||||
vector<OString> aCommand;
|
vector<OString> aCommand;
|
||||||
aRead = 0;
|
|
||||||
while ( true )
|
while ( true )
|
||||||
{
|
{
|
||||||
aBuffer.resize( aRead + 100 );
|
OString aLine;
|
||||||
aRet = mpSocket->recv( &aBuffer[aRead], 100 );
|
aRet = mpSocket->readLine( aLine );
|
||||||
if ( aRet == 0 )
|
if ( aRet == 0 )
|
||||||
{
|
{
|
||||||
break; // I.e. transmission finished.
|
break; // I.e. transmission finished.
|
||||||
}
|
}
|
||||||
aRead += aRet;
|
if ( aLine.getLength() )
|
||||||
vector<char>::iterator aIt;
|
|
||||||
while ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' ))
|
|
||||||
!= aBuffer.end() )
|
|
||||||
{
|
{
|
||||||
sal_uInt64 aLocation = aIt - aBuffer.begin();
|
aCommand.push_back( aLine );
|
||||||
|
}
|
||||||
aCommand.push_back( OString( &(*aBuffer.begin()), aLocation ) );
|
else
|
||||||
if ( aIt == aBuffer.begin() )
|
|
||||||
{
|
{
|
||||||
aReceiver.parseCommand( aCommand );
|
aReceiver.parseCommand( aCommand );
|
||||||
aCommand.clear();
|
aCommand.clear();
|
||||||
}
|
}
|
||||||
aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
|
|
||||||
aRead -= (aLocation + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// TODO: deal with transmision errors gracefully.
|
// TODO: deal with transmision errors gracefully.
|
||||||
disposeListener();
|
disposeListener();
|
||||||
|
@@ -55,8 +55,6 @@ void Receiver::parseCommand( std::vector<OString> aCommand )
|
|||||||
}
|
}
|
||||||
catch (uno::RuntimeException &)
|
catch (uno::RuntimeException &)
|
||||||
{
|
{
|
||||||
fprintf( stderr, "Error in retrieving Controller\n" );
|
|
||||||
//return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( aCommand[0].equals( "transition_next" ) )
|
if ( aCommand[0].equals( "transition_next" ) )
|
||||||
|
@@ -83,7 +83,6 @@ void RemoteServer::execute()
|
|||||||
|
|
||||||
if ( ! pSocket->readLine( aLine ) ) delete pSocket;
|
if ( ! pSocket->readLine( aLine ) ) delete pSocket;
|
||||||
OString aPin( aLine );
|
OString aPin( aLine );
|
||||||
fprintf( stderr, "Pin:%s\n", aPin.getStr() );
|
|
||||||
|
|
||||||
SocketAddr aClientAddr;
|
SocketAddr aClientAddr;
|
||||||
pSocket->getPeerAddr( aClientAddr );
|
pSocket->getPeerAddr( aClientAddr );
|
||||||
@@ -117,6 +116,8 @@ void RemoteServer::setup()
|
|||||||
|
|
||||||
spServer = new RemoteServer();
|
spServer = new RemoteServer();
|
||||||
spServer->launch();
|
spServer->launch();
|
||||||
|
|
||||||
|
sd::BluetoothServer::setup( &(spServer->mCommunicators) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -211,7 +212,6 @@ void SdDLL::RegisterRemotes()
|
|||||||
|
|
||||||
sd::RemoteServer::setup();
|
sd::RemoteServer::setup();
|
||||||
sd::DiscoveryService::setup();
|
sd::DiscoveryService::setup();
|
||||||
sd::BluetoothServer::setup();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||||
|
@@ -13,9 +13,9 @@ using namespace std;
|
|||||||
using namespace osl; // Sockets etc.
|
using namespace osl; // Sockets etc.
|
||||||
using namespace sd;
|
using namespace sd;
|
||||||
|
|
||||||
Transmitter::Transmitter( StreamSocket &aSocket )
|
Transmitter::Transmitter( BufferedStreamSocket* aSocket )
|
||||||
: Thread( "TransmitterThread" ),
|
: Thread( "TransmitterThread" ),
|
||||||
mStreamSocket( aSocket ),
|
pStreamSocket( aSocket ),
|
||||||
mQueuesNotEmpty(),
|
mQueuesNotEmpty(),
|
||||||
mFinishRequested(),
|
mFinishRequested(),
|
||||||
mQueueMutex(),
|
mQueueMutex(),
|
||||||
@@ -38,13 +38,13 @@ void Transmitter::execute()
|
|||||||
{
|
{
|
||||||
OString aMessage( mHighPriority.front() );
|
OString aMessage( mHighPriority.front() );
|
||||||
mHighPriority.pop();
|
mHighPriority.pop();
|
||||||
mStreamSocket.write( aMessage.getStr(), aMessage.getLength() );
|
pStreamSocket->write( aMessage.getStr(), aMessage.getLength() );
|
||||||
}
|
}
|
||||||
else if ( !mLowPriority.empty() )
|
else if ( !mLowPriority.empty() )
|
||||||
{
|
{
|
||||||
OString aMessage( mLowPriority.front() );
|
OString aMessage( mLowPriority.front() );
|
||||||
mLowPriority.pop();
|
mLowPriority.pop();
|
||||||
mStreamSocket.write( aMessage.getStr(), aMessage.getLength() );
|
pStreamSocket->write( aMessage.getStr(), aMessage.getLength() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( mLowPriority.empty() && mHighPriority.empty() )
|
if ( mLowPriority.empty() && mHighPriority.empty() )
|
||||||
|
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
#include <osl/conditn.hxx>
|
#include <osl/conditn.hxx>
|
||||||
#include <osl/mutex.hxx>
|
#include <osl/mutex.hxx>
|
||||||
#include <osl/socket.hxx>
|
#include "BufferedStreamSocket.hxx"
|
||||||
#include <salhelper/thread.hxx>
|
#include <salhelper/thread.hxx>
|
||||||
#include <rtl/string.hxx>
|
#include <rtl/string.hxx>
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ class Transmitter
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum Priority { PRIORITY_LOW = 1, PRIORITY_HIGH };
|
enum Priority { PRIORITY_LOW = 1, PRIORITY_HIGH };
|
||||||
Transmitter( osl::StreamSocket &aSocket );
|
Transmitter( ::sd::BufferedStreamSocket* aSocket );
|
||||||
~Transmitter();
|
~Transmitter();
|
||||||
void addMessage( const rtl::OString& aMessage, const Priority aPriority );
|
void addMessage( const rtl::OString& aMessage, const Priority aPriority );
|
||||||
void notifyFinished();
|
void notifyFinished();
|
||||||
@@ -33,7 +33,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
void execute();
|
void execute();
|
||||||
|
|
||||||
::osl::StreamSocket mStreamSocket;
|
::sd::BufferedStreamSocket* pStreamSocket;
|
||||||
|
|
||||||
::osl::Condition mQueuesNotEmpty;
|
::osl::Condition mQueuesNotEmpty;
|
||||||
::osl::Condition mFinishRequested;
|
::osl::Condition mFinishRequested;
|
||||||
|
Reference in New Issue
Block a user