Skip to content
Permalink
Browse files

Add ioWriteStr() and ioWriteStrLine().

These function names make it clearer what is being written.

The old ioWriteLine() has been repurposed to write buffers.
  • Loading branch information...
dwsteele committed Apr 22, 2019
1 parent 81f6521 commit c650134a0414c154eb21609b363aeceafbfdda60
@@ -25,6 +25,10 @@
<p>Only process next filter in <code>IoFilterGroup</code> when input buffer is full or flushing.</p>
</release-item>

<release-item>
<p>Add <code>ioWriteStr()</code> and <code>ioWriteStrLine()</code>.</p>
</release-item>

<release-item>
<p>Add separate functions to encode/decode each JSON type.</p>
</release-item>
@@ -248,7 +248,7 @@ httpClientRequest(
// Write the request
String *queryStr = httpQueryRender(query);

ioWriteLine(
ioWriteStrLine(
tlsClientIoWrite(this->tls),
strNewFmt(
"%s %s%s%s " HTTP_VERSION "\r", strPtr(verb), strPtr(httpUriEncode(uri, true)), queryStr == NULL ? "" : "?",
@@ -262,14 +262,14 @@ httpClientRequest(
for (unsigned int headerIdx = 0; headerIdx < strLstSize(headerList); headerIdx++)
{
const String *headerKey = strLstGet(headerList, headerIdx);
ioWriteLine(
ioWriteStrLine(
tlsClientIoWrite(this->tls),
strNewFmt("%s:%s\r", strPtr(headerKey), strPtr(httpHeaderGet(requestHeader, headerKey))));
}
}

// Write out blank line to end the headers
ioWriteLine(tlsClientIoWrite(this->tls), CR_STR);
ioWriteLine(tlsClientIoWrite(this->tls), CR_BUF);

// Write out body if any
if (body != NULL)
@@ -123,7 +123,46 @@ ioWrite(IoWrite *this, const Buffer *buffer)
Write linefeed-terminated string
***********************************************************************************************************************************/
void
ioWriteLine(IoWrite *this, const String *string)
ioWriteLine(IoWrite *this, const Buffer *buffer)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(IO_WRITE, this);
FUNCTION_LOG_PARAM(BUFFER, buffer);
FUNCTION_LOG_END();

ASSERT(this != NULL);
ASSERT(buffer != NULL);

ioWrite(this, buffer);
ioWrite(this, LF_BUF);

FUNCTION_LOG_RETURN_VOID();
}

/***********************************************************************************************************************************
Write string
***********************************************************************************************************************************/
void
ioWriteStr(IoWrite *this, const String *string)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(IO_WRITE, this);
FUNCTION_LOG_PARAM(STRING, string);
FUNCTION_LOG_END();

ASSERT(this != NULL);
ASSERT(string != NULL);

ioWrite(this, BUFSTR(string));

FUNCTION_LOG_RETURN_VOID();
}

/***********************************************************************************************************************************
Write linefeed-terminated string
***********************************************************************************************************************************/
void
ioWriteStrLine(IoWrite *this, const String *string)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(IO_WRITE, this);
@@ -132,7 +171,6 @@ ioWriteLine(IoWrite *this, const String *string)

ASSERT(this != NULL);
ASSERT(string != NULL);
ASSERT(this->opened && !this->closed);

ioWrite(this, BUFSTR(string));
ioWrite(this, LF_BUF);
@@ -21,7 +21,9 @@ Functions
***********************************************************************************************************************************/
void ioWriteOpen(IoWrite *this);
void ioWrite(IoWrite *this, const Buffer *buffer);
void ioWriteLine(IoWrite *this, const String *string);
void ioWriteLine(IoWrite *this, const Buffer *buffer);
void ioWriteStr(IoWrite *this, const String *string);
void ioWriteStrLine(IoWrite *this, const String *string);
void ioWriteFlush(IoWrite *this);
void ioWriteClose(IoWrite *this);

@@ -12,6 +12,7 @@ Constant buffers that are generally useful
***********************************************************************************************************************************/
BUFFER_STRDEF_EXTERN(BRACKETL_BUF, "[");
BUFFER_STRDEF_EXTERN(BRACKETR_BUF, "]");
BUFFER_STRDEF_EXTERN(CR_BUF, "\r");
BUFFER_STRDEF_EXTERN(EQ_BUF, "=");
BUFFER_STRDEF_EXTERN(LF_BUF, "\n");

@@ -101,6 +101,7 @@ Constant buffers that are generally useful
***********************************************************************************************************************************/
BUFFER_DECLARE(BRACKETL_BUF);
BUFFER_DECLARE(BRACKETR_BUF);
BUFFER_DECLARE(CR_BUF);
BUFFER_DECLARE(EQ_BUF);
BUFFER_DECLARE(LF_BUF);

@@ -186,7 +186,7 @@ protocolClientWriteCommand(ProtocolClient *this, const ProtocolCommand *command)
ASSERT(command != NULL);

// Write out the command
ioWriteLine(this->write, protocolCommandJson(command));
ioWriteStrLine(this->write, protocolCommandJson(command));
ioWriteFlush(this->write);

// Reset the keep alive time
@@ -65,7 +65,7 @@ protocolServerNew(const String *name, const String *service, IoRead *read, IoWri
kvPut(greetingKv, VARSTR(PROTOCOL_GREETING_SERVICE_STR), VARSTR(service));
kvPut(greetingKv, VARSTR(PROTOCOL_GREETING_VERSION_STR), VARSTRZ(PROJECT_VERSION));

ioWriteLine(this->write, jsonFromKv(greetingKv, 0));
ioWriteStrLine(this->write, jsonFromKv(greetingKv, 0));
ioWriteFlush(this->write);
}
MEM_CONTEXT_TEMP_END();
@@ -114,7 +114,7 @@ protocolServerError(ProtocolServer *this, int code, const String *message, const
kvPut(error, VARSTR(PROTOCOL_OUTPUT_STR), VARSTR(message));
kvPut(error, VARSTR(PROTOCOL_ERROR_STACK_STR), VARSTR(stack));

ioWriteLine(this->write, jsonFromKv(error, 0));
ioWriteStrLine(this->write, jsonFromKv(error, 0));
ioWriteFlush(this->write);

FUNCTION_LOG_RETURN_VOID();
@@ -200,7 +200,7 @@ protocolServerResponse(ProtocolServer *this, const Variant *output)
if (output != NULL)
kvAdd(result, VARSTR(PROTOCOL_OUTPUT_STR), output);

ioWriteLine(this->write, jsonFromKv(result, 0));
ioWriteStrLine(this->write, jsonFromKv(result, 0));
ioWriteFlush(this->write);

FUNCTION_LOG_RETURN_VOID();
@@ -135,7 +135,7 @@ storageDriverRemoteFileWrite(StorageDriverRemoteFileWrite *this, const Buffer *b
ASSERT(this != NULL);
ASSERT(buffer != NULL);

ioWriteLine(protocolClientIoWrite(this->client), strNewFmt(PROTOCOL_BLOCK_HEADER "%zu", bufUsed(buffer)));
ioWriteStrLine(protocolClientIoWrite(this->client), strNewFmt(PROTOCOL_BLOCK_HEADER "%zu", bufUsed(buffer)));
ioWrite(protocolClientIoWrite(this->client), buffer);
ioWriteFlush(protocolClientIoWrite(this->client));

@@ -157,7 +157,7 @@ storageDriverRemoteFileWriteClose(StorageDriverRemoteFileWrite *this)
// Close if the file has not already been closed
if (this->client != NULL)
{
ioWriteLine(protocolClientIoWrite(this->client), STRDEF(PROTOCOL_BLOCK_HEADER "0"));
ioWriteLine(protocolClientIoWrite(this->client), BUFSTRDEF(PROTOCOL_BLOCK_HEADER "0"));
ioWriteFlush(protocolClientIoWrite(this->client));
protocolClientReadOutput(this->client, false);

@@ -319,7 +319,7 @@ storageDriverRemoteFileWriteFree(StorageDriverRemoteFileWrite *this)
// If freed without closing then notify the remote to close the file
if (this->client != NULL)
{
ioWriteLine(protocolClientIoWrite(this->client), STRDEF(PROTOCOL_BLOCK_HEADER "-1"));
ioWriteLine(protocolClientIoWrite(this->client), BUFSTRDEF(PROTOCOL_BLOCK_HEADER "-1"));
ioWriteFlush(protocolClientIoWrite(this->client));
protocolClientReadOutput(this->client, false);

@@ -94,7 +94,7 @@ storageDriverRemoteProtocol(const String *command, const VariantList *paramList,

if (bufUsed(buffer) > 0)
{
ioWriteLine(protocolServerIoWrite(server), strNewFmt(PROTOCOL_BLOCK_HEADER "%zu", bufUsed(buffer)));
ioWriteStrLine(protocolServerIoWrite(server), strNewFmt(PROTOCOL_BLOCK_HEADER "%zu", bufUsed(buffer)));
ioWrite(protocolServerIoWrite(server), buffer);
ioWriteFlush(protocolServerIoWrite(server));

@@ -104,7 +104,7 @@ storageDriverRemoteProtocol(const String *command, const VariantList *paramList,
while (!ioReadEof(fileRead));

// Write a zero block to show file is complete
ioWriteLine(protocolServerIoWrite(server), STRDEF(PROTOCOL_BLOCK_HEADER "0"));
ioWriteLine(protocolServerIoWrite(server), BUFSTRDEF(PROTOCOL_BLOCK_HEADER "0"));
ioWriteFlush(protocolServerIoWrite(server));
}
}
@@ -529,7 +529,7 @@ testRun(void)
lockAcquire(cfgOptionStr(cfgOptLockPath), cfgOptionStr(cfgOptStanza), cfgLockType(), 30000, true);

// Let the parent know the lock has been acquired and wait for the parent to allow lock release
ioWriteLine(write, strNew(""));
ioWriteStrLine(write, strNew(""));
ioWriteFlush(write);
ioReadLine(read);

@@ -552,7 +552,7 @@ testRun(void)
"unable to push WAL file '000000010000000100000001' to the archive asynchronously after 1 second(s)");

// Notify the child to release the lock
ioWriteLine(write, strNew(""));
ioWriteLine(write, bufNew(0));
ioWriteFlush(write);
}
HARNESS_FORK_PARENT_END();
@@ -18,7 +18,7 @@ testRun(void)

TEST_ASSIGN(exec, execNew(strNew("catt"), NULL, strNew("cat"), 1000), "invalid exec");
TEST_RESULT_VOID(execOpen(exec), "open invalid exec");
TEST_RESULT_VOID(ioWriteLine(execIoWrite(exec), EMPTY_STR), "write invalid exec");
TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), EMPTY_STR), "write invalid exec");
sleep(1);
TEST_ERROR(
ioWriteFlush(execIoWrite(exec)), ExecuteError,
@@ -32,7 +32,7 @@ testRun(void)
TEST_RESULT_VOID(execOpen(exec), "open cat exec");

String *message = strNew("ACKBYACK");
TEST_RESULT_VOID(ioWriteLine(execIoWrite(exec), message), "write cat exec");
TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), message), "write cat exec");
ioWriteFlush(execIoWrite(exec));
TEST_RESULT_STR(strPtr(ioReadLine(execIoRead(exec))), strPtr(message), "read cat exec");
TEST_RESULT_VOID(execFree(exec), "free exec");
@@ -57,7 +57,7 @@ testRun(void)
TEST_ASSIGN(exec, execNew(strNew("cat"), strLstAddZ(strLstNew(), "-b"), strNew("cat"), 1000), "new cat exec");
TEST_RESULT_VOID(execOpen(exec), "open cat exec");

TEST_RESULT_VOID(ioWriteLine(execIoWrite(exec), message), "write cat exec");
TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), message), "write cat exec");
ioWriteFlush(execIoWrite(exec));
TEST_RESULT_STR(strPtr(ioReadLine(execIoRead(exec))), " 1\tACKBYACK", "read cat exec");
TEST_RESULT_VOID(execFree(exec), "free exec");
@@ -76,7 +76,7 @@ testRun(void)
TEST_ASSIGN(exec, execNew(strNew("cat"), strLstAddZ(strLstNew(), "-b"), strNew("cat"), 1000), "new cat exec");
TEST_RESULT_VOID(execOpen(exec), "open cat exec");

TEST_RESULT_VOID(ioWriteLine(execIoWrite(exec), message), "write cat exec");
TEST_RESULT_VOID(ioWriteStrLine(execIoWrite(exec), message), "write cat exec");
ioWriteFlush(execIoWrite(exec));
TEST_RESULT_STR(strPtr(ioReadLine(execIoRead(exec))), " 1\tACKBYACK", "read cat exec");
TEST_RESULT_VOID(execFree(exec), "free exec");
@@ -92,7 +92,7 @@ testRun(void)
TEST_ERROR(execFree(exec), ExecuteError, "sleep did not exit when expected");

TEST_ERROR(ioReadLine(execIoRead(exec)), FileReadError, "unable to select from sleep read: [9] Bad file descriptor");
ioWriteLine(execIoWrite(exec), strNew(""));
ioWriteStrLine(execIoWrite(exec), strNew(""));
TEST_ERROR(ioWriteFlush(execIoWrite(exec)), FileWriteError, "unable to write to sleep write: [9] Bad file descriptor");

sleepMSec(500);
@@ -420,7 +420,7 @@ testRun(void)

TEST_RESULT_VOID(ioWriteOpen(write), " open io object");
TEST_RESULT_BOOL(testIoWriteOpenCalled, true, " check io object open");
TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("ABC")), " write 3 bytes");
TEST_RESULT_VOID(ioWriteStr(write, STRDEF("ABC")), " write 3 bytes");
TEST_RESULT_VOID(ioWriteClose(write), " close io object");
TEST_RESULT_BOOL(testIoWriteCloseCalled, true, " check io object closed");

@@ -453,14 +453,14 @@ testRun(void)
TEST_RESULT_VOID(ioWriteFilterGroupSet(ioBufferWriteIo(bufferWrite), filterGroup), " add filter group to write io");

TEST_RESULT_VOID(ioWriteOpen(ioBufferWriteIo(bufferWrite)), " open buffer write object");
TEST_RESULT_VOID(ioWriteLine(ioBufferWriteIo(bufferWrite), strNew("AB")), " write string");
TEST_RESULT_VOID(ioWriteLine(ioBufferWriteIo(bufferWrite), BUFSTRDEF("AB")), " write line");
TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), bufNew(0)), " write 0 bytes");
TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), NULL), " write 0 bytes");
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\n", " check write");

TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), BUFSTRDEF("Z")), " write string");
TEST_RESULT_VOID(ioWriteStr(ioBufferWriteIo(bufferWrite), STRDEF("Z")), " write string");
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\n", " no change because output buffer is not full");
TEST_RESULT_VOID(ioWrite(ioBufferWriteIo(bufferWrite), BUFSTRDEF("12345")), " write bytes");
TEST_RESULT_VOID(ioWriteStr(ioBufferWriteIo(bufferWrite), STRDEF("12345")), " write bytes");
TEST_RESULT_STR(strPtr(strNewBuf(buffer)), "AABB\n\nZZ1122334455", " check write");

TEST_RESULT_VOID(ioWriteClose(ioBufferWriteIo(bufferWrite)), " close buffer write object");
@@ -499,7 +499,7 @@ testRun(void)
ioWriteOpen(ioHandleWriteIo(write));

// Write a line to be read
TEST_RESULT_VOID(ioWriteLine(ioHandleWriteIo(write), strNew("test string 1")), "write test string");
TEST_RESULT_VOID(ioWriteStrLine(ioHandleWriteIo(write), strNew("test string 1")), "write test string");
ioWriteFlush(ioHandleWriteIo(write));
ioWriteFlush(ioHandleWriteIo(write));

Oops, something went wrong.

0 comments on commit c650134

Please sign in to comment.
You can’t perform that action at this time.