diff --git a/crm/crmd/atest.c b/crm/crmd/atest.c index daead582ac..b3a5b47903 100644 --- a/crm/crmd/atest.c +++ b/crm/crmd/atest.c @@ -1,185 +1,203 @@ -/* $Id: atest.c,v 1.7 2006/02/02 09:03:27 andrew Exp $ */ +/* $Id: atest.c,v 1.8 2006/04/03 10:03:06 andrew Exp $ */ /* * Copyright (C) 2004 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include const char* crm_system_name = "core"; gboolean process_atest_message( HA_Message *msg, crm_data_t *xml_data, IPC_Channel *sender); GMainLoop* mainloop = NULL; #define OPTARGS "V?f:" - +#include int main(int argc, char ** argv) { #if 0 - IPC_Channel* channels[2]; - crm_data_t *a_cib = NULL; - HA_Message *cmd = NULL; + + int start = 0, length = 0, read_len = 0; + char *buffer = NULL; + int rc = 0; + FILE *file_strm = NULL; + const char *my_msg = "my first message"; + char *compressed = NULL; + size_t compressed_len = 100; + + char *decomp = NULL; + size_t decomp_len = 1; + size_t alloced_decomp_len = decomp_len; - crm_log_init(crm_system_name); - set_crm_log_level(LOG_DEBUG_5-1); + cl_log_enable_stderr(1); - a_cib = string2xml(""); - /* createEmptyCib(); */ - cl_log_message(LOG_DEBUG, (HA_Message*)a_cib); + crm_malloc0(compressed, 100); + + cl_compress(compressed, &compressed_len, my_msg, strlen(my_msg)); + crm_err("Original: len: %zd, str: %s", + strlen(my_msg), my_msg); + + crm_err("Compressed: len: %zd, str: %s", + compressed_len, compressed); - if (ipc_channel_pair(channels) != IPC_OK) { - cl_perror("Can't create ipc channel pair"); - exit(1); + file_strm = fopen("foo.bz2", "w"); + rc = fprintf(file_strm, "%s", compressed); + if(rc < 0) { + cl_perror("Cannot write output to foo.bz2"); } - G_main_add_IPC_Channel(G_PRIORITY_LOW, - channels[1], FALSE, - subsystem_msg_dispatch, - (void*)process_atest_message, - default_ipc_connection_destroy); - - /* send transition graph over IPC instead */ - cmd = create_request(CRM_OP_TRANSITION, a_cib, NULL, - CRM_SYSTEM_TENGINE, CRM_SYSTEM_TENGINE, NULL); + fflush(file_strm); + fclose(file_strm); - send_ipc_message(channels[0], cmd); - - mainloop = g_main_new(FALSE); - crm_debug("Starting mainloop"); - g_main_run(mainloop); + file_strm = fopen("foo.bz2", "r"); + start = ftell(file_strm); + fseek(file_strm, 0L, SEEK_END); + length = ftell(file_strm); + fseek(file_strm, 0L, start); -#else - int flag = 0; - FILE *xml_strm = NULL; - const char *xml_file = "default.xml"; - crm_data_t *input = NULL; - crm_data_t *output = NULL; - int start = 0, length = 0, time_diff_ms = 0; - longclock_t time_start = 0; - longclock_t time_stop = 0; - longclock_t time_diff = 0; - - crm_log_init(crm_system_name); - set_crm_log_level(LOG_DEBUG); - - while (1) { - flag = getopt(argc, argv, OPTARGS); - if (flag == -1) - break; - - switch(flag) { - case 'f': - xml_file = optarg; - break; - case 'V': - cl_log_enable_stderr(TRUE); - alter_debug(DEBUG_INC); - break; - default: - printf("Argument code 0%o (%c)" - " is not (?yet?) supported\n", - flag, flag); - break; + CRM_ASSERT(start == ftell(file_strm)); + + crm_err("Reading %d bytes from file", length); + crm_malloc0(buffer, sizeof(char) * (length+1)); + read_len = fread(buffer, sizeof(char), length, file_strm); + if(read_len != length) { + crm_err("Calculated and read bytes differ: %d vs. %d", + length, read_len); + crm_free(buffer); + buffer = NULL; + + } else if(length <= 0) { + crm_info("foo.bz2 was not valid"); + crm_free(buffer); + buffer = NULL; + } + + while(alloced_decomp_len <= decomp_len) { + crm_err("Trying with buffer size: %zd", alloced_decomp_len); + if(decomp != NULL) { + crm_err("Found: %s", decomp); + crm_free(decomp); } + + decomp_len = 2*decomp_len; + crm_malloc0(decomp, (decomp_len+1)*sizeof(char)); + alloced_decomp_len = decomp_len; + + cl_decompress(decomp, &decomp_len, buffer, length); +/* cl_decompress(decomp, &decomp_len, compressed, compressed_len); */ } - xml_strm = fopen(xml_file, "r"); - - start = ftell(xml_strm); - fseek(xml_strm, 0L, SEEK_END); - length = ftell(xml_strm); - fseek(xml_strm, 0L, start); + crm_err("Decompressed: len: %zd, str: %s", decomp_len, decomp); + + return 0; +#else + int rc = 0; + FILE *file_strm = NULL; + BZFILE *my_file = NULL; + char *my_msg = strdup("my first message"); + unsigned int in = 0, out = 0; + char *decomp = NULL; + size_t decomp_len = 1; + size_t alloced_decomp_len = decomp_len; - if(xml_strm != NULL) { - crm_debug("Reading: %s", xml_file); - input = file2xml(xml_strm); - } else { - cl_perror("File not found: %s", xml_file); + cl_log_enable_stderr(1); + file_strm = fopen("foo.bz2", "w"); + my_file = BZ2_bzWriteOpen(&rc, file_strm, 1, 0, 0); + if(rc != BZ_OK) { + crm_err("Failed: open (%d)", rc); + return 0; } - - time_start = time_longclock(); - output = copy_xml(input); - time_stop = time_longclock(); + BZ2_bzWrite(&rc, my_file, my_msg, strlen(my_msg)); + if(rc != BZ_OK) { + crm_err("Failed: write (%d)", rc); + return 0; + } + BZ2_bzWriteClose(&rc, my_file, 0, &in, &out); + crm_err("In: %d, out: %d", in, out); + fclose(file_strm); + my_file = NULL; + file_strm = NULL; - time_diff = sub_longclock(time_stop, time_start); - time_diff_ms = longclockto_ms(time_diff); - crm_warn("Copy %s (%d bytes): %dms", xml_file, length, time_diff_ms); - - time_start = time_longclock(); - free_xml(output); - time_stop = time_longclock(); - - time_diff = sub_longclock(time_stop, time_start); - time_diff_ms = longclockto_ms(time_diff); - crm_warn("Free %s (%d bytes): %dms", xml_file, length, time_diff_ms); - - time_start = time_longclock(); - write_xml_file(input, "/tmp/foo.xml"); - time_stop = time_longclock(); + while(alloced_decomp_len <= decomp_len) { + crm_err("Trying with buffer size: %zd", alloced_decomp_len); + if(decomp != NULL) { + crm_err("Found: %s", decomp); + crm_free(decomp); + } + if(my_file != NULL) { + crm_err("Closing..."); + BZ2_bzReadClose(&rc, my_file); + fclose(file_strm); + } + file_strm = fopen("foo.bz2", "r"); + my_file = BZ2_bzReadOpen(&rc, file_strm, 0, 0, NULL, 0); + if(rc != BZ_OK) { + crm_err("Failed: read open (%d)", rc); + } - time_diff = sub_longclock(time_stop, time_start); - time_diff_ms = longclockto_ms(time_diff); - crm_warn("Write %s (%d bytes): %dms", xml_file, length, time_diff_ms); + decomp_len = 2*decomp_len; + crm_malloc0(decomp, (decomp_len+1)*sizeof(char)); + alloced_decomp_len = decomp_len; + + decomp_len = BZ2_bzRead(&rc, my_file, decomp, decomp_len); + if(rc != BZ_OK) { + crm_err("Failed: read (%d)", rc); + } + } + crm_err("Decompressed: len: %zd, str: %s", decomp_len, decomp); + return 0; #endif - return 0; } -gboolean -process_atest_message(HA_Message *msg, crm_data_t *xml_data, IPC_Channel *sender) -{ - crm_debug("made it"); - cl_log_message(LOG_DEBUG, msg); - return TRUE; -}