#include "comn.h"

int main(int argc, char* argv[]) {
	void *context = zmq_ctx_new();
        void *requester = zmq_socket(context, ZMQ_REQ);
	
	if(argv[1] != NULL) {
                char path[20];
		char *command[50];
		int i = 0;
                memset(path, 0, sizeof(path));
		memset(command, 0, sizeof(command));
                sprintf(path, "tcp://localhost:%s", argv[1]);

		zmq_connect(requester, path);

		char msg_buf[50];
		memset(msg_buf, 0, sizeof(msg_buf));
		printf("### connect with server ###\n");

		while(1) {
			printf("> ");
		        fgets(msg_buf, sizeof(msg_buf), stdin);
			msg_buf[strlen(msg_buf) - 1] = '\0';
			if(msg_buf != NULL) {
				if(strcmp(msg_buf, "exit") == 0) {
					zmq_close(requester);
        	                        zmq_ctx_destroy(context);
					exit(1);
				} else {
					char buffer[1000];
					char *result;
                	                zmq_send(requester, msg_buf, sizeof(msg_buf),0);
        	                        zmq_recv(requester, buffer, sizeof(buffer), 0);
					int i = 0;
					printf("%s\n", buffer);
					if(strcmp(buffer, "0") == 0) {
						printf("success.\n");
					} else if(strcmp(buffer, "1") == 0) {
                                                printf("fail.\n");
                                        } else {
						result = strtok(buffer, ",");
						while(result != NULL) {
							result = strtok(NULL, ",");
						}
					}
				}
			} else {
				printf("### enter command ###\n");
			}
		}
	}
	return 0;
}
