static char * receive_message(ZSocket *socket, char *sender) { char *buffer; ZStream *stream = z_stream_new(CTX_FROM_OBJECT(socket), ALLOCATOR_FROM_OBJECT(socket)); z_socket_connect_stream(socket, stream); ZString *text = z_stream_read_text(stream); z_object_unref(Z_OBJECT(stream)); if (!text || z_string_get_length(text) == 0) { buffer = malloc(1); buffer[0] = 0; if (text) z_object_unref(Z_OBJECT(text)); return buffer; } buffer = z_string_get_cstring(text, Z_STRING_ENCODING_UTF8); trace("== %s received '%s' (len=%d)\n", sender, buffer, z_string_get_length(text)); z_object_unref(Z_OBJECT(text)); return buffer; }
static void on_new_client1(ZBind *task, int *stage, ZSocket *socket, ZServer *server) { trace("Server: New client connected\n"); assert(*stage == 1); ++(*stage); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) server_message_handler1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_readable(socket, task); z_object_unref(Z_OBJECT(task)); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) server_close_connection1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_disconnect(socket, task); z_object_unref(Z_OBJECT(task)); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) server_hangup_connection1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_hangup(socket, task); z_object_unref(Z_OBJECT(task)); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) server_error_connection1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_error(socket, task); z_object_unref(Z_OBJECT(task)); z_object_unref(Z_OBJECT(server)); }
static void on_client_ready1(ZBind *task, ZSocket *socket, int *stage) { trace("Client: Connection established\n"); assert(*stage == 2); ++(*stage); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) client_close_connection1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_disconnect(socket, task); z_object_unref(Z_OBJECT(task)); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) client_hangup_connection1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_hangup(socket, task); z_object_unref(Z_OBJECT(task)); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) client_error_connection1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_error(socket, task); z_object_unref(Z_OBJECT(task)); task = z_bind_util_create_c_bind(Z_OBJECT(socket), (ZBindHandler) client_message_handler1); z_bind_append_ptr(task, socket); z_bind_append_ptr(task, stage); z_socket_set_on_readable(socket, task); z_object_unref(Z_OBJECT(task)); send_message(socket, "Client", "Hi"); }
static void case5(struct zco_context_t *context) { /* Testing z_set_insert */ ZSet *myset; ZSetIter *it, *end; ZString *key; char *test_result[4] = { "a", "b", "c", "z" }; int i; trace("======================\n"); trace("ZSet case #5:\n"); myset = z_set_new(context, NULL); z_set_set_key_destruct(myset, (ZSetItemCallback) z_object_unref); z_set_set_compare(myset, set_compare); key = z_string_new(context, NULL); z_string_set_cstring(key, "a", Z_STRING_ENCODING_UTF8); assert(z_set_insert(myset, key) == 0); key = z_string_new(context, NULL); z_string_set_cstring(key, "z", Z_STRING_ENCODING_UTF8); assert(z_set_insert(myset, key) == 0); key = z_string_new(context, NULL); z_string_set_cstring(key, "z", Z_STRING_ENCODING_UTF8); assert(z_set_insert(myset, key) == -1); z_object_unref(Z_OBJECT(key)); key = z_string_new(context, NULL); z_string_set_cstring(key, "b", Z_STRING_ENCODING_UTF8); assert(z_set_insert(myset, key) == 0); key = z_string_new(context, NULL); z_string_set_cstring(key, "c", Z_STRING_ENCODING_UTF8); assert(z_set_insert(myset, key) == 0); it = z_set_get_begin(myset); end = z_set_get_end(myset); trace("myset contains:"); for (i = 0; !z_set_iter_is_equal(it, end); z_set_iter_increment(it), ++i) { ZString *x = z_set_get_key(myset, it); char *key_cstr = z_string_get_cstring(x, Z_STRING_ENCODING_UTF8); trace(" %s", key_cstr); assert(strcmp(test_result[i], key_cstr) == 0); free(key_cstr); } trace("\n\n"); z_object_unref(Z_OBJECT(myset)); z_object_unref(Z_OBJECT(it)); z_object_unref(Z_OBJECT(end)); }
static void case1(struct zco_context_t *context) { /* Testing z_set_get_begin / z_set_get_end */ ZSet *myset; ZSetIter *it, *end; ZString *key; char *test_result[3] = { "a", "b", "c" }; int i; trace("======================\n"); trace("ZSet case #1:\n"); /* Here is a sequence of steps: 1. set object is destroyed 2. at some later point, the vector inside the set is disposed 3. the set_item_destruct method is called during step #2 4. the set object is already destroyed in step #1 so 'self' is undefined */ myset = z_set_new(context, NULL); z_set_set_key_destruct(myset, (ZSetItemCallback) z_object_unref); z_set_set_compare(myset, set_compare); key = z_string_new(context, NULL); z_string_set_cstring(key, "b", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); key = z_string_new(context, NULL); z_string_set_cstring(key, "a", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); key = z_string_new(context, NULL); z_string_set_cstring(key, "c", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); it = z_set_get_begin(myset); end = z_set_get_end(myset); trace("myset contains:"); for (i = 0; !z_set_iter_is_equal(it, end); z_set_iter_increment(it), ++i) { ZString *key = z_set_get_key(myset, it); char *key_cstr = z_string_get_cstring(key, Z_STRING_ENCODING_UTF8); trace(" %s", key_cstr); assert(strcmp(test_result[i], key_cstr) == 0); free(key_cstr); } trace("\n\n"); z_object_unref(Z_OBJECT(end)); z_object_unref(Z_OBJECT(it)); z_object_unref(Z_OBJECT(myset)); }
void zco_context_init(struct zco_context_t *ctx) { ctx->type_count = 0; ctx->types = 0; ctx->fixed_allocator = NULL; ctx->flex_allocator = NULL; /* Set the default minimum segment capacity to be 3 times the overhead incurred by each segment. This means that there can be at most 40% wasted space that is not used by the actual data */ zco_context_set_min_segment_capacity_by_size(ctx, 3 * sizeof(ZVectorSegment)); /* Set the default minimum segment capacity to hold 2 items. If each segment holds a single item, it is nothing more than a linked list. */ zco_context_set_min_segment_capacity_by_count(ctx, 2); /* Create the allocators */ #if !defined(BOOTSTRAP) && defined(USE_CUSTOM_ALLOCATORS) ZSimpleMemoryAllocator *fixed_allocator = z_simple_memory_allocator_new(ctx, NULL); ZBuddyMemoryAllocator *flex_allocator = z_buddy_memory_allocator_new(ctx, NULL); ZThreadSimpleMemoryAllocator *ts_fixed_allocator = z_thread_simple_memory_allocator_new(ctx, NULL); #else ZSysMemoryAllocator *fixed_allocator = z_sys_memory_allocator_new(ctx, NULL); ZSysMemoryAllocator *flex_allocator = z_sys_memory_allocator_new(ctx, NULL); ZSysMemoryAllocator *ts_fixed_allocator = z_sys_memory_allocator_new(ctx, NULL); #endif ctx->fixed_allocator = (ZMemoryAllocator *) fixed_allocator; ctx->flex_allocator = (ZMemoryAllocator *) flex_allocator; ctx->ts_fixed_allocator = (ZMemoryAllocator *) ts_fixed_allocator; /* Keep note of the last type id for which the object buffer was not allocated using the allocators. */ if (last_allocator_id == -1) last_allocator_id = next_id; #if !defined(BOOTSTRAP) && defined(USE_GARBAGE_COLLECTION) /* Create an object tracker for each allocator */ ZDefaultObjectTracker *fixed_tracker = z_default_object_tracker_new(ctx, NULL); ZDefaultObjectTracker *flex_tracker = z_default_object_tracker_new(ctx, NULL); ZDefaultObjectTracker *ts_fixed_tracker = z_default_object_tracker_new(ctx, NULL); /* Assign the object tracker to the allocator */ z_memory_allocator_set_object_tracker((ZMemoryAllocator *) fixed_allocator, (ZObjectTracker *) fixed_tracker); z_memory_allocator_set_object_tracker((ZMemoryAllocator *) flex_allocator, (ZObjectTracker *) flex_tracker); z_memory_allocator_set_object_tracker((ZMemoryAllocator *) ts_fixed_allocator, (ZObjectTracker *) ts_fixed_tracker); z_object_unref(Z_OBJECT(fixed_tracker)); z_object_unref(Z_OBJECT(flex_tracker)); z_object_unref(Z_OBJECT(ts_fixed_tracker)); #endif }
static void send_message(ZSocket *socket, char *sender, char *msg) { ZStream *stream = z_stream_new(CTX_FROM_OBJECT(socket), ALLOCATOR_FROM_OBJECT(socket)); z_socket_connect_stream(socket, stream); ZString *message = z_string_new(CTX_FROM_OBJECT(socket), ALLOCATOR_FROM_OBJECT(socket)); z_string_append_cstring(message, msg, Z_STRING_ENCODING_UTF8); z_stream_write_text(stream, message); z_object_unref(Z_OBJECT(message)); z_object_unref(Z_OBJECT(stream)); trace("%s says: %s\n", sender, msg); }
/** * anypy_set_content_hint: * @self: AnyPyProxy instance * @args: Python args argument * * sets verdict for the parent proxy * args is (verdict,description) **/ static ZPolicyObj * anypy_set_content_hint(AnyPyProxy * self, ZPolicyObj *args) { gint64 length; z_proxy_enter(self); if (!z_policy_var_parse_tuple(args, "L", &length)) { z_policy_raise_exception_obj(z_policy_exc_value_error, "Invalid arguments."); z_proxy_leave(self); return NULL; } if (self->super.parent_proxy) { ZProxyStackIface *iface; iface = z_proxy_find_iface(self->super.parent_proxy, Z_CLASS(ZProxyStackIface)); if (iface) { z_proxy_stack_iface_set_content_hint(iface, length); z_object_unref(&iface->super); } } z_proxy_return(self, z_policy_none_ref()); }
/** * anypy_set_verdict: * @self: AnyPyProxy instance * @args: Python args argument * * sets verdict for the parent proxy * args is (verdict,description) **/ static ZPolicyObj * anypy_set_verdict(AnyPyProxy * self, ZPolicyObj *args) { gint verdict; gchar *description; z_proxy_enter(self); if (!z_policy_var_parse_tuple(args, "is", &verdict, &description)) { z_policy_raise_exception_obj(z_policy_exc_value_error, "Invalid arguments."); z_proxy_return(self, NULL); } if (self->super.parent_proxy) { ZProxyStackIface *iface; iface = z_proxy_find_iface(self->super.parent_proxy, Z_CLASS(ZProxyStackIface)); if (iface) { z_proxy_stack_iface_set_verdict(iface, verdict, description); z_object_unref(&iface->super); } } z_proxy_return(self, z_policy_none_ref()); }
void z_object_register_signal(Self *self,char *name) { { struct zco_context_t *ctx = CTX_FROM_OBJECT(self); ZMemoryAllocator *allocator = ALLOCATOR_FROM_OBJECT(self); /* create signal map (if necessary) */ if (!selfp->signal_map) { ZMap *signal_map = z_map_new(ctx, allocator); z_map_set_compare(signal_map, map_compare); z_map_set_key_destruct(signal_map, (ZMapItemCallback) free); z_map_set_value_destruct(signal_map, (ZMapItemCallback) unref); selfp->signal_map = (ZObject *) signal_map; } /* locate signal's closure list by name */ ZMap *signal_map = (ZMap *) selfp->signal_map; ZMapIter *it = z_map_find(signal_map, name); if (!it) { ZVector *closure_list = z_vector_new(ctx, allocator); z_vector_set_item_size(closure_list, 0); z_vector_set_item_destruct(closure_list, (ZVectorItemCallback) unref); z_map_insert(signal_map, strdup(name), closure_list); } else { z_object_unref(Z_OBJECT(it)); } } }
static void case3(struct zco_context_t *context) { /* Testing z_set_get_size */ ZSet *myset; ZString *key; int size; trace("======================\n"); trace("ZSet case #3:\n"); myset = z_set_new(context, NULL); z_set_set_key_destruct(myset, (ZSetItemCallback) z_object_unref); z_set_set_compare(myset, set_compare); key = z_string_new(context, NULL); z_string_set_cstring(key, "a", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); key = z_string_new(context, NULL); z_string_set_cstring(key, "b", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); key = z_string_new(context, NULL); z_string_set_cstring(key, "c", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); size = z_set_get_size(myset); trace("myset size = %d\n", size); assert(size == 3); z_object_unref(Z_OBJECT(myset)); }
static void client_hangup_connection1(ZBind *task, ZSocket *socket, int *stage) { assert(*stage == 9); ++(*stage); trace("Client: Connection hangup!\n"); z_object_unref(Z_OBJECT(socket)); }
void zco_app_context_destroy(struct zco_context_t *ctx) { /* Release the event loop. */ ZEventLoop *event_loop = z_event_loop_get_instance_ptr(ctx); if (event_loop) z_object_unref(Z_OBJECT(event_loop)); zco_context_destroy(ctx); }
static void case1(struct zco_context_t *context) { ZTestDerivedObject *test_object = z_test_derived_object_new(context, NULL); ZCClosureMarshal *marshal; marshal = z_c_closure_marshal_get_instance(context); z_object_set_closure_marshal(Z_OBJECT(test_object), Z_OBJECT(marshal)); z_object_unref(Z_OBJECT(marshal)); void *key1 = z_object_connect(Z_OBJECT(test_object), "clicked", Z_OBJECT(test_object), "close1", NULL); void *key2 = z_object_connect(Z_OBJECT(test_object), "clicked", Z_OBJECT(test_object), "close2", NULL); z_test_derived_object_click_it(test_object, "CLICK IT!!"); z_object_disconnect(Z_OBJECT(test_object), "clicked", key2); z_object_disconnect(Z_OBJECT(test_object), "clicked", key1); z_object_unref(Z_OBJECT(test_object)); }
static void server_hangup_connection1(ZBind *task, ZSocket *socket, int *stage) { assert(*stage == 10); ++(*stage); trace("Server: Connection hangup!\n"); ZEventLoop *ev = zco_app_context_get_event_loop_ptr(CTX_FROM_OBJECT(task)); z_event_loop_quit(ev); z_object_unref(Z_OBJECT(socket)); }
void z_object_set_closure_marshal(Self *self, ZObject * value) { { if (selfp->closure_marshal) z_object_unref(selfp->closure_marshal); selfp->closure_marshal = value; if (selfp->closure_marshal) z_object_ref(selfp->closure_marshal); } }
static ZServer * run_server(struct zco_context_t *context, ZBindHandler on_new_client, int port, int *stage) { ZServer *server = z_server_new(context, NULL); ZBind *task; task = z_bind_util_create_c_bind(Z_OBJECT(server), on_new_client); z_bind_append_ptr(task, stage); z_server_set_on_new_client(server, task); z_object_unref(Z_OBJECT(task)); z_server_start(server, port); return server; }
void define_test(void(*callback)(ZRuntimeContext *), struct zco_context_t *ctx, const char *title) { trace("======================\n"); trace("%s:\n", title); /* redirect stdout to a pipe */ int the_pipe[2]; pipe(the_pipe); int original_stdout = write_stdout_to_pipe(the_pipe[1]); /* run test in native mode */ ZRuntimeContext *runtime_context = z_runtime_context_new(ctx, NULL); z_runtime_context_set_target(runtime_context, 0); callback(runtime_context); /* restore stdout as we don't need any more data that is sent to it */ restore_stdout(original_stdout); /* read data that is sent to the pipe and close the pipe */ ZString *out = read_from_fd(ctx, NULL, the_pipe[0]); close(the_pipe[0]); z_object_unref(Z_OBJECT(runtime_context)); /* run test in JS mode and verify its output */ runtime_context = z_runtime_context_new(ctx, NULL); z_runtime_context_set_target(runtime_context, 1); callback(runtime_context); verify_output(runtime_context, out); z_object_unref(Z_OBJECT(runtime_context)); /* print out the expected string */ char *print_out = z_string_get_cstring(out, Z_STRING_ENCODING_UTF8); fputs(print_out, stdout); free(print_out); z_object_unref(Z_OBJECT(out)); trace("\n"); }
void network_test(struct zco_context_t *context, int id) { int i, first, last; if (id == 0) { first = 1; last = 1; } else { first = id; last = id; } for (int i=first; i<=last; ++i) { /* Create a guest thread in which the client & server will run */ struct zco_context_t app_ctx; zco_app_context_init(&app_ctx); ZEventLoop *ev = zco_app_context_get_event_loop_ptr(&app_ctx); z_event_loop_set_name(ev, "Application"); z_event_loop_run(ev); /* Initialize the stage counter */ int *stage = malloc(sizeof(int)); *stage = 0; /* Call application_main() on the guest thread */ ZBind *task = z_bind_new(context, NULL); z_bind_set_handler(task, (ZBindHandler) application_main); z_bind_append_int32(task, id); z_bind_append_ptr(task, stage); z_event_loop_post_task(ev, task, NULL, 0, 0); z_object_unref(Z_OBJECT(task)); /* Wait for event loop to quit */ z_event_loop_join(ev); /* Verify that 'stage' is the correct value */ assert(*stage == 11); free(stage); zco_app_context_destroy(&app_ctx); } }
static ZSocket * run_client(struct zco_context_t *context, ZBindHandler on_ready, int port, int *stage) { ZSocket *client = z_socket_new(context, NULL); assert(*stage == 0); ++(*stage); z_socket_declare_family(client, AF_INET6); z_socket_set_ipv6_address(client, in6addr_any); z_socket_set_flowinfo(client, 0); ZBind *task; task = z_bind_util_create_c_bind(Z_OBJECT(client), on_ready); z_bind_append_ptr(task, client); z_bind_append_ptr(task, stage); z_socket_set_on_ready(client, task); z_object_unref(Z_OBJECT(task)); trace("Client: Connecting to server\n"); z_socket_set_port(client, port); z_socket_initiate_connection(client); return client; }
void verify_output(ZRuntimeContext *runtime_context, ZString *expected_value) { pid_t pid; /* create a full-duplex pipe */ int pipe_to_child[2]; int pipe_to_parent[2]; pipe(pipe_to_child); pipe(pipe_to_parent); switch (pid = fork()) { case -1: perror("fork"); abort(); break; case 0: /* child */ close(pipe_to_child[1]); close(pipe_to_parent[0]); dup2(pipe_to_child[0], 0); dup2(pipe_to_parent[1], 1); execlp("node", "node", NULL); break; } close(pipe_to_child[0]); close(pipe_to_parent[1]); if (is_verbose) { /* print out the script if we are in verbose mode */ z_runtime_context_run(runtime_context); } /* backup the original stdout and redirect stdout to 'pipe_to_child' */ int original_stdout = write_stdout_to_pipe(pipe_to_child[1]); /* push the script to stdout, which is the pipe to the child */ z_runtime_context_run(runtime_context); z_runtime_context_clear(runtime_context); /* restore the original stdout */ restore_stdout(original_stdout); /* wait for child to exit */ siginfo_t info; memset (&info, 0, sizeof(info)); waitid (P_PID, pid, &info, WEXITED | WUNTRACED | WNOHANG | WNOWAIT); /* read all data from pipe_to_parent[0] and store it in a string */ ZString *out = read_from_fd(CTX_FROM_OBJECT(runtime_context), ALLOCATOR_FROM_OBJECT(runtime_context), pipe_to_parent[0]); close(pipe_to_parent[0]); /* compare the output matches the expected value */ if (z_string_compare(out, NULL, expected_value, NULL, 0, -1)) { char *s; s = z_string_get_cstring(expected_value, Z_STRING_ENCODING_UTF8); printf("Expected value: \n'%s'\n", s); free(s); s = z_string_get_cstring(out, Z_STRING_ENCODING_UTF8); printf("\nActual value: \n'%s'\n", s); free(s); abort(); } z_object_unref(Z_OBJECT(out)); }
/** * z_proxy_control_stream_read: * @stream: stream to read from * @cond: I/O condition which triggered this call * @user_data: ZProxy instance as a generic pointer * * This function is registered as the read callback for control channels * of stacked programs. **/ static gboolean z_proxy_control_stream_read(ZStream *stream, GIOCondition cond G_GNUC_UNUSED, gpointer user_data) { ZStackedProxy *stacked = (ZStackedProxy *) user_data; ZProxy *proxy = stacked->proxy; GIOStatus st; gboolean success = FALSE; ZCPCommand *request = NULL, *response = NULL; ZCPHeader *hdr1, *hdr2; guint cp_sid; ZProxyIface *iface = NULL; const gchar *fail_reason = "Unknown reason"; gboolean result = TRUE; z_enter(); g_static_mutex_lock(&stacked->destroy_lock); if (stacked->destroyed) { /* NOTE: this stacked proxy has already been destroyed, but a callback was still pending, make sure we don't come back again. Note that our arguments except stacked might already be freed. */ result = FALSE; goto exit_unlock; } if (!stacked->control_proto) stacked->control_proto = z_cp_context_new(stream); st = z_cp_context_read(stacked->control_proto, &cp_sid, &request); if (st == G_IO_STATUS_AGAIN) goto exit_unlock; if (st != G_IO_STATUS_NORMAL) { /* FIXME: hack, returning FALSE should be enough but it causes the poll loop to spin, see bug #7219 */ z_stream_set_cond(stream, G_IO_IN, FALSE); result = FALSE; goto exit_unlock; } response = z_cp_command_new("RESULT"); if (cp_sid != 0) { fail_reason = "Non-zero session-id"; goto error; } z_log(NULL, CORE_DEBUG, 6, "Read request from stack-control channel; request='%s'", request->command->str); if (strcmp(request->command->str, "SETVERDICT") == 0 ) { ZProxyStackIface *siface; iface = z_proxy_find_iface(proxy, Z_CLASS(ZProxyStackIface)); if (!iface) { fail_reason = "Proxy does not support Stack interface"; goto error; } siface = (ZProxyBasicIface *) iface; if (strcmp(request->command->str, "SETVERDICT") == 0) { ZVerdict verdict; hdr1 = z_cp_command_find_header(request, "Verdict"); hdr2 = z_cp_command_find_header(request, "Description"); if (!hdr1) { fail_reason = "No Verdict header in SETVERDICT request"; goto error; } if (strcmp(hdr1->value->str, "Z_ACCEPT") == 0) verdict = Z_ACCEPT; else if (strcmp(hdr1->value->str, "Z_REJECT") == 0) verdict = Z_REJECT; else if (strcmp(hdr1->value->str, "Z_DROP") == 0) verdict = Z_DROP; else if (strcmp(hdr1->value->str, "Z_ERROR") == 0) verdict = Z_ERROR; else verdict = Z_UNSPEC; z_proxy_stack_iface_set_verdict(iface, verdict, hdr2 ? hdr2->value->str : NULL); } } else { fail_reason = "Unknown request received"; goto error; } success = TRUE; error: z_cp_command_add_header(response, g_string_new("Status"), g_string_new(success ? "OK" : "Failure"), FALSE); if (!success) { z_cp_command_add_header(response, g_string_new("Fail-Reason"), g_string_new(fail_reason), FALSE); z_log(NULL, CORE_DEBUG, 6, "Error processing control channel request; request='%s', reason='%s'", request ? request->command->str : "None", fail_reason); } z_log(NULL, CORE_DEBUG, 6, "Responding on stack-control channel; response='%s'", response->command->str); if (z_cp_context_write(stacked->control_proto, 0, response) != G_IO_STATUS_NORMAL) { /* this should not have happened */ z_log(NULL, CORE_ERROR, 1, "Internal error writing response to stack-control channel;"); success = FALSE; } if (iface) z_object_unref(&iface->super); if (request) z_cp_command_free(request); if (response) z_cp_command_free(response); exit_unlock: g_static_mutex_unlock(&stacked->destroy_lock); z_return(result); }
static void cleanup_signal_arg(void *item, void *userdata) { ZObject **obj = (ZObject **) item; z_object_unref(*obj); }
static void case4(struct zco_context_t *context) { /* Testing z_set_find / z_set_assign */ ZSet *myset; ZString *key1, *key2, *key3, *key4, *key; ZString *test_key1, *test_key2, *test_key3; ZSetIter *it; char *raw_str; int size, found; trace("======================\n"); trace("ZSet case #4:\n"); myset = z_set_new(context, NULL); z_set_set_key_destruct(myset, (ZSetItemCallback) z_object_unref); z_set_set_compare(myset, set_compare); /* first element */ key1 = z_string_new(context, NULL); z_string_set_cstring(key1, "a", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key1); /* second element */ key2 = z_string_new(context, NULL); z_string_set_cstring(key2, "b", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key2); /* third element */ key3 = z_string_new(context, NULL); z_string_set_cstring(key3, "c", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key3); key4 = z_string_new(context, NULL); z_string_set_cstring(key4, "d", Z_STRING_ENCODING_UTF8); /* create test strings */ test_key1 = z_string_new(context, NULL); z_string_set_cstring(test_key1, "a", Z_STRING_ENCODING_UTF8); test_key2 = z_string_new(context, NULL); z_string_set_cstring(test_key2, "b", Z_STRING_ENCODING_UTF8); test_key3 = z_string_new(context, NULL); z_string_set_cstring(test_key3, "c", Z_STRING_ENCODING_UTF8); /* print myset['a'] */ it = z_set_find(myset, key1); assert(it != NULL); key = z_set_get_key(myset, it); z_object_unref(Z_OBJECT(it)); assert(z_string_compare(key, NULL, test_key1, NULL, 0, -1) == 0); if (key) { raw_str = z_string_get_cstring(key, Z_STRING_ENCODING_UTF8); trace("myset['a'].key = %s\n", raw_str); free(raw_str); } else { trace("myset['a'].key = undefined\n"); } /* print myset['b'] */ it = z_set_find(myset, key2); assert(it != NULL); key = z_set_get_key(myset, it); z_object_unref(Z_OBJECT(it)); assert(z_string_compare(key, NULL, test_key2, NULL, 0, -1) == 0); if (key) { raw_str = z_string_get_cstring(key, Z_STRING_ENCODING_UTF8); trace("myset['b'].key = %s\n", raw_str); free(raw_str); } else { trace("myset['b'].key = undefined\n"); } /* print myset['c'] */ it = z_set_find(myset, key3); assert(it != NULL); key = z_set_get_key(myset, it); z_object_unref(Z_OBJECT(it)); assert(z_string_compare(key, NULL, test_key3, NULL, 0, -1) == 0); if (key) { raw_str = z_string_get_cstring(key, Z_STRING_ENCODING_UTF8); trace("myset['c'].key = %s\n", raw_str); free(raw_str); } else { trace("myset['c'].key = undefined\n"); } /* print myset['d'] */ it = z_set_find(myset, key4); assert(it == NULL); trace("myset['d'].key = (none)\n"); size = z_set_get_size(myset); trace("myset size = %d\n", size); assert(size == 3); z_object_unref(Z_OBJECT(myset)); z_object_unref(Z_OBJECT(test_key1)); z_object_unref(Z_OBJECT(test_key2)); z_object_unref(Z_OBJECT(test_key3)); z_object_unref(Z_OBJECT(key4)); }
static void case8(struct zco_context_t *context) { /* Testing z_set_lower_bound / z_set_upper_bound */ ZSet *myset; ZSetIter *it, *end; ZString *key1, *key2, *key3, *key4, *key5; char *test_result[2] = { "a", "e" }; int i; trace("======================\n"); trace("ZSet case #8:\n"); myset = z_set_new(context, NULL); z_set_set_key_destruct(myset, (ZSetItemCallback) z_object_unref); z_set_set_compare(myset, set_compare); key1 = z_string_new(context, NULL); z_string_set_cstring(key1, "a", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key1); key2 = z_string_new(context, NULL); z_string_set_cstring(key2, "b", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key2); key3 = z_string_new(context, NULL); z_string_set_cstring(key3, "c", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key3); key4 = z_string_new(context, NULL); z_string_set_cstring(key4, "d", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key4); key5 = z_string_new(context, NULL); z_string_set_cstring(key5, "e", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key5); it = z_set_lower_bound(myset, key2); end = z_set_upper_bound(myset, key4); z_set_erase(myset, it, end); z_object_unref(Z_OBJECT(end)); z_object_unref(Z_OBJECT(it)); it = z_set_get_begin(myset); end = z_set_get_end(myset); trace("myset contains:"); for (i = 0; !z_set_iter_is_equal(it, end); z_set_iter_increment(it), ++i) { ZString *x = z_set_get_key(myset, it); char *key_cstr = z_string_get_cstring(x, Z_STRING_ENCODING_UTF8); trace(" %s", key_cstr); assert(strcmp(test_result[i], key_cstr) == 0); free(key_cstr); } trace("\n\n"); z_object_unref(Z_OBJECT(myset)); z_object_unref(Z_OBJECT(end)); z_object_unref(Z_OBJECT(it)); }
static void case2(struct zco_context_t *context) { /* Testing z_set_is_empty */ ZSet *myset; ZString *key; ZString *test_key[3]; int i; trace("======================\n"); trace("ZSet case #2:\n"); myset = z_set_new(context, NULL); z_set_set_key_destruct(myset, (ZSetItemCallback) z_object_unref); z_set_set_compare(myset, set_compare); key = z_string_new(context, NULL); z_string_set_cstring(key, "a", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); key = z_string_new(context, NULL); z_string_set_cstring(key, "b", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); key = z_string_new(context, NULL); z_string_set_cstring(key, "c", Z_STRING_ENCODING_UTF8); z_set_assign(myset, key); trace("myset contains:\n"); /* create the test key strings */ test_key[0] = z_string_new(context, NULL); test_key[1] = z_string_new(context, NULL); test_key[2] = z_string_new(context, NULL); z_string_set_cstring(test_key[0], "a", Z_STRING_ENCODING_UTF8); z_string_set_cstring(test_key[1], "b", Z_STRING_ENCODING_UTF8); z_string_set_cstring(test_key[2], "c", Z_STRING_ENCODING_UTF8); i = 0; while (!z_set_get_is_empty(myset)) { ZSetIter *first, *second; ZString *key; char *raw_key; int value; first = z_set_get_begin(myset); second = z_set_iter_dup(first); z_set_iter_increment(second); key = (ZString *) z_set_get_key(myset, first); raw_key = z_string_get_cstring(key, Z_STRING_ENCODING_UTF8); trace("\tkey: %s\n", raw_key); free(raw_key); assert(z_string_compare(key, NULL, test_key[i], NULL, 0, -1) == 0); z_set_erase(myset, first, second); z_object_unref(Z_OBJECT(second)); z_object_unref(Z_OBJECT(first)); ++i; } z_object_unref(Z_OBJECT(test_key[2])); z_object_unref(Z_OBJECT(test_key[1])); z_object_unref(Z_OBJECT(test_key[0])); trace("\n\n"); z_object_unref(Z_OBJECT(myset)); }
gboolean pop3_data_transfer(Pop3Proxy *owner) { Pop3Transfer *t; GString *preamble; gboolean success; gchar buf[256]; z_proxy_enter(owner); preamble = g_string_new(owner->response->str); if (owner->response_param->len) { g_string_append_c(preamble, ' '); g_string_append(preamble, owner->response_param->str); } g_string_append(preamble, "\r\n"); t = Z_CAST(z_dot_transfer_new(Z_CLASS(Pop3Transfer), &owner->super, owner->poll, owner->super.endpoints[EP_SERVER], owner->super.endpoints[EP_CLIENT], owner->buffer_length, owner->timeout, ZT2F_COMPLETE_COPY | ZT2F_PROXY_STREAMS_POLLED, preamble), Pop3Transfer); z_transfer2_set_content_format(&t->super.super, "email"); z_stream_line_set_nul_nonfatal(owner->super.endpoints[EP_SERVER], TRUE); if (owner->policy_enable_longline) z_stream_line_set_split(owner->super.endpoints[EP_SERVER], TRUE); success = z_transfer2_simple_run(&t->super.super); z_stream_line_set_split(owner->super.endpoints[EP_SERVER], FALSE); z_stream_line_set_nul_nonfatal(owner->super.endpoints[EP_SERVER], FALSE); if (t->super.dst_write_state == DOT_DW_PREAMBLE) { /* nothing was written to destination */ switch (z_transfer2_get_stack_decision(&t->super.super)) { case ZV_REJECT: /*LOG This message indicates that the stacked proxy rejected the content and Zorp rejects the response. */ z_proxy_log(owner, POP3_ERROR, 2, "Stacked proxy rejected contents; info='%s'", z_transfer2_get_stack_info(&t->super.super)); g_snprintf(buf, sizeof(buf), "Content rejected (%s)", z_transfer2_get_stack_info(&t->super.super)); if (owner->reject_by_mail) pop3_error_msg(owner, buf); else pop3_response_reject(owner, buf); break; case ZV_ERROR: g_snprintf(buf, sizeof(buf), "Error occurred while transferring data (%s)", z_transfer2_get_stack_info(&t->super.super)); pop3_response_reject(owner, buf); owner->pop3_state = POP3_STATE_QUIT; break; default: pop3_response_write(owner); pop3_write_client(owner, ".\r\n"); break; } } else { pop3_write_client(owner, ".\r\n"); } if (owner->from) { g_string_free(owner->from, TRUE); owner->from = NULL; } if (owner->to) { g_string_free(owner->to, TRUE); owner->to = NULL; } if (owner->subject) { g_string_free(owner->subject, TRUE); owner->subject = NULL; } z_object_unref(&t->super.super.super); z_proxy_return(owner, success); }
void zco_context_destroy(struct zco_context_t *ctx) { int i; /* Release method maps used for signals that use the fixed allocator to allocate space for the object buffer */ for (i=ctx->type_count-1; i>last_allocator_id ; --i) { struct ZCommonGlobal *global = ctx->types[i]; if (global) z_object_unref((ZObject *) global->method_map); } /* Perform full garbage collection */ zco_context_full_garbage_collect(ctx); /* Release the object trackers */ z_memory_allocator_set_object_tracker(ctx->ts_fixed_allocator, NULL); z_memory_allocator_set_object_tracker(ctx->flex_allocator, NULL); z_memory_allocator_set_object_tracker(ctx->fixed_allocator, NULL); /* Destroy objects for class static */ for (i=ctx->type_count-1; i >= 0; --i) { struct ZCommonGlobal *global = ctx->types[i]; if (global) { struct ZObjectGlobal *obj_global = ((ZObjectGlobal *) global); if (global->is_object) { z_object_class_destroy(obj_global); } } } /* Release memory allocators */ ZMemoryAllocator *ts_fixed_allocator = ctx->ts_fixed_allocator; ZMemoryAllocator *flex_allocator = ctx->flex_allocator; ZMemoryAllocator *fixed_allocator = ctx->fixed_allocator; ctx->ts_fixed_allocator = NULL; ctx->flex_allocator = NULL; ctx->fixed_allocator = NULL; z_object_unref(Z_OBJECT(ts_fixed_allocator)); z_object_unref(Z_OBJECT(flex_allocator)); z_object_unref(Z_OBJECT(fixed_allocator)); /* Release the remaining method maps used for signals. This includes the maps that were included before the fixed allocator was available for use. This is why it has to be deallocated after the allocators have been uninstalled. */ for (i=last_allocator_id; i>=0; --i) { struct ZCommonGlobal *global = ctx->types[i]; if (global) z_object_unref((ZObject *) global->method_map); } /* Free class static */ for (i=ctx->type_count - 1; i >= 0; --i) { struct ZCommonGlobal *global = ctx->types[i]; if (global) { struct ZObjectGlobal *obj_global = ((ZObjectGlobal *) global); free(obj_global->_class); free(global->vtable_off_list); free(global->svtable_off_list); free(global); } } free(ctx->types); ctx->types = NULL; ctx->type_count = 0; }