# =============================================================================
# Makefile for CMU 15-213 Lecture 21 (concurrent programming)
# =============================================================================
#
#   make run      the three concurrent servers under the same load test, the
#                 iterative server for contrast, and the sharing bug
#   make clean
#
# Links rio.o from LECTURE 18 and netutil.o from LECTURE 20, plus the iterative
# echoserver from Lecture 20 for the baseline comparison. All servers bind the
# loopback interface only and exit after a fixed number of clients.
# -----------------------------------------------------------------------------

CC     ?= cc
CFLAGS := -std=c11 -O2 -Wall -Wextra -I../lecture18 -I../lecture20
LDLIBS := -lpthread

NCLIENTS ?= 4
SLOW_MS  ?= 600

.PHONY: all run run-servers run-sharing clean
all: build/01_process_server build/02_select_server build/03_thread_server \
     build/04_unintended_sharing build/multiclient build/iterative_server

build:
	mkdir -p build

build/rio.o: ../lecture18/rio.c ../lecture18/rio.h | build
	$(CC) $(CFLAGS) -c -o $@ ../lecture18/rio.c

build/netutil.o: ../lecture20/netutil.c ../lecture20/netutil.h | build
	$(CC) $(CFLAGS) -c -o $@ ../lecture20/netutil.c

OBJS := build/netutil.o build/rio.o

build/01_process_server: 01_process_server.c $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ 01_process_server.c $(OBJS)
build/02_select_server: 02_select_server.c $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ 02_select_server.c $(OBJS)
build/03_thread_server: 03_thread_server.c $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ 03_thread_server.c $(OBJS) $(LDLIBS)
build/04_unintended_sharing: 04_unintended_sharing.c | build
	$(CC) $(CFLAGS) -o $@ 04_unintended_sharing.c $(LDLIBS)
build/multiclient: multiclient.c $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ multiclient.c $(OBJS)

# Lecture 20's ITERATIVE server, rebuilt here as the baseline to beat.
build/iterative_server: ../lecture20/echoserver.c $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ ../lecture20/echoserver.c $(OBJS)

# Each block: start the server, run the load test against it, wait for it to exit.
run-servers: all
	@echo "===== baseline: the ITERATIVE server of Lecture 20 (port 15220) ====="
	@./build/iterative_server 15220 $(NCLIENTS) > build/iterative.log 2>&1 & \
	 sleep 0.2; ./build/multiclient localhost 15220 $(NCLIENTS) $(SLOW_MS); wait
	@echo
	@echo "===== 1. PROCESS-based: fork per client (port 15221) ====="
	@./build/01_process_server 15221 $(NCLIENTS) > build/process.log 2>&1 & \
	 sleep 0.2; ./build/multiclient localhost 15221 $(NCLIENTS) $(SLOW_MS); wait; \
	 echo "  ----- server transcript -----"; sed 's/^/  /' build/process.log
	@echo
	@echo "===== 2. EVENT-based: one process + select() (port 15222) ====="
	@./build/02_select_server 15222 $(NCLIENTS) > build/select.log 2>&1 & \
	 sleep 0.2; ./build/multiclient localhost 15222 $(NCLIENTS) $(SLOW_MS); wait; \
	 echo "  ----- server transcript -----"; sed 's/^/  /' build/select.log
	@echo
	@echo "===== 3. THREAD-based: pthread per client (port 15223) ====="
	@./build/03_thread_server 15223 $(NCLIENTS) > build/thread.log 2>&1 & \
	 sleep 0.2; ./build/multiclient localhost 15223 $(NCLIENTS) $(SLOW_MS); wait; \
	 echo "  ----- server transcript -----"; sed 's/^/  /' build/thread.log

run-sharing: build/04_unintended_sharing
	@echo "===== 4. unintended sharing: passing &i to every thread ====="
	@./build/04_unintended_sharing

run: run-servers
	@echo
	@$(MAKE) --no-print-directory run-sharing

clean:
	rm -rf build __pycache__
