# Create a new connection to ribbon.
self.ribbon_ready = False
self.backlog = [] # A list of bytestrings queued to be sent on the socket.
- self.ribbon_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
+ self.ribbon_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
self.ribbon_stream = tornado.iostream.IOStream(self.ribbon_socket)
- self.ribbon_stream.connect( ("localhost", 8888), self.on_ribbon_ready)
+ self.ribbon_stream.connect( os.path.join(pwd, "state", "socket"), self.on_ribbon_ready)
log.info('new connection')
self.write_message('{"key":"value"}')
def on_message(self, message):
//#include <QtCore/QCoreApplication>
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
+#include <QDir>
#include <QDebug>
#include <QObject>
qDebug() << "libpurple initialized, version" << purple->get_version();
RibbonServer* server = new RibbonServer();
- server->listen(QHostAddress::Any, 8888);
+ QDir dir(purple->get_state_dir());
+ QString server_socket_path = dir.absoluteFilePath("socket");
+ bool success = server->listen(server_socket_path);
+ if (!success) {
+ return 1;
+ }
QObject::connect(purple, SIGNAL(event(QString,QString,QVariantMap)),
server, SLOT(sendEventToClient(QString,QString,QVariantMap)));
#include "ribbonsocket.h"
#include "ribbonutil.h"
-#include <QtNetwork/QTcpServer>
-#include <QtNetwork/QTcpSocket>
+#include <QtNetwork/QLocalServer>
+#include <QtNetwork/QLocalSocket>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>
RibbonServer::RibbonServer(QObject* parent) :
QObject(parent)
{
- server = new QTcpServer(this);
+ server = new QLocalServer(this);
QObject::connect(server, SIGNAL(newConnection()),
this, SLOT(onNewConnection()));
}
{
}
-bool RibbonServer::listen(const QHostAddress& address, quint16 port)
+bool RibbonServer::listen(const QString& name)
{
- _addr = address;
- _port = port;
- return server->listen(_addr, _port);
+ _socketname = name;
+ bool success;
+ success = QLocalServer::removeServer(name);
+ if (!success) {
+ return success;
+ }
+ success = server->listen(name);
+ if (success) {
+ qDebug() << "Server listening on" << _socketname;
+ } else {
+ qDebug() << "Failed to listen:" << server->serverError() << server->errorString();
+ }
+ return success;
}
void RibbonServer::onNewConnection()
{
- QTcpSocket* sock = server->nextPendingConnection();
- qDebug() << "Accepted connection from" << sock->peerAddress() << ":" << sock->peerPort();
+ QLocalSocket* sock = server->nextPendingConnection();
+ qDebug() << "Accepted connection";
RibbonSocket* rsock = new RibbonSocket(sock, this);
sockets.append(rsock);
if (sockets.length() >= 1) {
qDebug() << "Socket disconnected:" << r->s();
r->deleteLater();
if (!server->isListening()) {
- server->listen(_addr, _port);
- qDebug() << "Server listening on" << _addr << ":" << _port;
+ server->listen(_socketname);
+ qDebug() << "Server listening on" << _socketname;
}
}
void RibbonServer::onMessageReceived(RibbonSocket* r, QByteArray buf)
{
- qDebug() << "server: got data from" << r->s()->peerAddress() << r->s()->peerPort();
+ qDebug() << "server: got data";
QJsonParseError err;
QJsonDocument json = QJsonDocument::fromJson(buf, &err);
if (json.isNull()) {
#include <QtNetwork/QHostAddress>
// forward declarations
-class QTcpServer;
-class QTcpSocket;
+class QLocalServer;
+class QLocalSocket;
class RibbonSocket;
class RibbonServer : public QObject {
public:
RibbonServer(QObject* parent = 0);
~RibbonServer();
- bool listen(const QHostAddress& address = QHostAddress::Any, quint16 port = 0);
+ bool listen(const QString& name);
public slots:
//void dispatch(QByteArray ba);
void sendEventToClient(QString sender, QString method, QVariantMap ctx);
void newConnection();
void messageReceived(QString destination, QString method, QVariantMap ctx);
private:
- QTcpServer* server;
- QHostAddress _addr;
- quint16 _port;
+ QLocalServer* server;
+ QString _socketname;
QList<RibbonSocket*> sockets;
private slots:
void onNewConnection();
#include "ribbonsocket.h"
-RibbonSocket::RibbonSocket(QTcpSocket* sock, QObject* parent) :
+RibbonSocket::RibbonSocket(QLocalSocket* sock, QObject* parent) :
QObject(parent),
socket(sock),
state(WANT_LENGTH),
{
}
-QTcpSocket* RibbonSocket::s()
+QLocalSocket* RibbonSocket::s()
{
return socket;
}
#define RIBBONSOCKET_H
#include <QtCore/QByteArray>
-#include <QtNetwork/QTcpSocket>
+#include <QtNetwork/QLocalSocket>
class RibbonSocket : public QObject {
Q_OBJECT
};
public:
- RibbonSocket(QTcpSocket* socket, QObject* parent=0);
+ RibbonSocket(QLocalSocket* socket, QObject* parent=0);
// Consider s to be final. Don't modify it.
- QTcpSocket* s();
+ QLocalSocket* s();
void abort();
signals:
void disconnected(RibbonSocket* s);
void messageReceived(RibbonSocket* s, QByteArray buf);
private:
virtual ~RibbonSocket();
- QTcpSocket* socket;
+ QLocalSocket* socket;
QByteArray buf;
RibbonSocketState state;
quint32 bytes_wanted;