Skip to content

Commit

Permalink
feat: Add function numberOfInstances()
Browse files Browse the repository at this point in the history
This function returns the number of running instances, by add a new variable in InstancesInfo.

This feature is asked in itay-grudev#89.
  • Loading branch information
ouuan committed Jan 29, 2020
1 parent 473dfae commit 5b883c9
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ qint64 SingleApplication::primaryPid()

Returns the process ID (PID) of the primary instance.

---

```cpp
quint32 numberOfInstances()
```

Returns the number of running instances.

### Signals

```cpp
Expand Down
7 changes: 7 additions & 0 deletions singleapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,10 @@ bool SingleApplication::sendMessage( QByteArray message, int timeout )
d->socket->flush();
return dataWritten;
}

quint32 SingleApplication::numberOfInstances()
{
Q_D(SingleApplication);
InstancesInfo* inst = static_cast<InstancesInfo*>( d->memory->data() );
return inst->numberOfInstances;
}
6 changes: 6 additions & 0 deletions singleapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ class SingleApplication : public QAPPLICATION_CLASS
*/
bool sendMessage( QByteArray message, int timeout = 100 );

/**
* @brief Returns the number of running instances
* @returns {quint32}
*/
quint32 numberOfInstances();

Q_SIGNALS:
void instanceStarted();
void receivedMessage( quint32 instanceId, QByteArray message );
Expand Down
5 changes: 5 additions & 0 deletions singleapplication_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ SingleApplicationPrivate::~SingleApplicationPrivate()

memory->lock();
InstancesInfo* inst = static_cast<InstancesInfo*>(memory->data());
inst->numberOfInstances -= 1;
if( server != nullptr ) {
server->close();
delete server;
Expand Down Expand Up @@ -141,6 +142,7 @@ void SingleApplicationPrivate::initializeMemoryBlock()
inst->secondary = 0;
inst->primaryPid = -1;
inst->checksum = blockChecksum();
inst->numberOfInstances = 0;
}

void SingleApplicationPrivate::startPrimary()
Expand Down Expand Up @@ -174,12 +176,15 @@ void SingleApplicationPrivate::startPrimary()
inst->primary = true;
inst->primaryPid = q->applicationPid();
inst->checksum = blockChecksum();
inst->numberOfInstances += 1;

instanceNumber = 0;
}

void SingleApplicationPrivate::startSecondary()
{
InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() );
inst->numberOfInstances += 1;
}

void SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType )
Expand Down
1 change: 1 addition & 0 deletions singleapplication_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct InstancesInfo {
quint32 secondary;
qint64 primaryPid;
quint16 checksum;
quint32 numberOfInstances;
};

struct ConnectionInfo {
Expand Down

0 comments on commit 5b883c9

Please sign in to comment.