示例#1
0
/**
 * @brief Executed when new instance connect command is sent to LocalServer
 */
void Rshare::slotConnectionEstablished()
{
	QLocalSocket *socket = localServer->nextPendingConnection();
	socket->close();
	delete socket;

	QSharedMemory newArgs;
	newArgs.setKey(QString(TARGET) + "_newArgs");

	if (!newArgs.attach())
	{
		std::cerr << "(EE) Rshare::slotConnectionEstablished() Unable to attach to shared memory segment."
		          << newArgs.errorString().toStdString() << std::endl;
		return;
	}

	QBuffer buffer;
	QDataStream in(&buffer);
	QStringList args;

	newArgs.lock();
	buffer.setData((char*)newArgs.constData(), newArgs.size());
	buffer.open(QBuffer::ReadOnly);
	in >> args;
	newArgs.unlock();
	newArgs.detach();

	emit newArgsReceived(args);
	while (!args.empty())
	{
		std::cerr << "Rshare::slotConnectionEstablished args:" << QString(args.takeFirst()).toStdString() << std::endl;
	}
}
/*!
    Destructor
*/
TasDataShare::~TasDataShare()
{
    QMutableHashIterator<QString, QSharedMemory*> i(mStoredDataBlocks);
    while (i.hasNext()) {
        i.next();
        QSharedMemory* mem = i.value();
        mem->detach();
        delete mem;
    }
    mStoredDataBlocks.clear();
}
示例#3
0
void conflict_core::openAida(){
    aida.setKey("AIDA64_SensorValues");
    if (aida.isAttached()){
          aida.detach();
    }
    if (!aida.create(10000)){
        conflict.aidaOpen = false;
    }else{
        conflict.aidaOpen = true;
    }
}
bool TasDataShare::detachSharedData(const QString& identifier)
{
    if(mStoredDataBlocks.contains(identifier)){
        QSharedMemory* mem = mStoredDataBlocks.value(identifier);
        if(!mem->detach()){
            return false;
        }
        delete mem;
        mem = 0;
        mStoredDataBlocks.remove(identifier);
    }
    return true;
}
示例#5
0
int consumer()
{
    QSharedMemory consumer;
    consumer.setKey("market");

    //qDebug("consumer starting");
    int tries = 0;
    while (!consumer.attach()) {
        if (tries == 5000) {
            qWarning() << "consumer exiting, waiting too long";
            return EXIT_FAILURE;
        }
        ++tries;
        QTest::qSleep(1);
    }
    //qDebug("consumer attached");


    int i = 0;
    while (true) {
        if (!consumer.lock()) {
            qWarning() << "Could not lock" << consumer.key();
            return EXIT_FAILURE;
        }
        if (get(consumer, 0) == 'Q') {
            set(consumer, 0, ++i);
            //qDebug() << "consumer sets" << i;
        }
        if (get(consumer, 0) == 'E') {
            if (!consumer.unlock()) {
                qWarning() << "Could not unlock" << consumer.key();
                return EXIT_FAILURE;
            }
            break;
        }
        if (!consumer.unlock()) {
            qWarning() << "Could not unlock" << consumer.key();
            return EXIT_FAILURE;
        }
        QTest::qSleep(10);
    }

    //qDebug("consumer detaching");
    if (!consumer.detach()) {
        qWarning() << "Could not detach" << consumer.key();
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}
示例#6
0
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
{
    QSharedMemory *sharedMemory = new QSharedMemory(QString(valueKeyTemplateString).arg(key));

    bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
    if (!sharedMemoryIsCreated) {
        if (sharedMemory->isAttached())
            sharedMemory->attach();
        sharedMemory->detach();
        sharedMemoryIsCreated = sharedMemory->create(byteCount);
    }

    if (sharedMemoryIsCreated) {
        globalSharedMemoryCache.insert(key, sharedMemory);
        return sharedMemory;
    }

    return 0;
}
示例#7
0
void setBool(bool value, QString name) {
    QSharedMemory* sharedMemory = boolMemoryMap.value(name);

    if(sharedMemory->isAttached()) {
        sharedMemory->detach();
    }

    int size = sizeof(bool);

    if (!sharedMemory->create(size)) {
        sharedMemory->attach(QSharedMemory::ReadWrite);
    }

    sharedMemory->lock();

    bool *array = (bool*)sharedMemory->data();
    memcpy(array, &value, qMin(sharedMemory->size(), size));

    sharedMemory->unlock();
}
示例#8
0
void setString(const char value[], QString name) {
    QSharedMemory* sharedMemory = stringMemoryMap.value(name);

    if(sharedMemory->isAttached())
        sharedMemory->detach();

    int size = strlen(value) + sizeof(char);

    if (!sharedMemory->create(size)) {
        if (!sharedMemory->isAttached()) {
            sharedMemory->attach(QSharedMemory::ReadWrite);
        }
    }

    sharedMemory->lock();

    char *to = (char*)sharedMemory->data();
    memcpy(to, value, qMin(sharedMemory->size(), size));

    sharedMemory->unlock();
}
示例#9
0
文件: main.cpp 项目: bvgastel/xmas
// Example how to get json using shared memory. Not tested :-D
std::pair<bool, QString> json() {

    QSharedMemory sharedMemory;

    if (!sharedMemory.attach()) {
        return std::make_pair<bool, QString>(false, QString());
    }

    QBuffer buffer;
    QDataStream in(&buffer);
    QString json;

    sharedMemory.lock();
    buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    in >> json;
    sharedMemory.unlock();

    sharedMemory.detach();
    return std::make_pair<bool, QString>(false, QString());
}
示例#10
0
int getInt(QString name) {
    QSharedMemory* sharedMemory = intMemoryMap[name];

    sharedMemory->attach(QSharedMemory::ReadOnly);

    int returnValue = 0;

    if(!sharedMemory->isAttached()) {
        return returnValue;
    }

    sharedMemory->lock();

    int* value = (int*)sharedMemory->data();
    memcpy(&returnValue, value, sizeof(value));

    sharedMemory->unlock();

    sharedMemory->detach();

    return returnValue;
}
示例#11
0
void setInt(int value, QString name) {
    QSharedMemory* sharedMemory = intMemoryMap.value(name);

    if(sharedMemory->isAttached())
        sharedMemory->detach();

    int size = sizeof(int);

    if (!sharedMemory->create(size)) {
        if (!sharedMemory->isAttached()) {
            sharedMemory->attach(QSharedMemory::ReadWrite);
        }
    }

    sharedMemory->lock();

    int* to = (int*)sharedMemory->data();

    memcpy(to, &value, qMin(sharedMemory->size(), size));

    sharedMemory->unlock();
}
示例#12
0
bool getBool(QString name) {
    QSharedMemory* sharedMemory = boolMemoryMap[name];

    sharedMemory->attach(QSharedMemory::ReadOnly);

    bool returnValue = false;

    if(!sharedMemory->isAttached()) {
        return returnValue;
    }

    sharedMemory->lock();

    bool value = *(bool*)sharedMemory->data();

    returnValue = value;

    sharedMemory->unlock();

    sharedMemory->detach();

    return returnValue;
}
示例#13
0
char* getString(QString name) {
    QSharedMemory* sharedMemory = stringMemoryMap[name];

    sharedMemory->attach(QSharedMemory::ReadOnly);

    char* strValue = (char*)"";

    if(!sharedMemory->isAttached()) {
        return strValue;
    }

    sharedMemory->lock();

    char* shared = (char*)sharedMemory->data();
    strValue = new char[strlen(shared) + sizeof(char)];
    strcpy(strValue, shared);

    sharedMemory->unlock();

    sharedMemory->detach();

    return strValue;
}
示例#14
0
bool detach_jt9_() {return mem_jt9.detach();}
示例#15
0
/** Constructor. Parses the command-line arguments, resets Rshare's
 * configuration (if requested), and sets up the GUI style and language
 * translation. */
Rshare::Rshare(QStringList args, int &argc, char **argv, const QString &dir)
: QApplication(argc, argv)
{
  mStartupTime = QDateTime::currentDateTime();
  localServer = NULL;

  //Initialize connection to LocalServer to know if other process runs.
  {
    QString serverName = QString(TARGET);

    if (!args.isEmpty()) {
      // load into shared memory
      QBuffer buffer;
      buffer.open(QBuffer::ReadWrite);
      QDataStream out(&buffer);
      out << args;
      int size = buffer.size();

      QSharedMemory newArgs;
      newArgs.setKey(serverName + "_newArgs");
      if (newArgs.isAttached()) newArgs.detach();

      if (!newArgs.create(size)) {
        std::cerr << "(EE) Rshare::Rshare Unable to create shared memory segment of size:"
                  << size << " error:" << newArgs.errorString().toStdString() << "." << std::endl;
#ifdef Q_OS_UNIX
        std::cerr << "Look with `ipcs -m` for nattch==0 segment. And remove it with `ipcrm -m 'shmid'`." << std::endl;
        //No need for windows, as it removes shared segment directly even when crash.
#endif
        newArgs.detach();
        ::exit(EXIT_FAILURE);
      }
      newArgs.lock();
      char *to = (char*)newArgs.data();
      const char *from = buffer.data().data();
      memcpy(to, from, qMin(newArgs.size(), size));
      newArgs.unlock();

      // Connect to the Local Server of the main process to notify it
      // that a new process had been started
      QLocalSocket localSocket;
      localSocket.connectToServer(QString(TARGET));

      std::cerr << "Rshare::Rshare waitForConnected to other instance." << std::endl;
      if( localSocket.waitForConnected(100) )
      {
        std::cerr << "Rshare::Rshare Connection etablished. Waiting for disconnection." << std::endl;
        localSocket.waitForDisconnected(1000);
        newArgs.detach();
        std::cerr << "Rshare::Rshare Arguments was sended." << std::endl
                  << " To disable it, in Options - General - Misc," << std::endl
                  << " uncheck \"Use Local Server to get new Arguments\"." << std::endl;
        ::exit(EXIT_SUCCESS); // Terminate the program using STDLib's exit function
      }
      newArgs.detach();
    }
    // No main process exists
    // Or started without arguments
    // So we start a Local Server to listen for connections from new process
    localServer= new QLocalServer();
    QObject::connect(localServer, SIGNAL(newConnection()), this, SLOT(slotConnectionEstablished()));
    updateLocalServer();
  }

#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
  qInstallMessageHandler(qt_msg_handler);
#else
  qInstallMsgHandler(qt_msg_handler);
#endif

#ifndef __APPLE__

  /* set default window icon */
  setWindowIcon(QIcon(":/icons/logo_128.png"));

#endif

  mBlink = true;
  QTimer *timer = new QTimer(this);
  timer->setInterval(500);
  connect(timer, SIGNAL(timeout()), this, SLOT(blinkTimer()));
  timer->start();

  timer = new QTimer(this);
  timer->setInterval(60000);
  connect(timer, SIGNAL(timeout()), this, SIGNAL(minuteTick()));
  timer->start();

  /* Read in all our command-line arguments. */
  parseArguments(args);

  /* Check if we're supposed to reset our config before proceeding. */
  if (_args.contains(ARG_RESET)) {
    Settings->reset();
  }
  
  /* Handle the -loglevel and -logfile options. */
  if (_args.contains(ARG_LOGFILE))
    _log.open(_args.value(ARG_LOGFILE));
  if (_args.contains(ARG_LOGLEVEL)) {
    _log.setLogLevel(Log::stringToLogLevel(
                      _args.value(ARG_LOGLEVEL)));
    if (!_args.contains(ARG_LOGFILE))
      _log.open(stdout);
  }
  if (!_args.contains(ARG_LOGLEVEL) && 
      !_args.contains(ARG_LOGFILE))
    _log.setLogLevel(Log::Off);

  /* config directory */
  useConfigDir = false;
  if (dir != "")
  {
  	setConfigDirectory(dir);
  }

  /** Initialize support for language translations. */
  //LanguageSupport::initialize();

  resetLanguageAndStyle();

  /* Switch off auto shutdown */
  setQuitOnLastWindowClosed ( false );

	/* Initialize GxsIdDetails */
	GxsIdDetails::initialize();
}