/* 3000cp-rendezvous.c  Simple producer-consumer with shared memory
 * Copyright (C) 2026  Anil Somayaji
 * based on code Copyright (C) 2020  William Findlay
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>. */

/* You really shouldn't be incorporating parts of this in any other code,
   it is meant for teaching, not production */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>

#define QUEUESIZE 32
#define BUFSIZE 4096

typedef struct entry {
        char buf[BUFSIZE];
        ssize_t count;
        sem_t lock;
} entry;

typedef struct shared {
        pthread_mutex_t cond_mutex;
        pthread_cond_t  queue_nonempty;
        pthread_cond_t  queue_nonfull;
        entry queue[QUEUESIZE];
        int last_produced;
        int last_consumed;
        pid_t prod_pid;
        pid_t con_pid;
        int prod_count;
        int con_count;
} shared;


void report_error(char *error)
{
        fprintf(stderr, "Error: %s\n", error);
}

void usage_exit(char *progname)
{
        fprintf(stderr,
                "Usage: %s <source file> <dest file>\n",
                progname);
        exit(-1);
}

void wait_for_producer(shared *s)
{
        if (s->prod_pid != 0) {
                pthread_mutex_lock(&s->cond_mutex);
                pthread_cond_wait(&s->queue_nonempty, &s->cond_mutex);
                pthread_mutex_unlock(&s->cond_mutex);
        }
}

void wait_for_consumer(shared *s)
{
        if (s->con_pid != 0) {
                pthread_mutex_lock(&s->cond_mutex);
                pthread_cond_wait(&s->queue_nonfull, &s->cond_mutex);
                pthread_mutex_unlock(&s->cond_mutex);
        }
}

int queue_buffer(char *buf, ssize_t count, shared *s)
{
        entry *e;
        int current, retval;

        current = (s->last_produced + 1) % QUEUESIZE;

        e = &s->queue[current];

        sem_wait(&e->lock);

        if (e->count != 0) {
                sem_post(&e->lock);
                wait_for_consumer(s);
                sem_wait(&e->lock);
        }

        if (e->count != 0) {
                fprintf(stderr, "ERROR: No room for producer after waiting!\n");
                retval = -1;
                goto done;
        } else {
                memcpy(e->buf, buf, count);
                e->count = count;
                s->last_produced = current;
                s->prod_count++;
                /* Notify that queue is nonempty */
                pthread_cond_signal(&s->queue_nonempty);
                retval = 0;
                goto done;
        }

 done:
        sem_post(&e->lock);
        return retval;

}

ssize_t get_next_buffer(char *buf, shared *s)
{
        entry *e;
        int current;
        ssize_t count;

        current = (s->last_consumed + 1) % QUEUESIZE;

        e = &s->queue[current];

        sem_wait(&e->lock);

        if (e->count == 0) {
                /* producer hasn't filled in this entry yet */
                sem_post(&e->lock);
                wait_for_producer(s);
                sem_wait(&e->lock);
        }

        if (e->count == 0 && s->prod_pid != 0) {
                fprintf(stderr, "ERROR: Nothing for consumer after waiting!\n");
                count = -1;
                goto done;
        } else {
                count = e->count;
                memcpy(buf, e->buf, count);
                e->count = 0;
                s->last_consumed = current;
                s->con_count++;
                pthread_cond_signal(&s->queue_nonfull);
                goto done;
        }

 done:
        sem_post(&e->lock);
        return count;
}

void read_file(char *fn, shared *s)
{
        char buf[BUFSIZE];
        int in_fd;
        ssize_t read_count;
        int found_data = 0;

        in_fd = open(fn, O_RDONLY);

        if (in_fd == -1) {
                report_error("Couldn't open file for reading\n");
                s->prod_pid = 0;
                exit(-1);
        }

        while ((read_count = read(in_fd, buf, BUFSIZE)) > 0) {
                found_data = 1;
                queue_buffer(buf, read_count, s);
        }

        close(in_fd);
        s->prod_pid = 0;
        if (found_data > 0) {
                printf("File successfully read and sent.\n");
        } else {
                printf("No data read from file.\n");                
        }
        exit(0);
}

void write_file(char *fn, shared *s)
{
        char buf[BUFSIZE];
        int out_fd;
        ssize_t read_count, write_count;

        out_fd = creat(fn, 0666);
        
        if (out_fd == -1) {
                report_error("Couldn't open file for writing\n");
                s->con_pid = -1;
                exit(-1);
        }

        read_count = 1;
        while (read_count > 0) {
                read_count = get_next_buffer(buf, s);

                if (read_count == 0) {
                        if (s->prod_pid == 0) {
                                break;
                        }
                } else {
                        write_count = write(out_fd, buf, read_count);

                        if (write_count != read_count) {
                                report_error("Problem writing file to disk.");
                                s->con_pid = 0;
                                exit(-1);
                        }
                }
        }
        
        s->con_pid = 0;
        close(out_fd);
        printf("File successfully received and copied.\n");
        exit(0);
}

void init_shared(shared *s)
{
        int i;

        pthread_mutexattr_t mattr = {};
        pthread_mutexattr_setpshared(&mattr, 1);
        pthread_mutex_init(&s->cond_mutex, &mattr);

        pthread_condattr_t cattr = {};
        pthread_condattr_setpshared(&cattr, 1);
        pthread_cond_init(&s->queue_nonempty, &cattr);
        pthread_cond_init(&s->queue_nonfull, &cattr);

        s->last_consumed = -1;
        s->last_produced = -1;

        s->prod_pid = -1;
        s->con_pid  = -1;

        s->prod_count = 0;
        s->con_count  = 0;

        for (i=0; i<QUEUESIZE; i++) {
                s->queue[i].count = 0;
                sem_init(&s->queue[i].lock, 1, 1);
        }
}

int main(int argc, char *argv[])
{
        int pid;
        shared *s;
        char *source_fn, *dest_fn;

        if (argc < 3) {
                if (argc < 1) {
                        report_error("no command line");
                        usage_exit(argv[0]);
                } else {
                        report_error("Not enough arguments");
                        usage_exit(argv[0]);
                }
        }
        
        source_fn = argv[1];
        dest_fn = argv[2];

        s = (shared *) mmap(NULL, sizeof(shared),
                             PROT_READ|PROT_WRITE,
                             MAP_SHARED|MAP_ANONYMOUS, -1, 0);

        if (s == MAP_FAILED)
        {
                fprintf(stderr, "Error: Unable to mmap: %s\n", strerror(errno));
                exit(-1);
        }

        init_shared(s);

        pid = fork();

        if (pid == 0) {
                s->prod_pid = getpid();
                read_file(source_fn, s);
        } else {
                s->con_pid = getpid();
                write_file(dest_fn, s);
        }

        return -1;
}
