/*
 * mpl.c
 * 
 * Copyright 2025 osboxes <osboxes@osboxes>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * Programa Lector en cola de mensaje, chat con procesos independientes
 * forma de uso:
 $ ./mpl <tipo cola de mensaje lectura>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

typedef struct msg {
	long mtype;       /* message type, must be > 0 */
	char mtext[128];  /* message data */
} msg;

int main(int argc, char **argv) {
	if ( argc != 2 ) { 
		printf("Forma de Uso:\n./mpel <tipo cola mensaje lectura>\n");
		return 1;
	}
	long tcml = atol(argv[1]); // tipo lectura
	
	int msgid = msgget(0xA,0);
	printf("main(): msgid = %d leo de: %ld\n",msgid,tcml);

	msg m;
	
	// proceso lector
	m.mtype = tcml;
	do {
		printf("<<");
		memset(m.mtext,0,128);
		msgrcv(msgid,&m,128,tcml,0);
		printf("%s\n",m.mtext);
	} while(strncmp(m.mtext,"fin",3) != 0);

	return 0;
}

