summaryrefslogtreecommitdiffstats
path: root/common/listener.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-20 19:27:28 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-10-20 19:27:28 +0200
commit67d9cffe9c8c63a928e4f32b6c07adfea8f21084 (patch)
treeb9e6cd8dd2ed6273a1435ec81e9a931d1fbe3a4a /common/listener.cpp
parentb67ac4f7bad9abc30b99f60e87a1f1725057163b (diff)
downloadsink-67d9cffe9c8c63a928e4f32b6c07adfea8f21084.tar.gz
sink-67d9cffe9c8c63a928e4f32b6c07adfea8f21084.zip
Use a null-resource object to avoid having to check the pointer
everytime
Diffstat (limited to 'common/listener.cpp')
-rw-r--r--common/listener.cpp68
1 files changed, 23 insertions, 45 deletions
diff --git a/common/listener.cpp b/common/listener.cpp
index 55baed9..5468769 100644
--- a/common/listener.cpp
+++ b/common/listener.cpp
@@ -99,14 +99,14 @@ void Listener::closeAllConnections()
99 99
100void Listener::acceptConnection() 100void Listener::acceptConnection()
101{ 101{
102 Trace() << "Accepting connection"; 102 Log() << "Accepting connection";
103 QLocalSocket *socket = m_server->nextPendingConnection(); 103 QLocalSocket *socket = m_server->nextPendingConnection();
104 104
105 if (!socket) { 105 if (!socket) {
106 Warning() << "Accepted connection but didn't get a socket for it";
106 return; 107 return;
107 } 108 }
108 109
109 Log() << "Got a connection";
110 m_connections << Client("Unknown Client", socket); 110 m_connections << Client("Unknown Client", socket);
111 connect(socket, &QIODevice::readyRead, 111 connect(socket, &QIODevice::readyRead,
112 this, &Listener::onDataAvailable); 112 this, &Listener::onDataAvailable);
@@ -116,10 +116,7 @@ void Listener::acceptConnection()
116 116
117 //If this is the first client, set the lower limit for revision cleanup 117 //If this is the first client, set the lower limit for revision cleanup
118 if (m_connections.size() == 1) { 118 if (m_connections.size() == 1) {
119 loadResource(); 119 loadResource()->setLowerBoundRevision(0);
120 if (m_resource) {
121 m_resource->setLowerBoundRevision(0);
122 }
123 } 120 }
124 121
125 if (socket->bytesAvailable()) { 122 if (socket->bytesAvailable()) {
@@ -156,10 +153,7 @@ void Listener::checkConnections()
156{ 153{
157 //If this was the last client, disengage the lower limit for revision cleanup 154 //If this was the last client, disengage the lower limit for revision cleanup
158 if (m_connections.isEmpty()) { 155 if (m_connections.isEmpty()) {
159 loadResource(); 156 loadResource()->setLowerBoundRevision(std::numeric_limits<qint64>::max());
160 if (m_resource) {
161 m_resource->setLowerBoundRevision(std::numeric_limits<qint64>::max());
162 }
163 } 157 }
164 m_checkConnectionsTimer->start(); 158 m_checkConnectionsTimer->start();
165} 159}
@@ -226,17 +220,12 @@ void Listener::processCommand(int commandId, uint messageId, const QByteArray &c
226 if (Akonadi2::VerifySynchronizeBuffer(verifier)) { 220 if (Akonadi2::VerifySynchronizeBuffer(verifier)) {
227 auto buffer = Akonadi2::GetSynchronize(commandBuffer.constData()); 221 auto buffer = Akonadi2::GetSynchronize(commandBuffer.constData());
228 Log() << QString("\tSynchronize request (id %1) from %2").arg(messageId).arg(client.name); 222 Log() << QString("\tSynchronize request (id %1) from %2").arg(messageId).arg(client.name);
229 loadResource();
230 if (!m_resource) {
231 Warning() << "No resource loaded";
232 break;
233 }
234 auto job = KAsync::null<void>(); 223 auto job = KAsync::null<void>();
235 if (buffer->sourceSync()) { 224 if (buffer->sourceSync()) {
236 job = m_resource->synchronizeWithSource(); 225 job = loadResource()->synchronizeWithSource();
237 } 226 }
238 if (buffer->localSync()) { 227 if (buffer->localSync()) {
239 job = job.then<void>(m_resource->processAllMessages()); 228 job = job.then<void>(loadResource()->processAllMessages());
240 } 229 }
241 job.then<void>([callback]() { 230 job.then<void>([callback]() {
242 callback(); 231 callback();
@@ -252,10 +241,7 @@ void Listener::processCommand(int commandId, uint messageId, const QByteArray &c
252 case Akonadi2::Commands::ModifyEntityCommand: 241 case Akonadi2::Commands::ModifyEntityCommand:
253 case Akonadi2::Commands::CreateEntityCommand: 242 case Akonadi2::Commands::CreateEntityCommand:
254 Log() << "\tCommand id " << messageId << " of type \"" << Akonadi2::Commands::name(commandId) << "\" from " << client.name; 243 Log() << "\tCommand id " << messageId << " of type \"" << Akonadi2::Commands::name(commandId) << "\" from " << client.name;
255 loadResource(); 244 loadResource()->processCommand(commandId, commandBuffer);
256 if (m_resource) {
257 m_resource->processCommand(commandId, commandBuffer);
258 }
259 break; 245 break;
260 case Akonadi2::Commands::ShutdownCommand: 246 case Akonadi2::Commands::ShutdownCommand:
261 Log() << QString("\tReceived shutdown command from %1").arg(client.name); 247 Log() << QString("\tReceived shutdown command from %1").arg(client.name);
@@ -275,19 +261,13 @@ void Listener::processCommand(int commandId, uint messageId, const QByteArray &c
275 } else { 261 } else {
276 Warning() << "received invalid command"; 262 Warning() << "received invalid command";
277 } 263 }
278 loadResource(); 264 loadResource()->setLowerBoundRevision(lowerBoundRevision());
279 if (m_resource) {
280 m_resource->setLowerBoundRevision(lowerBoundRevision());
281 }
282 } 265 }
283 break; 266 break;
284 default: 267 default:
285 if (commandId > Akonadi2::Commands::CustomCommand) { 268 if (commandId > Akonadi2::Commands::CustomCommand) {
286 Log() << QString("\tReceived custom command from %1: ").arg(client.name) << commandId; 269 Log() << QString("\tReceived custom command from %1: ").arg(client.name) << commandId;
287 loadResource(); 270 loadResource()->processCommand(commandId, commandBuffer);
288 if (m_resource) {
289 m_resource->processCommand(commandId, commandBuffer);
290 }
291 } else { 271 } else {
292 ErrorMsg() << QString("\tReceived invalid command from %1: ").arg(client.name) << commandId; 272 ErrorMsg() << QString("\tReceived invalid command from %1: ").arg(client.name) << commandId;
293 } 273 }
@@ -391,28 +371,26 @@ void Listener::updateClientsWithRevision(qint64 revision)
391 continue; 371 continue;
392 } 372 }
393 373
374 Trace() << "Sending revision update for " << client.name << revision;
394 Akonadi2::Commands::write(client.socket, ++m_messageId, Akonadi2::Commands::RevisionUpdateCommand, m_fbb); 375 Akonadi2::Commands::write(client.socket, ++m_messageId, Akonadi2::Commands::RevisionUpdateCommand, m_fbb);
395 } 376 }
396 m_fbb.Clear(); 377 m_fbb.Clear();
397} 378}
398 379
399void Listener::loadResource() 380Akonadi2::Resource *Listener::loadResource()
400{ 381{
401 if (m_resource) { 382 if (!m_resource) {
402 return; 383 if (Akonadi2::ResourceFactory *resourceFactory = Akonadi2::ResourceFactory::load(m_resourceName)) {
403 } 384 m_resource = resourceFactory->createResource(m_resourceInstanceIdentifier);
404 385 Trace() << QString("Resource factory: %1").arg((qlonglong)resourceFactory);
405 Akonadi2::ResourceFactory *resourceFactory = Akonadi2::ResourceFactory::load(m_resourceName); 386 Trace() << QString("\tResource: %1").arg((qlonglong)m_resource);
406 if (resourceFactory) { 387 connect(m_resource, &Akonadi2::Resource::revisionUpdated,
407 m_resource = resourceFactory->createResource(m_resourceInstanceIdentifier); 388 this, &Listener::refreshRevision);
408 Trace() << QString("Resource factory: %1").arg((qlonglong)resourceFactory); 389 } else {
409 Trace() << QString("\tResource: %1").arg((qlonglong)m_resource); 390 ErrorMsg() << "Failed to load resource " << m_resourceName;
410 connect(m_resource, &Akonadi2::Resource::revisionUpdated, 391 m_resource = new Akonadi2::Resource;
411 this, &Listener::refreshRevision); 392 }
412 } else {
413 ErrorMsg() << "Failed to load resource " << m_resourceName;
414 } 393 }
415 //TODO: on failure ... what? 394 return m_resource;
416 //Enter broken state?
417} 395}
418 396