#include "comn.h"

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

		char file_path[20];
		memset(file_path, 0, sizeof(file_path));
		sprintf(file_path, "./json/%s.json", argv[3]);
		if (-1 != access(file_path, 0)) {
			void *context = zmq_ctx_new();
			assert(context != NULL);
			void *requester = zmq_socket(context, ZMQ_REQ);
			assert(requester != NULL);

			char path[20];
			char line[1000];
			FILE *file;

			char msg_buf[1000] = { 0, };
			char recv_buf[1000] = { 0, };

			json_t *jstr;
			json_error_t *error;

			columnStc col_stc = { 0, };
			dataStc data_stc = { 0, };
			columnStc process_list[10] = { 0, };
			columnStc condition_list[10] = { 0, };

			memset(path, 0, sizeof(path));
			sprintf(path, "tcp://%s:%s", argv[1], argv[2]);
			printf("path :: %s\n", path);

			printf("zmq connect\n");
			if (0 != zmq_connect(requester, path)) {
				printf("연결 실패\n");
			}
			printf("zmq send\n");
			strcpy(msg_buf, "message");
			printf("msg_buf :: %s\n", msg_buf);

			file = fopen(file_path, "r");

			int i = 1;
			while (!feof(file)) {
				if (fgets(line, sizeof(line), file) == 0)
					break;

				jstr = json_loads(line, 0, &error);

				json_t *data, *obj, *arr;
				const char *key;
				char *json_str;
				int idx;
				char data_list[1000] = {0, };
				json_object_foreach(jstr, key, obj)
				{
					if (!json_is_array(obj)) {
				json_object_foreach(jstr, key, obj)
						json_str = json_string_value(obj);

						if (0 == strcmp(key, "schema")) {
							data_stc.schema = json_str;
						} else if (0 == strcmp(key, "table")) {
							data_stc.table = json_str;
						} else {
							data_stc.dml_type = json_str;
						}
						//snprintf(data_list, 1000, "%s|%s|%s", data_stc.schema, data_stc.table, data_stc.dml_type);
					}

					json_array_foreach(obj, idx, arr)
					{
						char *list_type;
						int pr_cnt = 0, con_cnt = 0;
						list_type = key;
						char col_list[1000] ={0, };

						json_object_foreach(arr, key, obj)
						{
							if (NULL != json_string_value(obj)) {
								json_str = json_string_value(obj);

								if (0 == strcmp(key, "column_name")) {
									col_stc.col_name = json_str;
								} else if (0 == strcmp(key, "value")) {
									col_stc.col_value = json_str;
								} else if (0 == strcmp(key, "column_type")) {
									col_stc.col_type = json_str;
								} else if (0 == strcmp(key, "column_size")) {
									col_stc.col_size = json_str;
								}
				//			snprintf(col_list, "|{%s,%s,%s,%d}", col_stc.col_name, col_stc.col_value, col_stc.col_type, col_stc.col_size);
							}
						}

						if (0 == strcmp(list_type, "process_column_list")) {
							process_list[pr_cnt] = col_stc;
							pr_cnt++;
						} else if (0 == strcmp(list_type, "condition_list")) {
							condition_list[con_cnt] = col_stc;
							con_cnt++;
						}
					}
					data_stc.process_column_list = process_list;
					data_stc.condition_list = condition_list;
				}

				if (i < 5) {
					zmq_send(requester, msg_buf, sizeof(msg_buf), ZMQ_SNDMORE);
				} else {
					zmq_send(requester, msg_buf, sizeof(msg_buf), 0);
					zmq_recv(requester, recv_buf, sizeof(recv_buf), 0);
					printf("recv_buf :: %s\n", recv_buf);
					i = 0;
				}
				i++;
				json_decref(jstr);
				sleep(1);
			}
			strcpy(msg_buf, "file end");
			zmq_send(requester, msg_buf, sizeof(msg_buf), 0);
			zmq_recv(requester, recv_buf, sizeof(recv_buf), 0);
			printf("end recv_buf :: %s\n", recv_buf);
		} else {
			printf("### not exist file path ###\n");
		}
	} else {
		printf("### no enough arguments  ###\n");
	}

	return 0;
}

