recieving zero value from vector inside of a struct in cpp

61 views Asked by At

i am trying to send a struct to another process via a pipe with write(), this struct contains a vector and the vector gives zero values when being read by the other process. whenever the process sends a message it increases its index of the vector and when it recieves a message it also increases its index of the vector and updates the other indices to the max of the recieved value and the value of the previously stored value of the vector (improves peterson kearns protocol) this is the struct i am sending

struct ptp_msg
{
    char msg_buf[50];
    int sending_process_nr;
};

struct ptp_err
{
    pid_t pid;
    int sending_process_nr;
    int fildes[2];
};

struct msg_t
{
    message_type msg_type;
    vector<int> time_v;
    vector<int> fail_v;
    union contents
    {
        struct ptp_msg ptp_msg;
        struct ptp_err ptp_err;
    } contents;
};

this is the code that sends the struct

int Pet_kea::send_msg(char *input, int fildes[2])
{
    // inc T^i
    time_v[id]++;

    struct msg_t msg;

    msg.msg_type = MSG;
    sprintf(msg.contents.ptp_msg.msg_buf, input);
    msg.time_v = time_v;
    msg.contents.ptp_msg.sending_process_nr = id;
    msg.fail_v = fail_v;

    // send the message
    cout << id << " vector sent =" << msg.time_v[0] << "-" << msg.time_v[1] << endl;

    if (write(fildes[1], &msg, sizeof(msg)) < 0)
    {
        // handle error
    }

    store_msg(&msg, false);

    return 0;
}

int Pet_kea::recv_msg(int fildes[2], struct msg_t *ret_buf)
{
    // read message
    int ret;
    ret = read(fildes[0], ret_buf, sizeof(msg_t));
    // do err checking
    if (ret < 0)
    {
    }
    cout << id << " vector recieved =" << ret_buf->time_v[0] << "-" << ret_buf->time_v[1] << endl;

    // check if it is an errmsg

    // inc T^i and inc T^/j to max(T^j of send event, prev event T^j)
    time_v[id]++;
    for (int i = 0; i < (int)time_v.size(); i++)
    {
        if (i == id)
            continue;
        time_v.at(i) = max(ret_buf->time_v[i], time_v[i]);
    }

    store_msg(ret_buf, true);

    return 0;
}

thnx for the help beforehand

i have tried to see if the problem was with my logic but the output of the test(also in the code above) is 0 vector sent =1-0 1 vector recieved =0-0 and these statements are right before write and right after read so they should match

1

There are 1 answers

0
robybert On

thnx for the help, this is the rudimentary code that i came up with:

void Pet_kea::serialize(struct msg_t *msg, char *data)
{
    int *q = (int *)data;
    *q = msg->msg_type;
    q++;
    *q = msg->sending_process_nr;
    q++;
    for (int i = 0; i < (int)time_v.size(); i++)
    {
        *q = msg->time_v[i];
        q++;
    }

    for (int i = 0; i < (int)fail_v.size(); i++)
    {
        *q = msg->fail_v[i];
        q++;
    }
    if (msg->msg_type == MSG)
    {
        char *p = (char *)q;
        for (int i = 0; i < 50; i++)
        {
            *p = msg->contents.ptp_msg.msg_buf[i];
            p++;
        }
    }
    else
    {
        // TODO: write serialization for errmsg
    }
}
void Pet_kea::deserialize(char *data, struct msg_t *msg)
{
    int *q = (int *)data;
    msg->msg_type = (msg_type)*q;
    q++;
    msg->sending_process_nr = *q;
    q++;

    for (int i = 0; i < (int)time_v.size(); i++)
    {
        msg->time_v[i] = *q; //////
        q++;
    }

    for (int i = 0; i < (int)fail_v.size(); i++)
    {
        msg->fail_v[i] = *q;
        q++;
    }
    if (msg->msg_type == MSG)
    {
        char *p = (char *)q;
        for (int i = 0; i < 50; i++)
        {
            msg->contents.ptp_msg.msg_buf[i] = *p;
            p++;
        }
    }
    else
    {
        // TODO: write deserialization for errmsg
    }
}

i will take a look at the flatbuffer and protobuffer libraries that were mentioned. any tips are still welcome