Romanof123 / document

0 stars 0 forks source link

Inter process Communication2 #6

Open Romanof123 opened 4 years ago

Romanof123 commented 4 years ago

pulse

MsgSendPulse

MsgReceive

Romanof123 commented 4 years ago

pulse test server code

////////////////////////////////////////////////////////////////////////////////
// server.c
//
// A QNX msg passing server.  It should receive a string from a client,
// calculate a checksum on it, and reply back the client with the checksum.
//
// The server prints out its pid and chid so that the client can be made aware
// of them.
//
// Using the comments below, put code in to complete the program.  Look up 
// function arguments in the course book or the QNX documentation.
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/neutrino.h>
#include <process.h>

#include "pulse_msg_def.h"  //layout of msg's should always defined by a struct, here's its definition

int
calculate_checksum(char *text);

int main(void)
{
    int chid;
    int pid;
    int rcvid;
    Mymassege_t msg;
    int status;
    int checksum;

    //PUT CODE HERE to create a channel, store channel id in the chid variable
    chid = ChannelCreate(0);
    if (-1 == chid)
    { //was there an error creating the channel?
        perror("ChannelCreate()"); //look up the errno code and print
        exit(EXIT_FAILURE);
    }

    pid = getpid(); //get our own pid
    printf("Server's pid: %d, chid: %d\n", pid, chid); //print our pid/chid so
    //client can be told where to connect
    while (1)
    {
        //PUT CODE HERE to receive msg from client
        rcvid = MsgReceive(chid, &msg,sizeof(msg),NULL);
        if (-1 == rcvid)
        { //was there an error receiving msg?
            perror("MsgReceive"); //look up errno code and print
            exit(EXIT_FAILURE); //give up
        }else if(rcvid==0){
            printf("i am pulse code is %d ,value %d\n",(int)msg.pulse.code,(int)msg.pulse.value.sival_int);
            goto END;
        }

        //PUT CODE HERE to calculate the check sum by calling calculate_checksum()

        checksum = calculate_checksum(msg.Mymsg.string_to_cksum);
        //PUT CODE HERE TO reply to client with checksum, store the return status in status
        status = MsgReply(rcvid,EOK,&checksum,sizeof(checksum));

        if (-1 == status)
        {
            perror("MsgReply");
            exit(EXIT_FAILURE);
        }
    }
END:
    return 0;
}

int calculate_checksum(char *text)
{
    char *c;
    int cksum = 0;

    for (c = text; *c != NULL; c++)
        cksum += *c;
    return cksum;
}
Romanof123 commented 4 years ago

pulse test client

////////////////////////////////////////////////////////////////////////////////
// client.c
//
// A QNX msg passing client.  It's purpose is to send a string of text to a
// server.  The server calculates a checksum and replies back with it.  The client
// expects the reply to come back as an int
//
// This program program must be started with commandline args.  
// See  if(argc != 4) below
//
// To complete the exercise, put in the code, as explained in the comments below
// Look up function arguments in the course book or the QNX documentation.
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/neutrino.h>
#include <sys/netmgr.h>     // #define for ND_LOCAL_NODE is in here
#include "pulse_msg_def.h"

int main(int argc, char* argv[])
{
    int coid; //Connection ID to server
    Mymassege_t msg;
    int incoming_checksum; //space for server's reply
    int status; //status return value used for ConnectAttach and MsgSend
    int server_pid; //server's process ID
    int server_chid; //server's channel ID

    if (3 > argc)
    {
        printf("ERROR: This program must be started with commandline arguments, for example:\n\n");
        printf("   client 482834 1 abcdefghi    \n\n");
        printf(" 1st arg(482834): server's pid\n");
        printf(" 2nd arg(1): server's chid\n");
        printf(" 3rd arg(abcdefghi): string to send to server\n"); //to make it 
        //easy, let's not bother handling spaces
        exit(EXIT_FAILURE);
    }

    server_pid = atoi(argv[1]);
    server_chid = atoi(argv[2]);

    printf("attempting to establish connection with server pid: %d, chid %d\n", server_pid,
            server_chid);

    //PUT CODE HERE to establish a connection to the server's channel, store the
    coid = ConnectAttach(ND_LOCAL_NODE,server_pid,server_chid,_NTO_SIDE_CHANNEL,0);
    //connection id in the variable 'coid'

    if (-1 == coid)
    { //was there an error attaching to server?
        perror("ConnectAttach"); //look up error code and print
        exit(EXIT_FAILURE);
    }
    if (3 == argc)
    {
        //send pulse
        msg.pulse.code = 3;
        msg.pulse.value.sival_int = 0;
        int ret = MsgSendPulse(coid,-1,msg.pulse.code,msg.pulse.value.sival_int);
        if(ret<0)
        {
            printf("pulse send error\n");
        }
        printf("send pulse valse %d code %d\n",msg.pulse.value.sival_int,msg.pulse.code);
        return 0;
    }
    msg.Mymsg.msg_type = CKSUM_MSG_TYPE;
    strcpy(msg.Mymsg.string_to_cksum, argv[3]);
    printf("Sending string: %s\n", msg.Mymsg.string_to_cksum);

    //PUT CODE HERE to send message to server and get the reply
    status = MsgSend(coid, &msg,sizeof(msg),&incoming_checksum,sizeof(incoming_checksum));
    if (-1 == status)
    { //was there an error sending to server?
        perror("MsgSend");
        exit(EXIT_FAILURE);
    }

    printf("received checksum=%d from server\n", incoming_checksum);
    printf("MsgSend return status: %d\n", status);

    return EXIT_SUCCESS;
}
Romanof123 commented 4 years ago

pulse server client header

#ifndef _MSG_DEF_H_
#define _MSG_DEF_H_

#include <sys/iomsg.h>

#define MAX_STRING_LEN    256
#define CKSUM_MSG_TYPE (_IO_MAX + 1)

//layout of msgs should always defined by a struct, and ID'd by a msg type
// number as the first member
typedef struct
{
    uint16_t msg_type;
    char string_to_cksum[MAX_STRING_LEN + 1]; //ptr to string we're sending to server, to make it
} cksum_msg_t; //easy, server assumes a max of 256 chars!!!!

typedef union
{
    struct _pulse pulse;
    cksum_msg_t Mymsg;
}Mymassege_t;
// checksum reply is an int

// If you are sharing a target with other people, please customize these server names
// so as not to conflict with the other person.

#define SERVER_NAME     "cksum_server"  

#define DISCONNECT_SERVER "disconnect_server"

#define UNBLOCK_SERVER "unblock_server"

#endif //_MSG_DEF_H_