Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gjacquemin committed Aug 6, 2018
1 parent 0231d03 commit 39146d0
Show file tree
Hide file tree
Showing 138 changed files with 19,346 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
*.pro.user
38 changes: 38 additions & 0 deletions Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleName</key>
<string>Realtime Message Broker</string>
<key>CFBundleExecutable</key>
<string>Realtime Message Broker</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>CFBundleIdentifier</key>
<string>com.buzzinglight.RTBroker</string>
<key>NSHumanReadableCopyright</key>
<string>Buzzing Light</string>
<key>CFBundleIconFile</key>
<string>icon.icns</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleGetInfoString</key>
<string>Buzzing Light</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>LSMinimumSystemVersion</key>
<string>10.6.0</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSHighResolutionCapable</key>
<string>True</string>
<key>LSAppNapIsDisabled</key>
<true/>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# RTBroker
# Realtime Message Broker
Realtime Message Broker is a message broker designed for realtime communication (OSC, HTTP, Websockets, Arduino) in creative technologies.
56 changes: 56 additions & 0 deletions RTBroker.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
include(SingleApplication/singleapplication.pri)
DEFINES += QAPPLICATION_CLASS=QApplication

QT += core widgets network


unix:TARGET = "RTBroker"
macx:TARGET = "Realtime Message Broker"
win32:TARGET = "Realtime Message Broker"
TEMPLATE = app

SOURCES += main.cpp

RESOURCES += RTBroker.qrc

#Interfaces
SOURCES += mainwindow.cpp interfacehttp.cpp serial.cpp udp.cpp global.cpp
HEADERS += mainwindow.h interfacehttp.h serial.h udp.h global.h
FORMS += mainwindow.ui

#Websockets
SOURCES += websockets.cpp qwebsockets/websocket.cpp qwebsockets/websocketserver.cpp qwebsockets/websocketprotocol.cpp qwebsockets/handshakerequest.cpp qwebsockets/handshakeresponse.cpp qwebsockets/dataprocessor.cpp
HEADERS += websockets.h qwebsockets/websocket.h qwebsockets/websocketserver.h qwebsockets/websocketprotocol.h qwebsockets/handshakerequest.h qwebsockets/handshakeresponse.h qwebsockets/dataprocessor.h

#WebApp
HEADERS += http/http.h http/requestmapper.h http/static.h http/filecontroller.h
SOURCES += http/http.cpp http/requestmapper.cpp http/static.cpp http/filecontroller.cpp
true {
HEADERS += http/lib/dumpcontroller.h http/lib/templatecontroller.h http/lib/formcontroller.h http/lib/fileuploadcontroller.h http/lib/sessioncontroller.h
SOURCES += http/lib/dumpcontroller.cpp http/lib/templatecontroller.cpp http/lib/formcontroller.cpp http/lib/fileuploadcontroller.cpp http/lib/sessioncontroller.cpp
include(http/lib/qtservice/src/qtservice.pri)
include(http/lib/bfLogging/src/bfLogging.pri)
include(http/lib/bfHttpServer/src/bfHttpServer.pri)
include(http/lib/bfTemplateEngine/src/bfTemplateEngine.pri)
}

#Serial
macx:LIBS += -framework CoreFoundation -framework IOKit -framework Carbon
macx:SOURCES += qextserialport/qextserialenumerator_osx.cpp
win32:SOURCES += qextserialport/win_qextserialport.cpp qextserialport/qextserialenumerator_win.cpp
unix:SOURCES += qextserialport/posix_qextserialport.cpp
unix:!macx:SOURCES += qextserialport/qextserialenumerator_unix.cpp
SOURCES += qextserialport/qextserialport.cpp
HEADERS += qextserialport/qextserialport.h qextserialport/qextserialenumerator.h qextserialport/qextserialport_global.h
win32:DEFINES += WINVER=0x0501
win32:LIBS += -lsetupapi -ladvapi32 -luser32
#HEADERS += qextserialport/qextserialport.h qextserialport/qextserialenumerator.h qextserialport/qextserialport_global.h qextserialport/qextserialport_p.h qextserialport/qextserialenumerator_p.h
#SOURCES += qextserialport/qextserialport.cpp qextserialport/qextserialenumerator.cpp

#Icons & co
macx {
ICON = icons/icon.icns
QMAKE_INFO_PLIST = Info.plist
}
win32:RC_FILE = icon.rc

6 changes: 6 additions & 0 deletions RTBroker.qrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/icons">
<file alias="icon_black">icons/icon_black.png</file>
<file alias="icon">icons/icon.png</file>
</qresource>
</RCC>
174 changes: 174 additions & 0 deletions SingleApplication/singleapplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// The MIT License (MIT)
//
// Copyright (c) Itay Grudev 2015 - 2018
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include <QtCore/QTime>
#include <QtCore/QThread>
#include <QtCore/QDateTime>
#include <QtCore/QByteArray>
#include <QtCore/QSharedMemory>

#include "singleapplication.h"
#include "singleapplication_p.h"

/**
* @brief Constructor. Checks and fires up LocalServer or closes the program
* if another instance already exists
* @param argc
* @param argv
* @param {bool} allowSecondaryInstances
*/
SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSecondary, Options options, int timeout )
: app_t( argc, argv ), d_ptr( new SingleApplicationPrivate( this ) )
{
Q_D(SingleApplication);

// Store the current mode of the program
d->options = options;

// Generating an application ID used for identifying the shared memory
// block and QLocalServer
d->genBlockServerName();

#ifdef Q_OS_UNIX
// By explicitly attaching it and then deleting it we make sure that the
// memory is deleted even after the process has crashed on Unix.
d->memory = new QSharedMemory( d->blockServerName );
d->memory->attach();
delete d->memory;
#endif
// Guarantee thread safe behaviour with a shared memory block.
d->memory = new QSharedMemory( d->blockServerName );

// Create a shared memory block
if( d->memory->create( sizeof( InstancesInfo ) ) ) {
// Initialize the shared memory block
d->memory->lock();
d->initializeMemoryBlock();
d->memory->unlock();
} else {
// Attempt to attach to the memory segment
if( ! d->memory->attach() ) {
qCritical() << "SingleApplication: Unable to attach to shared memory block.";
qCritical() << d->memory->errorString();
delete d;
::exit( EXIT_FAILURE );
}
}

InstancesInfo* inst = static_cast<InstancesInfo*>( d->memory->data() );
QTime time;
time.start();

// Make sure the shared memory block is initialised and in consistent state
while( true ) {
d->memory->lock();

if( d->blockChecksum() == inst->checksum ) break;

if( time.elapsed() > 5000 ) {
qWarning() << "SingleApplication: Shared memory block has been in an inconsistent state from more than 5s. Assuming primary instance failure.";
d->initializeMemoryBlock();
}

d->memory->unlock();

// Random sleep here limits the probability of a colision between two racing apps
qsrand( QDateTime::currentMSecsSinceEpoch() % std::numeric_limits<uint>::max() );
QThread::sleep( 8 + static_cast <unsigned long>( static_cast <float>( qrand() ) / RAND_MAX * 10 ) );
}

if( inst->primary == false) {
d->startPrimary();
d->memory->unlock();
return;
}

// Check if another instance can be started
if( allowSecondary ) {
inst->secondary += 1;
inst->checksum = d->blockChecksum();
d->instanceNumber = inst->secondary;
d->startSecondary();
if( d->options & Mode::SecondaryNotification ) {
d->connectToPrimary( timeout, SingleApplicationPrivate::SecondaryInstance );
}
d->memory->unlock();
return;
}

d->memory->unlock();

d->connectToPrimary( timeout, SingleApplicationPrivate::NewInstance );

delete d;

::exit( EXIT_SUCCESS );
}

/**
* @brief Destructor
*/
SingleApplication::~SingleApplication()
{
Q_D(SingleApplication);
delete d;
}

bool SingleApplication::isPrimary()
{
Q_D(SingleApplication);
return d->server != nullptr;
}

bool SingleApplication::isSecondary()
{
Q_D(SingleApplication);
return d->server == nullptr;
}

quint32 SingleApplication::instanceId()
{
Q_D(SingleApplication);
return d->instanceNumber;
}

qint64 SingleApplication::primaryPid()
{
Q_D(SingleApplication);
return d->primaryPid();
}

bool SingleApplication::sendMessage( QByteArray message, int timeout )
{
Q_D(SingleApplication);

// Nobody to connect to
if( isPrimary() ) return false;

// Make sure the socket is connected
d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect );

d->socket->write( message );
bool dataWritten = d->socket->flush();
d->socket->waitForBytesWritten( timeout );
return dataWritten;
}
Loading

0 comments on commit 39146d0

Please sign in to comment.