qtmobility/src/publishsubscribe/sharedmemorylayer.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
  1919         connections.insert(protocol);
  1919         connections.insert(protocol);
  1920     }
  1920     }
  1921 
  1921 
  1922     if(Server == type) {
  1922     if(Server == type) {
  1923         shm = new QSharedMemory(socket(), this);
  1923         shm = new QSharedMemory(socket(), this);
  1924         bool created = shm->create(SHMLAYER_SIZE);
  1924         // create or reattach to existing shared memory
  1925         if (!created)  {
  1925         if (!shm->create(SHMLAYER_SIZE))
  1926             //qDebug() << "Reattaching to existing memory";
       
  1927             shm->attach();
  1926             shm->attach();
  1928         }
  1927 
  1929         lock = new QSystemReadWriteLock(socket() + "_lock", QSystemReadWriteLock::Create);
  1928         if (shm->error() != QSharedMemory::NoError) {
       
  1929             qFatal("SharedMemoryLayer: Unable to create or access shared resource. %d(%s)",
       
  1930                    shm->error(),
       
  1931                    shm->errorString().toLatin1().constData());
       
  1932         }
       
  1933         lock = new QSystemReadWriteLock(socket() + QLatin1String("_lock"),
       
  1934                                         QSystemReadWriteLock::Create);
  1930     } else {
  1935     } else {
  1931         shm = new QSharedMemory(socket(), this);
  1936         shm = new QSharedMemory(socket(), this);
  1932         shm->attach(QSharedMemory::ReadOnly);
  1937         shm->attach(QSharedMemory::ReadOnly);
       
  1938         if (shm->error() != QSharedMemory::NoError) {
       
  1939             qFatal("SharedMemoryLayer: Unable to access shared memory. %d(%s)",
       
  1940                    shm->error(), shm->errorString().toLatin1().constData());
       
  1941         }
       
  1942 
  1933         qsrand(QTime(0,0,0).secsTo(QTime::currentTime())+QCoreApplication::applicationPid());
  1943         qsrand(QTime(0,0,0).secsTo(QTime::currentTime())+QCoreApplication::applicationPid());
  1934         subShm = new QSharedMemory(socket()+QString::number(qrand()), this);
  1944         subShm = new QSharedMemory(socket()+QString::number(qrand()), this);
  1935         if (!subShm->create((VERSION_TABLE_ENTRIES + 7) / 8, QSharedMemory::ReadWrite)) {
  1945         if (!subShm->create((VERSION_TABLE_ENTRIES + 7) / 8, QSharedMemory::ReadWrite)) {
  1936             qWarning() << "SharedMemoryLayer client cannot create clientIndex:"
  1946             qWarning("SharedMemoryLayer: client cannot create clientIndex: %s %s",
  1937                        << subShm->errorString() << subShm->key();
  1947                      subShm->errorString().toLatin1().constData(), subShm->key().toLatin1().constData());
  1938         }
  1948         }
  1939 
  1949 
  1940         lock = new QSystemReadWriteLock(socket() + "_lock", QSystemReadWriteLock::Open);
  1950         lock = new QSystemReadWriteLock(socket() + QLatin1String("_lock"),
  1941     }
  1951                                         QSystemReadWriteLock::Open);
  1942 
       
  1943     if (shm->error() != QSharedMemory::NoError ||
       
  1944         ((!subShm || subShm->error()!= QSharedMemory::NoError) && Server != type)) {
       
  1945         qFatal("SharedMemoryLayer: Unable to create or access shared resources. (%s - %s)",
       
  1946                shm->errorString().toLatin1().constData(),
       
  1947                subShm->errorString().toLatin1().constData());
       
  1948         return false;
       
  1949     }
  1952     }
  1950 
  1953 
  1951     if (subShm)
  1954     if (subShm)
  1952         clientIndex = (uchar *)subShm->data();
  1955         clientIndex = (uchar *)subShm->data();
  1953     if(Client == type) {
  1956     if(Client == type) {
  2078         NodeOwner owner;
  2081         NodeOwner owner;
  2079         owner.data1 = (unsigned long)protocol;
  2082         owner.data1 = (unsigned long)protocol;
  2080         owner.data2 = 0xFFFFFFFF;
  2083         owner.data2 = 0xFFFFFFFF;
  2081 
  2084 
  2082         connections.remove(protocol);
  2085         connections.remove(protocol);
  2083         if(layer->remove("/", owner)) {
  2086         if (layer->remove("/", owner)) {
  2084             QPacket others;
  2087             QPacket others;
  2085             others << (quint8)SHMLAYER_SYNC << (unsigned int)0;
  2088             others << (quint8)SHMLAYER_SYNC << (unsigned int)0;
  2086             for(QSet<QPacketProtocol *>::ConstIterator iter = connections.begin();
  2089             for(QSet<QPacketProtocol *>::ConstIterator iter = connections.begin();
  2087                     iter != connections.end();
  2090                     iter != connections.end();
  2088                     ++iter)
  2091                     ++iter)
  2855 
  2858 
  2856 bool SharedMemoryLayer::setWatch(NodeWatch watch, const QByteArray &path)
  2859 bool SharedMemoryLayer::setWatch(NodeWatch watch, const QByteArray &path)
  2857 {
  2860 {
  2858     QMutexLocker locker(&localLock);
  2861     QMutexLocker locker(&localLock);
  2859 
  2862 
  2860     if(path.count() > MAX_PATH_SIZE || path.startsWith("/.ValueSpace") || !valid)
  2863     if (path.count() > MAX_PATH_SIZE || path.startsWith("/.ValueSpace") || !valid)
  2861         return false;
  2864         return false;
  2862     Q_ASSERT(layer);
  2865     Q_ASSERT(layer);
  2863 
  2866 
  2864     if(Client == type) {
  2867     if(Client == type) {
  2865         if(todo.isEmpty())
  2868         if(todo.isEmpty())
  2930 bool SharedMemoryLayer::setItem(NodeOwner owner, const QByteArray &path,
  2933 bool SharedMemoryLayer::setItem(NodeOwner owner, const QByteArray &path,
  2931                                const QVariant &val)
  2934                                const QVariant &val)
  2932 {
  2935 {
  2933     QMutexLocker locker(&localLock);
  2936     QMutexLocker locker(&localLock);
  2934 
  2937 
  2935     if(path.count() > MAX_PATH_SIZE || path.startsWith("/.ValueSpace") || !valid)
  2938     if (path.count() > MAX_PATH_SIZE || path.startsWith("/.ValueSpace") || !valid)
  2936         return false;
  2939         return false;
  2937     Q_ASSERT(layer);
  2940     Q_ASSERT(layer);
  2938     bool changed = false;
  2941     bool changed = false;
  2939 
  2942 
  2940     if(Client == type) {
  2943     if(Client == type) {
  3302 
  3305 
  3303     QByteArray fullPath(readHandle->path);
  3306     QByteArray fullPath(readHandle->path);
  3304     if (!fullPath.endsWith('/') && path != QLatin1String("/"))
  3307     if (!fullPath.endsWith('/') && path != QLatin1String("/"))
  3305         fullPath.append('/');
  3308         fullPath.append('/');
  3306 
  3309 
  3307     fullPath.append(path.mid(1));
  3310     fullPath.append(path.mid(1).toUtf8());
  3308 
  3311 
  3309     return setItem(owner, fullPath, data);
  3312     return setItem(owner, fullPath, data);
  3310 }
  3313 }
  3311 
  3314 
  3312 bool SharedMemoryLayer::removeValue(QValueSpacePublisher *creator,
  3315 bool SharedMemoryLayer::removeValue(QValueSpacePublisher *creator,
  3330 
  3333 
  3331     int index = 0;
  3334     int index = 0;
  3332     while (index < path.length() && path[index] == QLatin1Char('/'))
  3335     while (index < path.length() && path[index] == QLatin1Char('/'))
  3333         ++index;
  3336         ++index;
  3334 
  3337 
  3335     fullPath.append(path.mid(index));
  3338     fullPath.append(path.mid(index).toUtf8());
  3336 
  3339 
  3337     return remItems(owner, fullPath);
  3340     return remItems(owner, fullPath);
  3338 }
  3341 }
  3339 
  3342 
  3340 bool SharedMemoryLayer::removeSubTree(QValueSpacePublisher *creator, Handle handle)
  3343 bool SharedMemoryLayer::removeSubTree(QValueSpacePublisher *creator, Handle handle)