Exemple #1
0
void		Drone::Recive()
{
  std::string	str = this->net.getmsg();
  std::string	tmp;
  unsigned int	pos;

  while (str.find("\n") != std::string::npos && this->level != MAX)
    {
      pos = str.find("\n");
      tmp = str.substr(0, pos);

      if (tmp.find("message") == 0)
	Check_msg(tmp);
      else if (tmp.find("deplacement") == 0)
	{}
      else if (tmp.find("mort") == 0)
	throw My_Exception("Sorry you are dead.");
      else if (!this->rep.empty())
	(this->*actions[this->rep.front()])(tmp);
      str = str.substr(pos + 1);
    }
}
Exemple #2
0
static int LB_write_internal (int lbd, const char *msg, int length, LB_id_t id)
{
    LB_struct *lb;
    unsigned int num_ptr, org_num_ptr;
    int ptr, nmsgs;
    int n_rm;
    LB_dir *dir;
    int loc, new_space;
    int ret, ind, tag;

    if ((length < 0 && msg != DELETE_FLAG) || (length > 0 && msg == NULL) ||
	(id > LB_MAX_ID && id != LB_ANY))	/* check arguments */
	return (LB_BAD_ARGUMENT);

    /* get the LB structure, lock and mmap the file */
    lb = LB_Get_lb_structure (lbd, &ret);
    if (lb == NULL) 
	return (ret);
    if ((ret = LB_lock_mmap (lb, WRITE_PERM, EXC_LOCK)) < 0)
	return (LB_Unlock_return (lb, ret));

    if (!(lb->flags & LB_WRITE))	/* check write flag */
	return (LB_Unlock_return (lb, LB_BAD_ACCESS));

    if ((lb->flags & LB_DIRECT) &&	/* check direct access lock */
	LB_direct_access_lock (lb, TEST_LOCK, 0) != 0)
	return (LB_Unlock_return (lb, LB_BAD_ACCESS));

    if (length <= 0 && !(lb->flags & LB_DB))
	return (LB_Unlock_return (lb, LB_BAD_ARGUMENT));

    if (msg == DELETE_FLAG && (lb->hd->miscflags & LB_ID_BY_USER))
	return (LB_Unlock_return (lb, LB_NOT_SUPPORTED));

    if (lb->active_test) {		/* test LB_ACTIVE_SV_LOCK_OFF lock */
	ret = LB_process_lock (TEST_LOCK, lb, EXC_LOCK, LB_ACTIVE_SV_LOCK_OFF);
	if (ret != LB_LOCKED)
	    return (LB_Unlock_return (lb, LB_NOT_ACTIVE));
    }

    if (C_and_w && Check_msg (lb, msg, length, id))
	return (LB_Unlock_return (lb, 0));

    org_num_ptr = lb->hd->num_pt;	/* save for evaluating # expired msgs */

    if (lb->utag != NULL)
	tag = *(lb->utag);
    else
	tag = 0;

    /* process message replacing */
    if (lb->flags & LB_DB) {
	ret = Replace_message (lb, id, length, msg, tag);
	if (ret != MSG_NOT_REPLACED)
	    return (LB_Unlock_return (lb, ret));
    }

    /* find the write pointer and the new dir slot */
    num_ptr = lb->hd->num_pt;
    ptr = GET_POINTER (num_ptr);
    ind = ptr % lb->n_slots;		/* new slot index */
    nmsgs = GET_NMSG (num_ptr) + 1;	/* msg number including the new */

    if (lb->flags & LB_DB) {
	if (id != LB_ANY && (lb->hd->miscflags & LB_MSG_DELETED))
	    return (LB_Unlock_return (lb, LB_NOT_SUPPORTED));
	if (nmsgs > lb->maxn_msgs)
	    return (LB_Unlock_return (lb, LB_FULL));
    }

    /* get free space */
    loc = new_space = 0;
    if (length > 0)
	loc = LB_sms_get_free_space (lb, length, &new_space);
    if (loc < 0)
	return (LB_Unlock_return (lb, loc));

    /* write the message */
    if (length > 0 &&
	(ret = Write_msg_body (lb, loc, length, msg, new_space)) < 0)
	return (LB_Unlock_return (lb, ret));

    /* update the dir slot and the msg info */
    dir = lb->dir + ind;
    if (lb->flags & LB_DB) {
	LB_msg_info_t *msginfo;
	dir->loc = 0;			/* for ucnt */
	msginfo = (LB_msg_info_t *)lb->msginfo + DB_WORD_OFFSET (ind, 0);
	msginfo->len = length;
	msginfo->loc = loc;
    }
    else if (lb->ma_size == 0) {
	LB_msg_info_seq_t *msginfo;
	dir->loc = loc;
	msginfo = (LB_msg_info_seq_t *)lb->msginfo + ind;
	msginfo->len = length;
    }
    else
	dir->loc = (loc + length) % lb->ma_size;
    {	/* find the previous id and check if the LB is of non-decreasing ID */
	LB_id_t prid;

	/* this works since the entire control area is initialized to 0 */
	if (nmsgs > 1) {	/* find previous id */
	    int ppt;		/* pointer to the previous slot */
	    ppt = ptr - 1;
	    if (ppt < 0)
		ppt = lb->ptr_range - 1;
	    prid = (lb->dir [ppt % lb->n_slots]).id;
	}
	else
	    prid = 0;
	if (id == LB_ANY)		/* new id */
	    id = (lb_t)((prid + 1) % (unsigned int)(LB_MAX_ID + 1));
	else
	    lb->hd->miscflags |= LB_ID_BY_USER;
	if (id < prid && nmsgs > 1)	/* The LB is not of non-decreasing ID */
	    lb->hd->non_dec_id &= 0xfe;
    }
    dir->id = id;

    /* write the tag for the new message */
    if (lb->hd->tag_size > 0)
	LB_write_tag (lb, ind, tag, 0);

    /* process nrs and update LB time */
    if (lb->off_nra > 0 &&
	(ret = LB_process_nr (lb, id, length, tag,
				N_MSG_RM_COMPU, org_num_ptr)) < 0)
	    return (LB_Unlock_return (lb, ret));
    lb->hd->lb_time = time (NULL);

    /* update num_ptr and page number */
    if (nmsgs > lb->maxn_msgs)
	n_rm = nmsgs - lb->maxn_msgs;	/* number of messages to remove */
    else
	n_rm = 0;
    LB_Update_pointer (lb, 1, n_rm);	/* we add one new message */

    if (((lb->flags & LB_DB) || lb->ma_size == 0) &&
	length > 0)			/* sets sms_ok flag */
	lb->hd->sms_ok = 1;

    if (nmsgs > lb->maxn_msgs && lb->ma_size == 0) {
	int ln, lc, p;

	p = ptr - nmsgs + 1 + lb->n_slots;
	ln = LB_Get_message_info (lb, p, &lc, NULL);
	LB_sms_free_space (lb, lc, ln);
    }

    if (lb->flags & LB_SHARE_STREAM) {
	int exppt = (GET_POINTER (lb->hd->num_pt) + lb->ptr_range -
			(lb->maxn_msgs + 1)) % lb->ptr_range;
	if (LB_Ptr_compare (lb, exppt, (int)lb->hd->ptr_read) > 0)
	    lb->hd->ptr_read = exppt;
    }		/* advance ptr_read to keep close to the available msgs */

    lb->prev_id = id;
    if (lb->umid != NULL)
	*lb->umid = id;
    if (length < 0)
	length = 0;
    return (LB_Unlock_return (lb, length));
}