Ejemplo n.º 1
0
void		my_layer_shift_down_shape		(MyLayer *self, MyShape *theShape) {
	
	
	GQueue *shapes = MY_LAYER_GET_PRIVATE (self)->shapes;
	guint shapeCount, j;
	MyShape *shape;
	shapeCount = my_layer_shape_count (self);
	
	assert (theShape);
	for (j = shapeCount - 1; j >= 0; j--) {
		shape = my_layer_peek_nth (self, j);
		if (shape == theShape) {
			g_queue_pop_nth (shapes, j);
			break;
		}
	}
	assert (shape == theShape);
	
	shape = g_queue_peek_nth (shapes, j - 1);
	if (shape->isControlShape) {
		g_queue_push_nth (shapes, theShape, j);		
	} else {
		g_queue_push_nth (shapes, theShape, j - 1);		
	}
}
Ejemplo n.º 2
0
static void
on_druid_get_new_page (AnjutaAutogen* gen, gpointer data)
{
	NPWDruid* druid = (NPWDruid *)data;
	gint current;
	NPWPage* page;

	current = gtk_assistant_get_current_page (GTK_ASSISTANT (druid->window));
	page = g_queue_peek_nth (druid->page_list, current - (druid->no_selection ? 0 : 1));

	if (npw_page_get_name (page) == NULL)
	{
		/* no page, display finish page */
		npw_druid_fill_summary_page (druid);

		page = g_queue_pop_nth (druid->page_list, current - (druid->no_selection ? 0 : 1));
		if (page != NULL) npw_page_free (page);
		gtk_container_remove (GTK_CONTAINER (druid->window), gtk_assistant_get_nth_page (GTK_ASSISTANT (druid->window), current + 1));
		gtk_assistant_set_current_page (GTK_ASSISTANT (druid->window), current + 1);
	}
	else
	{
		/* display property page */
		npw_druid_fill_property_page (druid, page);

		gtk_assistant_set_current_page (GTK_ASSISTANT (druid->window), current + 1);
	}
}
Ejemplo n.º 3
0
/**
 *  \param id MessageID of requested message
 *
 *  Reads data from message queue until message with matching id is found.
 *
 *  \return GString containing unpacked message.
 **/
GString *get_message_by_id(MessageID id)
{
    GString *data = NULL;
    guint x, len;
    gboolean found = FALSE;

    g_mutex_lock(message_queue_mutex);
    do {
        len = g_queue_get_length(message_queue);
        for (x = 0; x<len; x++) {
            data = g_queue_peek_nth(message_queue, x);
            if (get_message_id(data) == id) {
                found = TRUE;
                g_queue_pop_nth(message_queue, x);
                break;
            }
        }

        if (found == FALSE)
            g_cond_wait(message_queue_cond, message_queue_mutex);

    } while (found == FALSE);
    g_mutex_unlock(message_queue_mutex);

    unpack_message(data);

    return data;
}
Ejemplo n.º 4
0
static int _remove_timeout_dev(void)
{
	GQueue *queue = &gl_ctx.queue;
	int len = g_queue_get_length(queue);
	int id;
	VS_DEV_T *pdev;

	for (id = 0; id < len; ) {
		pdev = g_queue_peek_nth(queue, id);
		if (!pdev)
			break;

		if (pdev->idle_time > 10) {
			dev_offline(pdev->id);

			g_queue_pop_nth(queue, id);
			hsb_debug("device offline %s\n", inet_ntoa(pdev->ip));
			g_slice_free(VS_DEV_T, pdev);

			continue;
		}

		pdev->idle_time++;

		id++;
	}

	return 0;
}
Ejemplo n.º 5
0
Archivo: queue.c Proyecto: pes0/spop
void queue_remove_tracks(gboolean notif, int idx, int nb) {
    int len;
    int i;

    g_debug("Removing %d tracks from queue, starting at %d.", nb, idx);

    if ((idx < 0) || (nb < 0))
        return;

    len = g_queue_get_length(&g_queue);

    if (idx < len) {
        if (idx + nb >= len)
            nb = len - idx;

        for (i=0; i < nb; i++)
            sp_track_release(g_queue_pop_nth(&g_queue, idx));

        /* Was the current track removed too? */
        if (g_current_track >= idx) {
            if (g_current_track < idx+nb) {
                queue_stop(FALSE);
                g_current_track = -1;
            }
            else
                g_current_track -= nb;
        }
    }

    if (g_shuffle) queue_setup_shuffle();
    g_shuffle_first = g_current_track;

    if (notif) queue_notify();
}
Ejemplo n.º 6
0
static struct avrcp_request *pop_request(uint8_t pdu_id, uint8_t event_id,
								bool peek)
{
	GSList *l;

	for (l = devices; l; l = g_slist_next(l)) {
		struct avrcp_device *dev = l->data;
		GList *reqs = g_queue_peek_head_link(dev->queue);
		int i;

		for (i = 0; reqs; reqs = g_list_next(reqs), i++) {
			struct avrcp_request *req = reqs->data;

			if (req->pdu_id != pdu_id || req->event_id != event_id)
				continue;

			if (!peek)
				g_queue_pop_nth(dev->queue, i);

			return req;
		}
	}

	return NULL;
}
Ejemplo n.º 7
0
void
account_list_move_up(guint account_index)
{
    if (account_index != 0) {
        gpointer acc = g_queue_pop_nth(accountQueue, account_index);
        g_queue_push_nth(accountQueue, acc, account_index - 1);
    }
}
Ejemplo n.º 8
0
void
account_list_move_down(guint account_index)
{
    if (account_index != accountQueue->length) {
        gpointer acc = g_queue_pop_nth(accountQueue, account_index);
        g_queue_push_nth(accountQueue, acc, account_index + 1);
    }
}
Ejemplo n.º 9
0
MyShape*	my_layer_pop_shape (MyLayer *self, MyShape *shape) {

	gint pos;
	MyLayerPrivate* priv = MY_LAYER_GET_PRIVATE (self);
	
	pos = g_queue_index (priv->shapes, shape);
	if (pos != -1) { // find
		return (MyShape *) g_queue_pop_nth (priv->shapes, pos);
	}
	return NULL;
}
Ejemplo n.º 10
0
void account_list_set_current(account_t *current)
{
    // 2 steps:
    // 1 - retrieve the index of the current account in the Queue
    // 2 - then set it as first
    guint pos = account_list_get_position(current);

    if (pos > 0) {
        gpointer acc = g_queue_pop_nth(accountQueue, pos);
        g_queue_push_nth(accountQueue, acc, 0);
    }
}
Ejemplo n.º 11
0
int random_assignment(Job *jobarray, int njobs, int nmachines,
                      solution *new_sol, GRand *rand_) {
    int i, val = 0;
    double n;
    partlist *temp = (partlist *) NULL;
    Job *j = (Job *) NULL;
    GQueue *queue = (GQueue *) NULL;
    queue = g_queue_new();

    for (i = 0; i < nmachines; ++i) {
        g_queue_push_head(queue, new_sol->part + i);
    }

    for (i = 0; i < njobs; ++i) {
        j = jobarray + i;
        n = g_rand_double_range(rand_, 0.0, 1.0);

        if (n < 0.8) {
            temp = (partlist *) g_queue_pop_head(queue);
        } else if (n >= 0.8 && n < 0.95) {
            temp = (partlist *) g_queue_pop_nth(queue, 1);
        } else {
            if(nmachines > 2) {
                temp = (partlist *)g_queue_pop_nth(queue, 2);
            } else {
                temp = (partlist *)g_queue_pop_nth(queue, 1);
            }
        }

        val = partlist_insert(temp, new_sol->vlist, j);
        CCcheck_val_2(val, "Failed in partlist_insert_order");
        g_queue_insert_sorted(queue, temp, compare_func1, NULL);
    }

CLEAN:
    g_queue_free(queue);
    return val;
}
Ejemplo n.º 12
0
API bool deleteStorePath(Store *store, char *path)
{
	bool result = false;
	GPtrArray *array = splitStorePath(path);
	char **parts = (char **) array->pdata;
	char *key = parts[array->len - 1];
	parts[array->len - 1] = NULL; // cut off last element

	char *parentpath = g_strjoinv("/", parts);

	Store *parent = getStorePath(store, "%s", parentpath);

	int i;
	void *value;

	if(parent != NULL) {
		switch(parent->type) {
			case STORE_ARRAY:
				result = g_hash_table_remove(parent->content.array, key); // The hash table frees the removed value automatically
			break;
			case STORE_LIST:
				i = atoi(key);
				value = g_queue_pop_nth(parent->content.list, i);
				if(value != NULL) {
					freeStore(value);
					result = true;
				} else {
					result = false;
				}
			break;
			default: // cannot delete inside a leaf value
				result = false;
			break;
		}
	} else {
		result = false;
	}

	// Cleanup
	free(parentpath);
	free(key);
	for(i = 0; i < array->len - 1; i++) {
		free(parts[i]);
	}
	g_ptr_array_free(array, TRUE);

	return result;
}
Ejemplo n.º 13
0
void handle_work_enqueue_event(
    awe_server_state * ns,
    tw_bf * b,
    awe_msg * m,
    tw_lp * lp)
{
    fprintf(event_log, "%lf;awe_server;%lu;WQ;work=%s\n", now_sec(lp), lp->gid, m->object_id);
    
    char* workid=NULL;
    workid = malloc(sizeof(char[MAX_LENGTH_ID]));
    strcpy(workid, m->object_id);
    assert(workid);
    tw_lpid *clientid;
    int has_match = 0;

    int len = g_queue_get_length(client_req_queue);
    if (len > 0) {
    	int n = -1;
    	for (int i=0; i<len; i++) {
    		clientid = g_queue_peek_nth(client_req_queue, i);
    		if (client_match_work(*clientid, workid)) {
    			 n = i;
    			 break;
    		}
    	}
    	if (n >=0) {
    		clientid = g_queue_pop_nth(client_req_queue, n);
    		has_match = 1;
    	}
    }
    
    if (has_match) {
        tw_event *e;
        awe_msg *msg;
        e = codes_event_new(*clientid, ns_tw_lookahead, lp);
        msg = tw_event_data(e);
        msg->event_type = WORK_CHECKOUT;
        strcpy(msg->object_id, workid);
        tw_event_send(e);
        fprintf(event_log, "%lf;awe_server;%lu;WC;work=%s client=%lu\n", now_sec(lp), lp->gid, workid, *clientid);
        free(workid);
        free(clientid);
    } else {
    	g_queue_push_tail(work_queue, workid);
    }
    return;
}
Ejemplo n.º 14
0
guint garbage_connection_pool_clean_old_entries(network_garbage_connection_pool *gc_pool, gint grace_secs) {
	guint count = 0;
	guint i = 0;
	network_connection_pool_entry *entry = NULL;
	GTimeVal now_tv;
	guint q_len_before = 0;
	guint q_len_after = 0;

	g_assert(gc_pool);

	g_get_current_time(&now_tv);

	g_mutex_lock(&(gc_pool->mutex));
	q_len_before = g_queue_get_length(gc_pool->entries);

	for (i = 0; i < g_queue_get_length(gc_pool->entries); ) {
		entry = g_queue_peek_nth(gc_pool->entries, i);
		if (entry == NULL) {
			i++;
			continue;
		}
		if (now_tv.tv_sec > (entry->added_ts).tv_sec + grace_secs) {
			//remove this entry
			entry = g_queue_pop_nth(gc_pool->entries, i);
			network_connection_pool_entry_free(entry, TRUE);
			entry = NULL;
			count++;
		} else {
			i++;
		}
	}

	q_len_after = g_queue_get_length(gc_pool->entries);

	g_mutex_unlock(&(gc_pool->mutex));

#ifdef DEBUG_CONN_POOL
	if (q_len_before > 0) {
		g_debug("[%s]: gc_pool length before: %d, after: %d, removed: %d",
				G_STRLOC, q_len_before, q_len_after, count);
	}
#endif

	return count;
}
Ejemplo n.º 15
0
char* get_first_work_by_stage(int stage) {
	char *workid = NULL;
	int len = g_queue_get_length(work_queue);
    int n = -1;
	for (int i=0; i<len; i++) {
	    workid = g_queue_peek_nth(work_queue, i);
        gchar **seg = g_strsplit(workid, "_", 3);
        int taskid = atoi(seg[1]);
        if (taskid == stage) {
        	n = i;
        	break;
        }
	}
	if (n >= 0) {
		return g_queue_pop_nth(work_queue, n);
	}
	return NULL;
}
Ejemplo n.º 16
0
static void
npw_druid_remove_following_page (NPWDruid* druid)
{
	gint current;

	current = gtk_assistant_get_current_page (GTK_ASSISTANT (druid->window));
	for(;;)
	{
		GtkWidget *widget;
		NPWPage* page;

		widget = gtk_assistant_get_nth_page (GTK_ASSISTANT (druid->window), current + 1);
		if (widget == druid->finish_page) break;

		gtk_container_remove (GTK_CONTAINER (druid->window), widget);

		page = g_queue_pop_nth (druid->page_list, current  - (druid->no_selection ? 0 : 1));
		if (page != NULL) npw_page_free (page);
	}
}
Ejemplo n.º 17
0
void navqueue_go_forward()
{
	filepos *fnext;

	if (nav_queue_pos < 1 ||
		nav_queue_pos >= g_queue_get_length(navigation_queue))
		return;

	/* jump forward */
	fnext = g_queue_peek_nth(navigation_queue, nav_queue_pos - 1);
	if (goto_file_pos(fnext->file, fnext->pos))
	{
		nav_queue_pos--;
	}
	else
	{
		/** TODO: add option to re open the file */
		g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos - 1));
	}

	adjust_buttons();
}
Ejemplo n.º 18
0
TEST_F(GQueueTest, popNth)
{
	int testData1 = 42;
	int testData2 = 1337;
	int testData3 = 27;

	g_queue_push_tail(queue, &testData1);
	g_queue_push_tail(queue, &testData2);
	g_queue_push_tail(queue, &testData3);

	gpointer data = g_queue_pop_nth(queue, 1);
	ASSERT_EQ(&testData2, data) << "popped queue head should have correct value";
	ASSERT_TRUE(queue->head != NULL) << "queue head should not be NULL after removing one element";
	ASSERT_NE(queue->head, queue->tail) << "queue tail should not be equal to head after removing one element";
	ASSERT_EQ(&testData1, queue->head->data) << "queue head data should be set correctly";
	ASSERT_EQ(queue->tail, queue->head->next) << "queue head element next should be tail";
	ASSERT_TRUE(queue->head->prev == NULL) << "queue head should not have a previous element";
	ASSERT_EQ(&testData3, queue->tail->data) << "queue tail data should be set correctly";
	ASSERT_EQ(queue->head, queue->tail->prev) << "queue tail element previous should be head";
	ASSERT_TRUE(queue->tail->next == NULL) << "queue tail should not have a next element";
	ASSERT_EQ(2, queue->length) << "queue should have one element left after popping one";
}
Ejemplo n.º 19
0
void navqueue_go_back()
{
	filepos *fprev;

	/* return if theres no place to go back to */
	if (g_queue_is_empty(navigation_queue) ||
		nav_queue_pos >= g_queue_get_length(navigation_queue) - 1)
		return;

	/* jump back */
	fprev = g_queue_peek_nth(navigation_queue, nav_queue_pos + 1);
	if (goto_file_pos(fprev->file, fprev->pos))
	{
		nav_queue_pos++;
	}
	else
	{
		/** TODO: add option to re open the file */
		g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos + 1));
	}
	adjust_buttons();
}
Ejemplo n.º 20
0
int flom_resource_simple_clean(flom_resource_t *resource,
                               flom_conn_t *conn)
{
    enum Exception { NULL_OBJECT
                     , SIMPLE_WAITINGS_ERROR
                     , INTERNAL_ERROR
                     , NONE } excp;
    int ret_cod = FLOM_RC_INTERNAL_ERROR;
    
    FLOM_TRACE(("flom_resource_simple_clean\n"));
    TRY {
        GSList *p = NULL;

        if (NULL == resource)
            THROW(NULL_OBJECT);
        /* check if the connection keeps a lock */
        p = resource->data.simple.holders;
        while (NULL != p) {
            if (((struct flom_rsrc_conn_lock_s *)p->data)->conn == conn)
                break;
            else
                p = p->next;
        } /* while (NULL != p) */
        if (NULL != p) {
            struct flom_rsrc_conn_lock_s *cl =
                (struct flom_rsrc_conn_lock_s *)p->data;
            FLOM_TRACE(("flom_resource_simple_clean: the client is holding "
                        "a lock mode %d, removing it...\n",
                        cl->info.lock_mode));
            FLOM_TRACE(("flom_resource_simple_clean: cl=%p\n", cl));
            resource->data.simple.holders = g_slist_remove(
                resource->data.simple.holders, cl);
            /* free the now useless connection lock record */
            flom_rsrc_conn_lock_delete(cl);
            /*
            FLOM_TRACE(("flom_resource_simple_clean: g_slist_length=%u\n",
                        g_slist_length(resource->data.simple.holders)));
            */
            /* check if some other clients can get a lock now */
            if (FLOM_RC_OK != (ret_cod = flom_resource_simple_waitings(
                                   resource)))
                THROW(SIMPLE_WAITINGS_ERROR);
        } else {
            guint i = 0;
            /* check if the connection was waiting a lock */
            do {
                struct flom_rsrc_conn_lock_s *cl =
                    (struct flom_rsrc_conn_lock_s *)
                    g_queue_peek_nth(resource->data.simple.waitings, i);
                if (NULL == cl)
                    break;
                if (cl->conn == conn) {
                    /* remove from waitings */
                    FLOM_TRACE(("flom_resource_simple_clean: the client is "
                                "waiting for a lock mode %d, removing "
                                "it...\n", cl->info.lock_mode));
                    cl = g_queue_pop_nth(resource->data.simple.waitings, i);
                    if (NULL == cl) {
                        /* this should be impossibile because peek was ok
                           some rows above */
                        THROW(INTERNAL_ERROR);
                    } else {
                        /* free the now useless connection lock record */
                        flom_rsrc_conn_lock_delete(cl);
                    }
                    break;
                } else
                    ++i;
            } while (TRUE);
        } /* if (NULL != p) */
                
        THROW(NONE);
    } CATCH {
        switch (excp) {
            case NULL_OBJECT:
                ret_cod = FLOM_RC_NULL_OBJECT;
                break;
            case SIMPLE_WAITINGS_ERROR:
                break;
            case INTERNAL_ERROR:
                ret_cod = FLOM_RC_INTERNAL_ERROR;
                break;
            case NONE:
                ret_cod = FLOM_RC_OK;
                break;
            default:
                ret_cod = FLOM_RC_INTERNAL_ERROR;
        } /* switch (excp) */
    } /* TRY-CATCH */
    FLOM_TRACE(("flom_resource_simple_clean/excp=%d/"
                "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
    return ret_cod;
}
Ejemplo n.º 21
0
static void meh_screen_popup_favorite_toggle(App* app, Screen* screen) {
    g_assert(app != NULL);
    g_assert(screen != NULL);

    PopupData* data = meh_screen_popup_get_data(screen);
    ExecutableListData* exec_list_data = meh_exec_list_get_data(data->src_screen);

    /* updates the value of the executable */

    gboolean new_value = data->executable->favorite == 1 ? FALSE : TRUE;

    if (meh_db_set_executable_favorite(app->db, data->executable, new_value)) {
        data->executable->favorite = new_value;
    }

    /* re-position the executable in the executables list if necessary */
    if (g_queue_get_length(exec_list_data->executables) > 1) {
        int prev_selected = exec_list_data->selected_executable;

        unsigned int i = 0;

        /* retrieves the one which will move in the list */
        Executable* to_move = g_queue_pop_nth(exec_list_data->executables, exec_list_data->selected_executable);

        /* find the good position for the moved executable */

        for (i = 0; i < g_queue_get_length(exec_list_data->executables); i++) {
            gboolean exit = FALSE;
            Executable* ex = g_queue_peek_nth(exec_list_data->executables, i);
            /* if favorite, ensure to stay in the favorite zone */
            if (new_value == TRUE) {
                if (ex->favorite == FALSE)  {
                    exit = TRUE;
                }
            }

            gchar* first = g_utf8_strup(ex->display_name, g_utf8_strlen(ex->display_name, -1));
            gchar* second = g_utf8_strup(data->executable->display_name, g_utf8_strlen(ex->display_name, -1));

            if (g_utf8_collate(first, second) > 0) {
                if (new_value == TRUE && ex->favorite == TRUE) {
                    exit = TRUE;
                }
                else if (new_value == FALSE && ex->favorite == FALSE) {
                    exit = TRUE;
                }
            }

            g_free(first);
            g_free(second);

            if (exit) {
                break;
            }
        }

        GList* after = g_queue_peek_nth_link(exec_list_data->executables, i);

        /* re-add it to the good position */

        g_queue_insert_before(exec_list_data->executables, after, to_move);

        /* notify the screen of the new selected executable */

        exec_list_data->selected_executable = i;

        /* redraw the executables list texts */

        meh_exec_list_refresh_executables_widget(app, data->src_screen);

        /* move and redraw the selection */

        meh_exec_list_after_cursor_move(app, data->src_screen, prev_selected);
    }

    /* finally close the popup */

    meh_screen_popup_close(screen);
}
Ejemplo n.º 22
0
int flom_resource_simple_waitings(flom_resource_t *resource)
{
    enum Exception { INTERNAL_ERROR
                     , MSG_BUILD_ANSWER_ERROR
                     , MSG_SERIALIZE_ERROR
                     , MSG_SEND_ERROR
                     , MSG_FREE_ERROR
                     , NONE } excp;
    int ret_cod = FLOM_RC_INTERNAL_ERROR;
    struct flom_rsrc_conn_lock_s *cl = NULL;
    
    FLOM_TRACE(("flom_resource_simple_waitings\n"));
    TRY {
        guint i = 0;
        struct flom_msg_s msg;
        char buffer[FLOM_NETWORK_BUFFER_SIZE];
        size_t to_send;
        
        /* check if there is any connection waiting for a lock */
        do {
            cl = (struct flom_rsrc_conn_lock_s *)
                g_queue_peek_nth(resource->data.simple.waitings, i);
            if (NULL == cl)
                break;
            /* try to apply this lock... */
            if (flom_resource_simple_can_lock(resource, cl->info.lock_mode)) {
                /* remove from waitings */
                cl = g_queue_pop_nth(resource->data.simple.waitings, i);
                if (NULL == cl)
                    /* this should be impossibile because peek was ok
                       some rows above */
                    THROW(INTERNAL_ERROR);
                /* send a message to the client that is waiting the lock */
                flom_msg_init(&msg);
                if (FLOM_RC_OK != (ret_cod = flom_msg_build_answer(
                                       &msg, FLOM_MSG_VERB_LOCK,
                                       3*FLOM_MSG_STEP_INCR,
                                       FLOM_RC_OK, NULL)))
                    THROW(MSG_BUILD_ANSWER_ERROR);
                if (FLOM_RC_OK != (
                        ret_cod = flom_msg_serialize(
                            &msg, buffer, sizeof(buffer), &to_send)))
                    THROW(MSG_SERIALIZE_ERROR);
                if (FLOM_RC_OK != (ret_cod = flom_conn_send(
                                       cl->conn, buffer, to_send)))
                    THROW(MSG_SEND_ERROR);
                flom_conn_set_last_step(cl->conn, msg.header.pvs.step);
                if (FLOM_RC_OK != (ret_cod = flom_msg_free(&msg)))
                    THROW(MSG_FREE_ERROR);                
                /* insert into holders */
                resource->data.simple.holders = g_slist_prepend(
                    resource->data.simple.holders,
                    (gpointer)cl);
                cl = NULL;
            } else
                ++i;
        } while (TRUE);
        
        THROW(NONE);
    } CATCH {
        switch (excp) {
            case INTERNAL_ERROR:
                ret_cod = FLOM_RC_INTERNAL_ERROR;
                break;
            case MSG_BUILD_ANSWER_ERROR:
            case MSG_SERIALIZE_ERROR:
            case MSG_SEND_ERROR:
            case MSG_FREE_ERROR:
                break;
            case NONE:
                ret_cod = FLOM_RC_OK;
                break;
            default:
                ret_cod = FLOM_RC_INTERNAL_ERROR;
        } /* switch (excp) */
    } /* TRY-CATCH */
    if (NULL != cl) {
        flom_rsrc_conn_lock_delete(cl);
    }
    FLOM_TRACE(("flom_resource_simple_waitings/excp=%d/"
                "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
    return ret_cod;
}
Ejemplo n.º 23
0
static gssize
socket_send_message (NiceSocket *sock, const NiceOutputMessage *message)
{
  TcpPriv *priv = sock->priv;
  gssize ret;
  GError *gerr = NULL;
  gsize message_len;

  /* Don't try to access the socket if it had an error, otherwise we risk a
   * crash with SIGPIPE (Broken pipe) */
  if (priv->error)
    return -1;

  message_len = output_message_get_size (message);

  /* First try to send the data, don't send it later if it can be sent now
   * this way we avoid allocating memory on every send */
  if (g_queue_is_empty (&priv->send_queue)) {
    ret = g_socket_send_message (sock->fileno, NULL, message->buffers,
        message->n_buffers, NULL, 0, G_SOCKET_MSG_NONE, NULL, &gerr);

    if (ret < 0) {
      if (g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK) ||
          g_error_matches (gerr, G_IO_ERROR, G_IO_ERROR_FAILED)) {
        /* Queue the message and send it later. */
        add_to_be_sent (sock, message, 0, message_len, FALSE);
        ret = message_len;
      }

      g_error_free (gerr);
    } else if ((gsize) ret < message_len) {
      /* Partial send. */
      add_to_be_sent (sock, message, ret, message_len, TRUE);
      ret = message_len;
    }
  } else {
    /* FIXME: This dropping will break http/socks5/etc
     * We probably need a way to the upper layer to control reliability
     */
    /* If the queue is too long, drop whatever packets we can. */
    if (g_queue_get_length (&priv->send_queue) >= MAX_QUEUE_LENGTH) {
      guint peek_idx = 0;
      struct to_be_sent *tbs = NULL;

      while ((tbs = g_queue_peek_nth (&priv->send_queue, peek_idx)) != NULL) {
        if (tbs->can_drop) {
          tbs = g_queue_pop_nth (&priv->send_queue, peek_idx);
          free_to_be_sent (tbs);
          break;
        } else {
          peek_idx++;
        }
      }
    }

    /* Queue the message and send it later. */
    add_to_be_sent (sock, message, 0, message_len, FALSE);
    ret = message_len;
  }

  return ret;
}
Ejemplo n.º 24
0
int random_rcl_assignment(Job *jobarray, int njobs, int nmachines,
                          solution *new_sol, GRand *rand_) {
    int i;
    double max;
    double min;
    double lb, ub;
    double temp_dbl;
    partlist *temp = (partlist *) NULL;
    Job *temp_job = (Job *) NULL;
    //GList *it = (GList *) NULL;
    GQueue *to_do_list = (GQueue *) NULL;
    temp = new_sol->part;
    to_do_list = g_queue_new();

    for (i = 0; i < njobs; ++i) {
        g_queue_push_tail(to_do_list, jobarray + i);
    }

    while (!g_queue_is_empty(to_do_list)) {
        temp_job = (Job *)to_do_list->head->data;
        max = ((double)temp[0].completiontime + (double)temp_job->processingime);
        min = max;
        GArray *rcl = g_array_new(FALSE, FALSE, sizeof(pair_job_machine));

        /** Compute min and max */
        for (i = 1; i < nmachines; ++i) {
            //for (it = to_do_list->head; it; it = it->next)
            //{
            //temp_job = (Job*)it->data;
            temp_dbl = (temp[i].completiontime + temp_job->processingime);

            if (max < temp_dbl) {
                max = temp_dbl;
            }

            if (min > temp_dbl) {
                min = temp_dbl;
            }

            //}
        }

        /** Compute RCL */
        pair_job_machine temp_job_machine;
        lb = min;
        ub = min + 0.25 * (max - lb);

        for (i = 0; i < nmachines; ++i) {
            //for (it = to_do_list->head; it; it = g_list_next(it))
            //{
            //temp_job = ((Job*)it->data);
            double g = ((double)temp[i].completiontime + (double)temp_job->processingime);

            if (lb <= g && g <= ub) {
                temp_job_machine.job = temp_job->job;
                temp_job_machine.machine = i;
                g_array_append_val(rcl, temp_job_machine);
            }

            //}
        }

        /** Choose uniformaly an assignment of a job to a machine */
        int a = g_rand_int_range(rand_, 0, rcl->len);
        int job = g_array_index(rcl, pair_job_machine, a).job;
        int machine = g_array_index(rcl, pair_job_machine, a).machine;
        partlist_insert(temp + machine, new_sol->vlist, jobarray + job);
        g_queue_pop_nth(to_do_list, g_queue_index(to_do_list, jobarray + job));
        g_array_free(rcl, TRUE);
    }

    g_queue_free(to_do_list);
    return 0;
}
Ejemplo n.º 25
0
/**
 * get a connection from the pool
 *
 * make sure we have at lease <min-conns> for each user
 * if we have more, reuse a connect to reauth it to another user
 *
 * @param pool connection pool to get the connection from
 * @param username (optional) name of the auth connection
 * @param default_db (unused) unused name of the default-db
 */
network_socket *network_connection_pool_get(network_connection_pool *pool,
		GString *username,
		GString *UNUSED_PARAM(default_db), conn_ctl_info *info) {

    guint32  cur;
    guint    len, i;
	network_socket *sock = NULL;
	network_connection_pool_entry *entry, *found_entry = NULL;

	GQueue *conns = network_connection_pool_get_conns(pool, username, NULL);

	/**
	 * if we know this use, return a authed connection 
	 */
	if (conns) {
        cur = time(0);
        len = g_queue_get_length(conns);
        for (i = 0; i < len; i++) {
            entry = g_queue_peek_nth(conns, i);
            if (entry->key == info->key) {
                if (!entry->shared) {
                    found_entry = entry;
                    g_queue_pop_nth (conns, i);
                    break;
                }
            }
        }

        if (!found_entry && len > 0) {
            entry = g_queue_peek_nth(conns, 0);
            found_entry = entry;
            g_queue_pop_nth (conns, 0);
            g_debug("%s: (get) entry for user '%s' -> %p, cur:%u",
                    G_STRLOC, username ? username->str : "", entry, cur);
        }

		if (conns->length == 0) {
			/**
			 * all connections are gone, remove it from the hash
			 */
			g_hash_table_remove(pool->users, username);
		}
	}

    if (!found_entry) {
		g_debug("%s: (get) no entry for user '%s' -> %p", G_STRLOC, username ? username->str : "", conns);
		return NULL;
	}

	sock = found_entry->sock;

    g_debug("%s: recv queue length:%d, sock:%p", 
                        G_STRLOC, sock->recv_queue->chunks->length, sock);

	network_connection_pool_entry_free(found_entry, FALSE);

	/* remove the idle handler from the socket */	
	event_del(&(sock->event));
		
	g_debug("%s: (get) got socket for user '%s' -> %p", G_STRLOC, username ? username->str : "", sock);

	return sock;
}