<commit_before>/*
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dbseer.middleware.client;

import com.esotericsoftware.minlog.Log;
import dbseer.middleware.constant.MiddlewareConstants;
import dbseer.middleware.event.MiddlewareClientEvent;
import dbseer.middleware.packet.MiddlewarePacketDecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by <NAME> on 12/1/15.
 *
 * The client for the middleware.
 */
public class MiddlewareClient extends Observable implements Runnable
{
	private static final int MAX_RETRY = 3;

	private String id;
	private String password;
	private String host;
	private int port;
	private int retry;
	private boolean isMonitoring;

	private String logPath;

	private Channel channel = null;
	private ExecutorService requesterExecutor = null;

	private ExecutorService heartbeatSenderExecutor = null;
	private MiddlewareClientHeartbeatSender heartbeatSender = null;

	private MiddlewareClientLogRequester txLogRequester = null;
	private Map<String, MiddlewareClientLogRequester> sysLogRequester = null;

	private ArrayList<String> serverNameList = null;

	public MiddlewareClient(String host, String id, String password, int port, String logPath)
	{
		this.retry = 0;
		this.id = id;
		this.password = password;
		this.host = host;
		this.port = port;
		this.logPath = logPath;
		this.isMonitoring = false;
		this.sysLogRequester = new HashMap<>();
		this.serverNameList = new ArrayList<>();
	}

	public void setLogLevel(int level)
	{
		Log.set(level);
	}

	public void run()
	{
		// debug info
		Log.debug(String.format("host = %s", host));
		Log.debug(String.format("port = %d", port));
		Log.debug(String.format("log path = %s", logPath));

		// client needs to handle incoming messages from the middleware as well.
		EventLoopGroup group = new NioEventLoopGroup(4);

		try
		{
			// set up log files
//			File dbLogFile = new File(dbLogPath);
//			File sysLogFile = new File(sysLogPath);
//			final PrintWriter dbLogWriter = new PrintWriter(new FileWriter(dbLogFile, false));
//			final PrintWriter sysLogWriter = new PrintWriter(new FileWriter(sysLogFile, false));

			final MiddlewareClient client = this;

			Bootstrap b = new Bootstrap();
			b.group(group)
					.channel(NioSocketChannel.class)
					.handler(new ChannelInitializer<SocketChannel>()
					{
						@Override
						protected void initChannel(SocketChannel ch) throws Exception
						{
							ChannelPipeline p = ch.pipeline();
							p.addLast(new IdleStateHandler(10, 0, 0));
							p.addLast(new MiddlewarePacketDecoder(),new MiddlewareClientHandler(client));
						}
					});

			ChannelFuture f = b.connect(host, port).sync();<commit_msg>creates log dir if it doesn't exist.<commit_after>/*
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dbseer.middleware.client;

import com.esotericsoftware.minlog.Log;
import dbseer.middleware.constant.MiddlewareConstants;
import dbseer.middleware.event.MiddlewareClientEvent;
import dbseer.middleware.packet.MiddlewarePacketDecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by <NAME> on 12/1/15.
 *
 * The client for the middleware.
 */
public class MiddlewareClient extends Observable implements Runnable
{
	private static final int MAX_RETRY = 3;

	private String id;
	private String password;
	private String host;
	private int port;
	private int retry;
	private boolean isMonitoring;

	private String logPath;

	private Channel channel = null;
	private ExecutorService requesterExecutor = null;

	private ExecutorService heartbeatSenderExecutor = null;
	private MiddlewareClientHeartbeatSender heartbeatSender = null;

	private MiddlewareClientLogRequester txLogRequester = null;
	private Map<String, MiddlewareClientLogRequester> sysLogRequester = null;

	private ArrayList<String> serverNameList = null;

	public MiddlewareClient(String host, String id, String password, int port, String logPath)
	{
		this.retry = 0;
		this.id = id;
		this.password = password;
		this.host = host;
		this.port = port;
		this.logPath = logPath;
		this.isMonitoring = false;
		this.sysLogRequester = new HashMap<>();
		this.serverNameList = new ArrayList<>();
	}

	public void setLogLevel(int level)
	{
		Log.set(level);
	}

	public void run()
	{
		// debug info
		Log.debug(String.format("host = %s", host));
		Log.debug(String.format("port = %d", port));
		Log.debug(String.format("log path = %s", logPath));

		// client needs to handle incoming messages from the middleware as well.
		EventLoopGroup group = new NioEventLoopGroup(4);

		try
		{
			File logDir = new File(logPath);
			if (!logDir.exists())
			{
				logDir.mkdirs();
			}

			final MiddlewareClient client = this;

			Bootstrap b = new Bootstrap();
			b.group(group)
					.channel(NioSocketChannel.class)
					.handler(new ChannelInitializer<SocketChannel>()
					{
						@Override
						protected void initChannel(SocketChannel ch) throws Exception
						{
							ChannelPipeline p = ch.pipeline();
							p.addLast(new IdleStateHandler(10, 0, 0));
							p.addLast(new MiddlewarePacketDecoder(),new MiddlewareClientHandler(client));
						}
					});

			ChannelFuture f = b.connect(host, port).sync();
{'fraction_non_alphanumeric': 0.09812692200167739, 'fraction_numerical': 0.005591277606933185, 'mean_word_length': 4.2765486725663715, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4223921', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2096, 'n_words': 624}