static gboolean afstomp_dd_connect(STOMPDestDriver *self, gboolean reconnect) { stomp_frame frame; if (reconnect && self->conn) return TRUE; if (!afstomp_try_connect(self)) return FALSE; afstomp_create_connect_frame(self, &frame); if (!afstomp_send_frame(self, &frame)) { msg_error("Sending CONNECT frame to STOMP server failed!", NULL); return FALSE; } stomp_receive_frame(self->conn, &frame); if (strcmp(frame.command, "CONNECTED")) { msg_debug("Error connecting to STOMP server, stomp server did not accept CONNECT request", NULL); stomp_frame_deinit(&frame); return FALSE; } msg_debug("Connecting to STOMP succeeded", evt_tag_str("driver", self->super.super.super.id), NULL); stomp_frame_deinit(&frame); return TRUE; }
static int stomp_check_for_frame(stomp_connection *connection) { struct pollfd pfd; pfd.fd = connection->socket; pfd.events = POLLIN | POLLPRI; poll(&pfd, 1, 0); if (pfd.revents & ( POLLIN | POLLPRI)) { stomp_frame frame; if (!stomp_receive_frame(connection, &frame)) return FALSE; if (!strcmp(frame.command, "ERROR")) { msg_error("ERROR frame received from stomp_server", NULL); stomp_frame_deinit(&frame); return FALSE; } /* According to stomp protocol, here only ERROR or RECEIPT frames can come, so we missed a RECEIPT frame here, our bad. */ stomp_frame_deinit(&frame); return TRUE; } return TRUE; }
Test(stomp_proto, test_only_command) { stomp_frame frame; stomp_parse_frame(g_string_new("CONNECTED\n\n"), &frame); assert_stomp_command(&frame, "CONNECTED"); stomp_frame_deinit(&frame); }
Test(stomp_proto, test_command_and_header) { stomp_frame frame; stomp_parse_frame(g_string_new("CONNECTED\nsession:ID:tusa-38077-1378214843533-2:1\n"), &frame); assert_stomp_command(&frame, "CONNECTED"); assert_stomp_header(&frame, "session", "ID:tusa-38077-1378214843533-2:1"); stomp_frame_deinit(&frame); };
Test(stomp_proto, test_command_and_data) { stomp_frame frame; stomp_parse_frame(g_string_new("CONNECTED\n\nalmafa"), &frame); assert_stomp_command(&frame, "CONNECTED"); assert_stomp_body(&frame, "almafa"); stomp_frame_deinit(&frame); };
Test(stomp_proto, test_command_and_header_and_data) { stomp_frame frame; stomp_parse_frame(g_string_new("CONNECTED\nheader_name:header_value\n\nbelafa"), &frame); assert_stomp_command(&frame, "CONNECTED"); assert_stomp_header(&frame, "header_name", "header_value"); assert_stomp_body(&frame, "belafa"); stomp_frame_deinit(&frame); };
int stomp_write(stomp_connection *connection, stomp_frame *frame) { GString *data; if (!stomp_check_for_frame(connection)) return FALSE; data = create_gstring_from_frame(frame); if (!write_gstring_to_socket(connection->socket, data)) { msg_error("Write error, partial write", NULL); stomp_frame_deinit(frame); g_string_free(data, TRUE); return FALSE; } g_string_free(data, TRUE); stomp_frame_deinit(frame); return TRUE; }
Test(stomp_proto, test_generate_gstring_from_frame) { stomp_frame frame; GString *actual; stomp_frame_init(&frame, "SEND", sizeof("SEND")); stomp_frame_add_header(&frame, "header_name", "header_value"); stomp_frame_set_body(&frame, "body", sizeof("body")); actual = create_gstring_from_frame(&frame); cr_assert_str_eq(actual->str, "SEND\nheader_name:header_value\n\nbody", "Generated stomp frame does not match"); stomp_frame_deinit(&frame); };