PAPIMessage * sendRequest( PAPIConnection * inConnection, PAPIMessage * inRequestMessage, PAPIStatus * outStatus ) { int theContentLength; char theContentLengthString[ MAX_CONTENT_LENGTH_SIZE ]; char * theRequest; PAPIMessage * theResponse = 0; *outStatus = PAPISuccess; if ( inConnection == 0 || inConnection->mFD == INVALID_SOCKET ) { *outStatus = PAPIConnectionFailure; return 0; } theRequest = messageToString( inRequestMessage ); if ( theRequest != 0 && ( theContentLength = strlen( theRequest ) ) > 0 ) { char * theBuf; int theSize; snprintf( theContentLengthString, MAX_CONTENT_LENGTH_SIZE, "%s%d\r\n", CONTENTLENGTH, theContentLength ); deleteMessage( inConnection->mSavedMessage ); inConnection->mSavedMessage = 0; theSize = strlen( theContentLengthString ) + theContentLength + 1; theBuf = ( char * )malloc( theSize ); if ( theBuf == 0 ) { *outStatus = PAPIOutOfMemoryFailure; free( ( void * )theRequest ); return 0; } snprintf( theBuf, theSize, "%s%s", theContentLengthString, theRequest ); if ( send( inConnection->mFD, ( void * )theBuf, theSize - 1, 0 ) == SOCKET_ERROR ) { *outStatus = PAPIConnectionFailure; free( ( void * )theBuf ); free( ( void * )theRequest ); return 0; } free( ( void * )theBuf ); free( ( void * )theRequest ); theResponse = getResponse( inConnection, outStatus ); } else { free( ( void * )theRequest ); } return theResponse; }
void ChatClient::incomingMessage() { QtJsonDb::QJsonDbWatcher *watcher = qobject_cast<QtJsonDb::QJsonDbWatcher*>(sender()); if (!watcher) return; QList<QtJsonDb::QJsonDbNotification> messages = watcher->takeNotifications(); QStringList results; foreach (const QtJsonDb::QJsonDbNotification &message, messages) results << messageToString(message.object()); fprintf(stderr, "\n%s\njsondb-chat> ", qPrintable(results.join("\n"))); }
void ChatClient::listHistory() { QtJsonDb::QJsonDbReadRequest *request = qobject_cast<QtJsonDb::QJsonDbReadRequest*>(sender()); if (!request) return; QList<QJsonObject> history = request->takeResults(); request->deleteLater(); if (history.isEmpty()) { fprintf(stderr, "No conversation history\n"); return; } QStringList results; foreach (const QJsonObject &message, history) results << messageToString(message); fprintf(stderr, "%s\n", qPrintable(results.join("\n"))); }