Exemple #1
0
void server(int readfd,int writefd)
{
	int fd;
	struct mymesg * buf;
	ssize_t n;

	if ((n = mesg_recv(readfd,buf))==0)
	{
		perror("read filepath error");
		return;
	}
	buf[n]='\0';
	printf("server:read path from client\n");
	if ((fd =open(buf,O_RDONLY)) <0)
	{
		snprintf(buf,sizeof(buf),"cant not open %s",strerror(errno));
		n = strlen(buf);
		mesg_send(writefd,buf);
	}
	else
	{
		while((n = read(fd,buf,MAXLINE)) >0)
		{
			mesg_send(writefd,buf);
		}
		close(fd);
	}
}
void client(int readfd, int writefd)
{
	size_t len;
	ssize_t n;
	char buff[MAXMESGDATA];
	sleep(1);

	printf("=== Нажмите enter или введите get_info:\n");
	fgets(buff,MAXMESGDATA,stdin);
	len=strlen(buff);
	if(buff[len-1]=='\n')
	len--;

	msg.mesg_len = len;
	msg.mesg_type = 1;
	sprintf(msg.mesg_data,"%s",buff);
	mesg_send(writefd, &msg);

	n = mesg_recv(readfd, &msg);

	if(!strcmp(msg.mesg_data,GET) && 
	((msg.mesg_type==001) || (msg.mesg_type==101) || (msg.mesg_type==011)))
	{
		printf("=== Сервер запрашивает id клиента 3...\n");
		msg.mesg_len = strlen(ID);
		msg.mesg_type = 1;
		sprintf(msg.mesg_data,"%s",ID);
		mesg_send(writefd, &msg);
	}
	else 
	{
		if(msg.mesg_type==000)
		{
			printf("=== Ни один клиент не запросил данные! Выход...\n");
			if (unlink(FIFO3_in) < 0) printf("Client_3: can not delete FIFO3_in\n");
			else printf("Client_3: FIFO3_in is deleted!\n");
			if (unlink(FIFO3_out) < 0) printf("Client_3: can not delete FIFO3_out\n");
			else printf("Client_3: FIFO3_out is deleted!\n");
			exit(0);
		}
		else if(msg.mesg_type==111)
		{
			printf("=== Все клиенты запросили данные! Выход...\n");
			if (unlink(FIFO3_in) < 0) printf("Client_3: can not delete FIFO3_in\n");
			else printf("Client_3: FIFO3_in is deleted!\n");
			if (unlink(FIFO3_out) < 0) printf("Client_3: can not delete FIFO3_out\n");
			else printf("Client_3: FIFO3_out is deleted!\n");
			exit(0);
		}
		else printf("=== Сервер скоро пришлет информацию из БД...\n");

		msg.mesg_len = 0;
		msg.mesg_type = 0;
		sprintf(msg.mesg_data,"%s","");
		mesg_send(writefd, &msg);
		if((n=mesg_recv(readfd, &msg))>0)
			printf("=== Информация о других клиентах:\n%s\n",msg.mesg_data);
	}
}
	void
server(int readfd, int writefd)
{
	FILE	*fp;
	ssize_t	n;
	struct mymesg	mesg;

	/* 4read pathname from IPC channel */
	mesg.mesg_type = 1;
	if ( (n = mesg_recv(readfd, &mesg)) == 0)
	{
		perror("pathname missing");
		exit(1);
	}
	mesg.mesg_data[n] = '\0';	/* null terminate pathname */

	if ( (fp = fopen(mesg.mesg_data, "r")) == NULL) {
		/* 4error: must tell client */
		snprintf(mesg.mesg_data + n, sizeof(mesg.mesg_data) - n,
				": can't open, %s\n", strerror(errno));
		mesg.mesg_len = strlen(mesg.mesg_data);
		mesg_send(writefd, &mesg);

	} else {
		/* 4fopen succeeded: copy file to IPC channel */
		while (fgets(mesg.mesg_data, MAXMESGDATA, fp) != NULL) {
			mesg.mesg_len = strlen(mesg.mesg_data);
			//mesg_send(writefd, &mesg);

			if ( (n = mesg_send(writefd, &mesg)) != (MESGHDRSIZE + mesg.mesg_len))
			{
				fprintf(stderr, "%s, message data: expect %ld,got %ld,\n", 
						strerror(n), (MESGHDRSIZE + mesg.mesg_len), n);
				exit(1);
			}
		}
		fclose(fp);
	}

	/* 4send a 0-length message to signify the end */
	mesg.mesg_len = 0;
	if ( (n = mesg_send(writefd, &mesg)) != (MESGHDRSIZE + mesg.mesg_len))
	{
		fprintf(stderr, "msg_send error :%s\n", strerror(n));
		exit(1);
	}
	// 也可以通过关闭IPC通道来通知对端已达到输入文件的结尾
	// 不过我们通过发送回一个长度为0的消息来达到同样的目的,因为之后还会遇到没有文件结束符概念的其他类型的IPC
	// 用sleep 可以模拟,如果不只是达到文件结尾 client 的while循环不会返回
	
	//close(writefd);

	//sleep(10);
}
Exemple #4
0
void server(int readfd, int writefd)
{
	FILE *fp;
	char *ptr;
	pid_t pid;
	ssize_t n;
	struct mymesg mesg;

	for ( ; ; )
	{
		//read pathname from IPC channel
		mesg.mesg_type = 1;
		if ((n = mesg_recv(readfd, &mesg)) == 0)
		{
			err_msg("pathname missing");
			continue;
		}
		mesg.mesg_data[n] = '\0';

		if ((ptr = strchr(mesg.mesg_data, ' ')) == NULL)
		{
			err_msg("bogus request: %s", mesg.mesg_data);
			continue;
		}

		*ptr++ = 0; // null terminate PID, ptr = pathname
		pid = atol(mesg.mesg_data);
		mesg.mesg_type = pid; // for message back to client

		if ((fp = fopen(ptr, "r")) == NULL)
		{
			snprintf(mesg.mesg_data + n, sizeof(mesg.mesg_data) - n, ": can not open, %s\n", strerror(errno));
			mesg.mesg_len = strlen(ptr);
			memmove(mesg.mesg_data, ptr, mesg.mesg_len);
			mesg_send(writefd, &mesg);
		}
		else
		{
			// fopen succeeded: copy file to IPC cahnnel
			while (Fgets(mesg.mesg_data, MAXMESGDATA, fp) != NULL)
			{
				mesg.mesg_len = strlen(mesg.mesg_data);
				mesg_send(writefd, &mesg);
			}
			Fclose(fp);
		}

		//send a 0 length message to signify the end
		mesg.mesg_len = 0;
		mesg_send(writefd, &mesg);
	}
}
Exemple #5
0
void Client() {
	int msg_id;
	key_t key;
	msg msgSend, msgRecv;
	key = ftok(".", 'a');
	char *filename =(char *) malloc(MAXBUFSIZE);
	if((msg_id = msgget(key, 0)) <0) {
				perror("msgget failed!");
				exit(2);
	}
	
	fprintf(stdout, "Input the file name:\n");
	fscanf(stdin, "%s", filename);
	strcpy(msgSend.mtext,filename);
	msgSend.mtype = 1;
	if(mesg_send(&msgSend, msg_id) == -1) {
		perror("mesg_send");
		exit(1);
	}
	if(mesg_recv(&msgRecv, 2, msg_id) == -1) {
		perror("mesg_recv");
		exit(1);
	}
	
	fprintf(stdout, "%s", msgRecv.mtext);
}
Exemple #6
0
void client(int readfd, int writefd)
{
	size_t len;
	ssize_t	n;
	struct mymesg mesg;

	/* 4read pathname */
	if (fgets(mesg.mesg_data, MAXMESGDATA, stdin) == NULL && ferror(stdin)) {
		perror("fgets error");
		exit(1);
	}
	len = strlen(mesg.mesg_data);
	if (mesg.mesg_data[len-1] == '\n')
		len--;				/* delete newline from fgets() */
	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	/* 4write pathname to IPC channel */
	mesg_send(writefd, &mesg);

	/* 4read from IPC, write to standard output */
	while ((n = mesg_recv(readfd, &mesg)) > 0) {
		if (write(STDOUT_FILENO, mesg.mesg_data, n) != n) {
			perror("write error");
			exit(1);
		}
	}
}
Exemple #7
0
void client(int readid, int writeid)
{
	size_t len;
	ssize_t n;
	char *ptr;
	struct mymesg mesg;

	//start buffer with msqid and a blank
	snprintf(mesg.mesg_data, MAXMESGDATA, "%d ", readid);
	len = strlen(mesg.mesg_data);
	ptr = mesg.mesg_data + len;

	//read pathname
	Fgets(ptr, MAXMESGDATA - len, stdin);
	len = strlen(mesg.mesg_data);
	if (mesg.mesg_data[len-1] == '\n')
		len--;
	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	//write msqid and pathname to server well-know queue
	mesg_send(writeid, &mesg);

	//read from our queue, wirte to standard output
	while ((n = mesg_recv(readid, &mesg)) > 0)
		Write(STDOUT_FILENO, mesg.mesg_data, n);
}
Exemple #8
0
/*
 * Called after search driver generates a candidate point, but before
 * that point is returned to the client API.
 *
 * This routine should fill the flow variable appropriately and return
 * 0 upon success.  Otherwise, it should call session_error() with a
 * human-readable error message and return -1.
 */
int codegen_generate(hflow_t *flow, htrial_t *trial)
{
    int i;

    i = cglog_find(&trial->point);
    if (i >= 0) {
        if (cglog[i].status == CODEGEN_STATUS_COMPLETE)
            flow->status = HFLOW_ACCEPT;

        if (cglog[i].status == CODEGEN_STATUS_REQUESTED)
            flow->status = HFLOW_WAIT;

        return 0;
    }

    if (cglog_insert(&trial->point) != 0) {
        session_error("Internal error: Could not grow codegen log");
        return -1;
    }

    mesg = HMESG_INITIALIZER;
    mesg.type = HMESG_FETCH;
    mesg.status = HMESG_STATUS_OK;
    mesg.data.fetch.cand = HPOINT_INITIALIZER;
    mesg.data.fetch.best = HPOINT_INITIALIZER;
    hpoint_copy(&mesg.data.fetch.cand, &trial->point);

    if (mesg_send(sockfd, &mesg) < 1) {
        session_error( strerror(errno) );
        return -1;
    }

    flow->status = HFLOW_WAIT;
    return 0;
}
Exemple #9
0
void client(int readfd, int writefd)
{
	size_t len;
	ssize_t n;
	char *ptr;
	struct mymesg mesg;

	//start buffer with pid and a blank
	snprintf(mesg.mesg_data, MAXMESGDATA, "%ld", (long) getpid());
	len = strlen(mesg.mesg_data);
	ptr = mesg.mesg_data + len;

	// read pathname
	Fgets(ptr, MAXMESGDATA - len, stdin);
	len = strlen(mesg.mesg_data);
	if (mesg.mesg_data[len-1] == '\n')
		len--;
	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	// write PID and pathname to IPC channed
	mesg_send(writefd, &mesg);

	// read from IPC, write to standard output
	mesg.mesg_type = getpid();
	while ((n = mesg_recv(readfd, &mesg)) > 0)
		Write(STDOUT_FILENO, mesg.mesg_data, n);
}
Exemple #10
0
void
Mesg_send(int fd, struct mymesg *mptr)
{
	ssize_t	n;

	if ( (n = mesg_send(fd, mptr)) != mptr->mesg_len)
		err_quit("mesg_send error");
}
Exemple #11
0
/*
 * Invoked once on module load.
 *
 * This routine should return 0 on success, and -1 otherwise.
 */
int codegen_init(hsignature_t *sig)
{
    const char *url;
    hcfg_t *cfg;

    url = session_getcfg(CFGKEY_TARGET_URL);
    if (!url || url[0] == '\0') {
        session_error("Destination URL for"
                      " generated code objects not specified");
        return -1;
    }

    url = session_getcfg(CFGKEY_SERVER_URL);
    if (!url || url[0] == '\0') {
        session_error("Codegen server URL not specified");
        return -1;
    }

    sockfd = url_connect(url);
    if (sockfd == -1) {
        session_error("Invalid codegen server URL");
        return -1;
    }

    /* In the future, we should rewrite the code generator system to
     * avoid using hmesg_t types.  Until then, generate a fake
     * HMESG_SESSION message to maintain compatibility.
     */
    mesg = HMESG_INITIALIZER;

    hsignature_copy(&mesg.data.session.sig, sig);
    cfg = hcfg_alloc();
    hcfg_set(cfg, CFGKEY_SERVER_URL, session_getcfg(CFGKEY_SERVER_URL));
    hcfg_set(cfg, CFGKEY_TARGET_URL, session_getcfg(CFGKEY_TARGET_URL));
    hcfg_set(cfg, CFGKEY_REPLY_URL, session_getcfg(CFGKEY_REPLY_URL));
    hcfg_set(cfg, CFGKEY_SLAVE_LIST, session_getcfg(CFGKEY_SLAVE_LIST));
    hcfg_set(cfg, CFGKEY_SLAVE_PATH, session_getcfg(CFGKEY_SLAVE_PATH));
    mesg.data.session.cfg = cfg;

    mesg.type = HMESG_SESSION;
    mesg.status = HMESG_STATUS_REQ;
    if (mesg_send(sockfd, &mesg) < 1)
        return -1;

    if (mesg_recv(sockfd, &mesg) < 1)
        return -1;

    if (callback_generate(sockfd, codegen_callback) != 0) {
        session_error("Could not register callback for codegen plugin");
        return -1;
    }

    return 0;
}
	void
client(int readid, int writeid)
{
	size_t	len;
	ssize_t	n;
	char *ptr;
	struct mymesg	mesg;


	// start buffer with pid and a blank
	snprintf(mesg.mesg_data, MAXMESGDATA, "%d ", readid);
	len = strlen(mesg.mesg_data);
	ptr = mesg.mesg_data + len;
	// A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.
	//
	// A terminating null character is automatically appended after the characters copied to str.
	//char * ret = fgets(buff, MAXLINE, stdin);
	fgets(ptr, MAXMESGDATA - len, stdin);
	len = strlen(mesg.mesg_data);
	if (mesg.mesg_data[len-1] == '\n')
	{
		len--;				/* delete newline from fgets() */
	}
	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	fprintf(stderr, "mesg.mesg_data:%s\n", mesg.mesg_data);
	/* 4write pathname to IPC channel */
	if ( (n = mesg_send(writeid, &mesg)) != (MESGHDRSIZE + len))
	{
		fprintf(stderr, "msg_send error :");
		exit(1);
	}

	/* 4read from IPC, write to standard output */
	fprintf(stderr, "mesg.mesg_type:%ld\n", mesg.mesg_type);

	while ( (n = mesg_recv(readid, &mesg)) > 0)
		write(STDOUT_FILENO, mesg.mesg_data, n);
	fprintf(stderr, "client while finish:\n");
}
Exemple #13
0
void client(int readfd, writefd)
{
	size_t len;
	ssize_t n;
	struct mymesg mesg;

	fgets(mesg.mesg_data, MAXMESGDATA, stdin);
	len = strlen(mesg.mesg_data);

	if (mesg.mesg_data[len-1] == '\n'){
		len--;
	}

	mesg.mesg_len = len;
	mesg.mesg_type = 1;

	mesg_send(writefd, &mesg);

	while((n = mesg_recv(readfd, &mesg)) > 0){
		write(STDOUT_FILENO, mesg.mesg_data, n);
	}
}
/*------------------------------------------------------------------------------------------------------------------ 
-- FUNCTION:	client_mngr
-- 
-- DATE:		January 21, 2015
-- 
-- REVISIONS:	
-- 
-- DESIGNER:	Ruoqi Jia
-- 
-- PROGRAMMER:	Ruoqi Jia
-- 
-- INTERFACE:	void client_mngr(char * filename, int priority)
-- 						char * filename : the name of file that will be sent from the client and opened by the server
--						int priority : The priority level of the connection. has to be 1-10
--
-- RETURNS: void
-- 
-- NOTES: The main starting point of the client process. It first query for a connection with the server, and once 
-- the acknowledge has been done, the client will wrap up the file name and priority level into a message and send 
-- to the server.
--------------------------------------------------------------------------------------------------------------------*/
void client_mngr(char * filename, int priority)
{
	char * c_pids;
	int s_pid, c_pid = getpid();
	struct msg snd_msg;
	struct msg rcv_msg;;

	/* establish connection */
	connect(c_pid, &snd_msg, &rcv_msg, &s_pid);

	printf("Client-[%d] Server-[%d]: Sending file name-[%s], Priority-[%d]\n", c_pid, s_pid, filename, priority);

	/* initialize message for file request */
	init_msg(&snd_msg, s_pid, priority, filename);

	/* send the message */
	mesg_send(&snd_msg);

	/* waits for the file packets and read it */
	get_qfile(&rcv_msg, c_pid, priority);


}
/*------------------------------------------------------------------------------------------------------------------ 
-- FUNCTION:	connect
-- 
-- DATE:		January 21, 2015
-- 
-- REVISIONS:	
-- 
-- DESIGNER:	Ruoqi Jia
-- 
-- PROGRAMMER:	Ruoqi Jia
-- 
-- INTERFACE:	void connect(int c_pid, struct msg * snd_msg, struct msg * rcv_msg, int * s_pid)
-- 						int c_pid : client's process ID
--						struct msg * snd_msg : pointer to the msg structure for sending 
--						struct msg * rcv_msg : pointer to the msg structure for reading
--						int * s_pid : server's process ID
--
-- RETURNS: void
-- 
-- NOTES: For mainly establishing connection with the server. The client will query a message to the queue and waits 
-- for an aknowledgement from the server with its process ID attached. The server process ID will be stored for future 
-- communications.
--------------------------------------------------------------------------------------------------------------------*/
void connect(int c_pid, struct msg * snd_msg, struct msg * rcv_msg, int * s_pid)
{
	char * c_pids;
	/* convert PID into a string */
	rev_atoi(c_pid, &c_pids);
	
	printf("Client-[%d] Server-[null]: Sending PID\n", c_pid);

	/* initialize message with own PID */ 
	init_msg(snd_msg, CLIENT_MSG, 0, c_pids);

	/* send PID to server */
	mesg_send(snd_msg);

	printf("Waiting for server response...\n");
	/* read msg from server */
	mesg_recv(rcv_msg, SERVER_MSG);
	
	/* store the server PID as an int */
	*s_pid = atoi(rcv_msg->data);

	printf("Client-[%d] Server-[%d]: Recieved response!\n", c_pid, s_pid);
}
int send_request(hdesc_t *hdesc, hmesg_type msg_type)
{
    hdesc->mesg.type = msg_type;
    hdesc->mesg.status = HMESG_STATUS_REQ;
    hdesc->mesg.src_id = hdesc->id;

    if (mesg_send(hdesc->socket, &hdesc->mesg) < 1) {
        hdesc->errstr = "Error sending Harmony message to server.";
        errno = ECOMM;
        return -1;
    }

    do {
        if (mesg_recv(hdesc->socket, &hdesc->mesg) < 1) {
            hdesc->errstr = "Error retrieving Harmony message from server.";
            errno = ECOMM;
            return -1;
        }

        /* If the httpinfo layer is enabled during a stand-alone session,
         * it will generate extraneous messages where dest == -1.
         * Make sure to ignore these messages.
         */
    } while (hdesc->mesg.dest == -1);

    if (hdesc->mesg.type != msg_type) {
        hdesc->mesg.status = HMESG_STATUS_FAIL;
        hdesc->errstr = "Internal error: Server response message mismatch.";
        return -1;
    }

    if (hdesc->mesg.status == HMESG_STATUS_FAIL) {
        hdesc->errstr = hdesc->mesg.data.string;
        return -1;
    }
    return 0;
}
Exemple #17
0
void Mesg_send(int fd, struct mymesg *mptr)
{
	if (mesg_send(fd, mptr) < 0)
		err_sys("mesg_send error");
}