#include "comn.h"

pid_t pid;

void connShMemory();

int main(int argc, char* argv[]) {

	printf("\n[ control process ]\n\n");
	
	connShMemory();

	// 1. status
	if (strcmp(argv[1], "status") == 0) {
//		shmid = shmget((key_t)0301, sizeof(shData), IPC_EXCL | IPC_CREAT | 0666);
//		printf("shmid :: %d\n", shmid);

//		if (shmid == -1) {
//			printf("server not connected\n");
//			exit(1);
//		}

//		if (shData == NULL) {
//			printf("no running process\n");
//			exit(1);
//		}
		
		// declare struct pointer
		shData = (struct SharedData*)shmaddr;

		if(shData == NULL) {
			printf("### shData NULL. no running process ###\n");
			exit(1);
		}

		// server process
		if (shData->pid != 0) {
			printf("### [ %d ] server process running ###\n", shData->pid);

			int i = 0;
			for (i = 0; i < MAX_USER_CNT; i++) {
				if (shData->client[i].pid != 0) {
					printf("### user [ %s ] running ###\n", shData->client[i].name);
				}
			}
		}

		exit(1);
	}

	// 2. server
	if (strcmp(argv[2], "server") == 0) {
		if (strcmp(argv[1], "start") == 0) {
			// check shared memory
//			shmid = shmget((key_t)0301, sizeof(shData), IPC_EXCL);
//			printf("shmid :: %d\n", shmid);
//			if (shmid != -1) {
				// one server running ok
//				printf("server running\n");
//				exit(1);
//			}
			// fork set
			pid = fork();
			if (pid == -1) {
				printf("### fork fail ###\n");
			} else if (pid == 0) {
				// parent : new pid, child : return 0
				execl("./server", "start", (char*)0);
			}
		}

		else if (strcmp(argv[1], "stop") == 0) {
//			shmid = shmget((key_t)0301, sizeof(shData), IPC_EXCL);
//			if (shmid == -1) {
//				printf("no running server\n");
//				exit(1);
//			}

//			if ((shmaddr = shmat(shmid, (void*)0, 0)) == (void*)-1) {
//				printf("shmat fail\n");
//				exit(1);
//			}

			shData = (struct SharedData*)shmaddr;
			if(shmid != -1) {
				printf("### no running server stop start ###\n");
				exit(1);
			}

			kill(shData->pid, SIGINT);
			printf("### running server stop ###\n");
		}
		else {
			printf("### wrong keyword ###\n");
		}
	}

	// 3. client
	if (strcmp(argv[2], "client") == 0) {
		shmid = shmget((key_t)0301, sizeof(shData), IPC_EXCL);
		if (shmid == -1) {
			printf("### shmget fail ###\n");
			exit(1);
		}

		if ((shmaddr = shmat(shmid, (void*)0, 0)) == (void*)-1) {
			printf("### shmat fail ###\n");
			exit(1);
		}

		shData = (struct SharedData*)shmaddr;

		if (strcmp(argv[1], "stop") == 0) {
			int i = 0;
			for (i = 0; i < MAX_USER_CNT; i++) {
				if (strcmp(argv[3], shData->client[i].name) == 0) {
					kill(shData->client[i].pid, SIGINT);
					printf("### user [ %s ] stop by control ###\n", argv[3]);
				}
			}
		}
	}

	return 0;
}


void connShMemory() {
	shmid = shmget((key_t)0301, sizeof(shData), IPC_EXCL | IPC_CREAT | 0666);
	printf("connShMemory shmid :: %d\n", shmid);
	
	if (shmid == -1) {
		// shared memory check -> create, conn
		shmid = shmget((key_t)0301, sizeof(shData), IPC_CREAT | 0666);
		if(shmid == -1) {
	        	printf("### shmget fail ###\n");
			
			// shared memory separate / disconn
			if(shmdt(shmaddr) == -1) {
				printf("### shmdt fail ###\n");
				exit(1);
			}
		} else {
			// shared memory allocate / conn
                	shmaddr = shmat(shmid, (void*)0, 0);
	                if(shmaddr == (void*)-1) {
        	                printf("### shmat fail ###\n");
                	        exit(1);
               		}
        	}
        } else {
		// shared memory delete
		if(shmctl(shmid, IPC_RMID, 0) == -1) {
			printf("### shmctl fail ###\n");
			exit(1);
		}
	}
}
