<commit_before>package com.xrtb.bidder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import tools.NameNode;

import com.google.gson.Gson;
import com.xrtb.commands.Echo;
import com.xrtb.common.Campaign;
import com.xrtb.common.Configuration;
import com.xrtb.pojo.BidRequest;
import com.xrtb.pojo.BidResponse;
import com.xrtb.pojo.WinObject;;

/**
 * A JAVA based RTB2.2 server.<br>
 * This is the RTB Bidder's main class. It is a Jetty based http server that
 * encapsulates the Jetty server. The class is Runnable, with the Jetty server
 * joining in the run method. This allows other parts of the bidder to interact
 * with the server, mainly to obtain status information from a command sent via
 * REDIS.
 * <p>
 * Prior to calling the RTBServer the configuration file must be Configuration
 * instance needs to be created - which is a singleton. The RTBServer class (as
 * well as many of the other classes also use configuration data in this
 * singleton.
 * <p>
 * A Jetty based Handler class is used to actually process all of the HTTP
 * requests coming into the bidder system.
 * <p>
 * Once the RTBServer.run method is invoked, the Handler is attached to the
 * Jetty server.
 * 
 * @author <NAME>
 * 
 */
public class RTBServer implements Runnable {
	/** The url of the simulator */
	public static final String SIMULATOR_URL = "/xrtb/simulator/exchange";
	/** The url of where the simulator's resources live */
	public static final String SIMULATOR_ROOT = "web/exchange.html";

	public static final String CAMPAIGN_URL = "/xrtb/simulator/campaign";
	public static final String LOGIN_URL = "/xrtb/simulator/login";
	public static final String ADMIN_URL = "/xrtb/simulator/admin";
	public static final String CAMPAIGN_ROOT = "web/test.html";
	public static final String LOGIN_ROOT = "web/login.html";
	public static final String ADMIN_ROOT = "web/admin.html";

	/** The HTTP code for a bid object */
	public static final int BID_CODE = 200; // http code ok
	/** The HTTP code for a no-bid objeect */
	public static final int NOBID_CODE = 204; // http code no bid

	/** The percentage of bid requests to consider for bidding */
	public static int percentage = 100; // throttle is wide open at 100, closed
										// at 0

	/** Indicates of the server is not accepting bids */
	public static boolean stopped = false; // is the server not accepting bid
											// requests?

	/** Counter for number of bids made */
	public static long bid = 0; // number of bids processed
	/** Counter for number of nobids made */
	public static long nobid = 0; // number of nobids processed
	/** Number of errors in accessing the bidder */
	public static long error = 0;
	/** Number of actual requests */
	public static long handled = 0;
	/** Number of unknown accesses */
	public static long unknown = 0;
	/** The configuration of the bidder */
	public static Configuration config;
	/** The number of win notifications */
	public static long win = 0;
	/** The number of clicks processed */
	public static long clicks = 0;
	/** The number of pixels fired */
	public static long pixels = 0;
	/** xtime counter */
	public static long xtime = 0;
	/** The hearbead pool controller */
	public static MyNameNode node;
	/** double adpsend */
	public static volatile double adspend;
	/** is the server ready to receive data */
	boolean ready;

	static long deltaTime = 0, deltaWin = 0, deltaClick = 0, deltaPixel = 0, deltaNobid = 0, deltaBid = 0;
	static double qps = 0;
	static double avgx = 0;
	
	/** The JETTY server used by the bidder */
	Server server;
	/** The default port of the JETTY server */
	int port = 8080;
	/**
	 * The bidder's main thread for handling the bidder's actibities outside of
	 * the JETTY processing
	 */
	Thread me;

	/** The campaigns that the bidder is using to make bids with */
	CampaignSelector campaigns;

	/** Bid target to exchange class map */
	public static Map<String, BidRequest> exchanges = new HashMap();

	/**
	 * This is the entry point for the RTB server.
	 * 
	 * @param args
	 *            . String[]. Config file name. If not present, uses default and
	 *            port 8080.
	 *            Options [-s shardkey] [-p port]
	 * @throws Exception
	 *             if the Server could not start (network error, error reading
	 *             configuration)
	 */
	public static void main(String[] args) throws Exception {
		 
		String fileName = "Campaigns/payday.json";
		String shard = "";
		Integer port = 8080;
		if (args.length == 0)
			fileName = args[0];
		else {
			int i = 0;
			while(i < args.length) {
				switch (args[i]) {
				case "-p":
					i++;
					port = Integer.parseInt(args[i]);
					break;
				case "-s":
					i++;
					shard = args[i];
					break;
				default:
					fileName = args[i];
					i++;
					break;
				}
			}
		}

		new RTBServer(fileName, shard, port);
	}

	/**
	 * Class instantiator of the RTBServer.
	 * 
	 * @param fileName
	 *            String. The filename of the configuration file.
	 * @throws Exception
	 *             if the Server could not start (network error, error reading
	 *             configuration)
	 */
	public RTBServer(String fileName) throws Exception {
		
		AddShutdownHook hook = new AddShutdownHook();
		hook.attachShutDownHook();
		 
		Configuration.getInstance(fileName);
		campaigns = CampaignSelector.getInstance(); // used to
		// select
		// campaigns
		// against
		// bid
		// requests
		me = new Thread(this);
		me.start();
		Thread.sleep(500);
	}
	
	/**
	 * Class instantiator of the RTBServer.
	 * 
	 * @param fileName
	 *            String. The filename of the configuration file.
	 * @param shard String. The shard key of this bidder instance.
	 * @param port. int. The port to use for this bidder.
	 * @throws Exception
	 *             if the Server could not start (network error, error reading
	 *             configuration)
	 */
	public RTBServer(String fileName, String shard, int port) throws Exception {
		
		 AddShutdownHook hook = new AddShutdownHook();
		 hook.attachShutDownHook();
		 
		Configuration.getInstance(fileName, shard,port);
		// Controller.getInstance();
		campaigns = CampaignSelector.getInstance(); // used to
		// select
		// campaigns
		// against
		// bid
		// requests
		me = new Thread(this);
		me.start();
		Thread.sleep(500);
	}
	
	/**
	 * Returns whether the server has started.
	 * @return boolean. Returns true if ready to start.
	 */
	public boolean isReady() {
		return ready;
	}
	
	public static void panicStop() {
		try {
			Controller.getInstance().sendLog(1, "panicStop",
					("Bidder is shutting down *** NOW ****"));
			
			Controller.getInstance().sendShutdown();
			node.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void setSummaryStats() {
		if (xtime == 0)
			avgx = 0;
		else
			avgx = (nobid+bid)/(double)xtime;
		
		if (System.currentTimeMillis() - deltaTime < 30000)
			return;
		
		deltaWin = win - deltaWin;
		deltaClick = clicks - deltaClick;
		deltaPixel = pixels - deltaPixel;
		deltaBid = bid - deltaBid;
		deltaNobid = nobid - deltaNobid;
		
		//System.out.println("+++>"+win+", "+clicks+", "+pixels+", "+bid+", "+nobid);
		//System.out.println("--->"+deltaWin+", "+deltaClick+", "+deltaPixel+", "+deltaBid+", "+deltaNobid);
		
		qps = (deltaWin+deltaClick+deltaPixel+deltaBid+deltaNobid);
		long secs = (System.currentTimeMillis() - deltaTime)/1000;
		qps = qps / secs;
		deltaTime = System.currentTimeMillis();
	}
	
	public static String getSummary() {
		setSummaryStats();
		Gson gson = new Gson();
		Map m = new HashMap();
		m.put("stopped",stopped);
		m.put("loglevel",Configuration.getInstance().logLevel);<commit_msg>Add some comments to summary status<commit_after>package com.xrtb.bidder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

import tools.NameNode;

import com.google.gson.Gson;
import com.xrtb.commands.Echo;
import com.xrtb.common.Campaign;
import com.xrtb.common.Configuration;
import com.xrtb.pojo.BidRequest;
import com.xrtb.pojo.BidResponse;
import com.xrtb.pojo.WinObject;;

/**
 * A JAVA based RTB2.2 server.<br>
 * This is the RTB Bidder's main class. It is a Jetty based http server that
 * encapsulates the Jetty server. The class is Runnable, with the Jetty server
 * joining in the run method. This allows other parts of the bidder to interact
 * with the server, mainly to obtain status information from a command sent via
 * REDIS.
 * <p>
 * Prior to calling the RTBServer the configuration file must be Configuration
 * instance needs to be created - which is a singleton. The RTBServer class (as
 * well as many of the other classes also use configuration data in this
 * singleton.
 * <p>
 * A Jetty based Handler class is used to actually process all of the HTTP
 * requests coming into the bidder system.
 * <p>
 * Once the RTBServer.run method is invoked, the Handler is attached to the
 * Jetty server.
 * 
 * @author <NAME>
 * 
 */
public class RTBServer implements Runnable {
	/** The url of the simulator */
	public static final String SIMULATOR_URL = "/xrtb/simulator/exchange";
	/** The url of where the simulator's resources live */
	public static final String SIMULATOR_ROOT = "web/exchange.html";

	public static final String CAMPAIGN_URL = "/xrtb/simulator/campaign";
	public static final String LOGIN_URL = "/xrtb/simulator/login";
	public static final String ADMIN_URL = "/xrtb/simulator/admin";
	public static final String CAMPAIGN_ROOT = "web/test.html";
	public static final String LOGIN_ROOT = "web/login.html";
	public static final String ADMIN_ROOT = "web/admin.html";

	/** The HTTP code for a bid object */
	public static final int BID_CODE = 200; // http code ok
	/** The HTTP code for a no-bid objeect */
	public static final int NOBID_CODE = 204; // http code no bid

	/** The percentage of bid requests to consider for bidding */
	public static int percentage = 100; // throttle is wide open at 100, closed
										// at 0

	/** Indicates of the server is not accepting bids */
	public static boolean stopped = false; // is the server not accepting bid
											// requests?

	/** Counter for number of bids made */
	public static long bid = 0; // number of bids processed
	/** Counter for number of nobids made */
	public static long nobid = 0; // number of nobids processed
	/** Number of errors in accessing the bidder */
	public static long error = 0;
	/** Number of actual requests */
	public static long handled = 0;
	/** Number of unknown accesses */
	public static long unknown = 0;
	/** The configuration of the bidder */
	public static Configuration config;
	/** The number of win notifications */
	public static long win = 0;
	/** The number of clicks processed */
	public static long clicks = 0;
	/** The number of pixels fired */
	public static long pixels = 0;
	/** xtime counter */
	public static long xtime = 0;
	/** The hearbead pool controller */
	public static MyNameNode node;
	/** double adpsend */
	public static volatile double adspend;
	/** is the server ready to receive data */
	boolean ready;

	static long deltaTime = 0, deltaWin = 0, deltaClick = 0, deltaPixel = 0, deltaNobid = 0, deltaBid = 0;
	static double qps = 0;
	static double avgx = 0;
	
	/** The JETTY server used by the bidder */
	Server server;
	/** The default port of the JETTY server */
	int port = 8080;
	/**
	 * The bidder's main thread for handling the bidder's actibities outside of
	 * the JETTY processing
	 */
	Thread me;

	/** The campaigns that the bidder is using to make bids with */
	CampaignSelector campaigns;

	/** Bid target to exchange class map */
	public static Map<String, BidRequest> exchanges = new HashMap();

	/**
	 * This is the entry point for the RTB server.
	 * 
	 * @param args
	 *            . String[]. Config file name. If not present, uses default and
	 *            port 8080.
	 *            Options [-s shardkey] [-p port]
	 * @throws Exception
	 *             if the Server could not start (network error, error reading
	 *             configuration)
	 */
	public static void main(String[] args) throws Exception {
		 
		String fileName = "Campaigns/payday.json";
		String shard = "";
		Integer port = 8080;
		if (args.length == 0)
			fileName = args[0];
		else {
			int i = 0;
			while(i < args.length) {
				switch (args[i]) {
				case "-p":
					i++;
					port = Integer.parseInt(args[i]);
					break;
				case "-s":
					i++;
					shard = args[i];
					break;
				default:
					fileName = args[i];
					i++;
					break;
				}
			}
		}

		new RTBServer(fileName, shard, port);
	}

	/**
	 * Class instantiator of the RTBServer.
	 * 
	 * @param fileName
	 *            String. The filename of the configuration file.
	 * @throws Exception
	 *             if the Server could not start (network error, error reading
	 *             configuration)
	 */
	public RTBServer(String fileName) throws Exception {
		
		AddShutdownHook hook = new AddShutdownHook();
		hook.attachShutDownHook();
		 
		Configuration.getInstance(fileName);
		campaigns = CampaignSelector.getInstance(); // used to
		// select
		// campaigns
		// against
		// bid
		// requests
		me = new Thread(this);
		me.start();
		Thread.sleep(500);
	}
	
	/**
	 * Class instantiator of the RTBServer.
	 * 
	 * @param fileName
	 *            String. The filename of the configuration file.
	 * @param shard String. The shard key of this bidder instance.
	 * @param port. int. The port to use for this bidder.
	 * @throws Exception
	 *             if the Server could not start (network error, error reading
	 *             configuration)
	 */
	public RTBServer(String fileName, String shard, int port) throws Exception {
		
		 AddShutdownHook hook = new AddShutdownHook();
		 hook.attachShutDownHook();
		 
		Configuration.getInstance(fileName, shard,port);
		// Controller.getInstance();
		campaigns = CampaignSelector.getInstance(); // used to
		// select
		// campaigns
		// against
		// bid
		// requests
		me = new Thread(this);
		me.start();
		Thread.sleep(500);
	}
	
	/**
	 * Returns whether the server has started.
	 * @return boolean. Returns true if ready to start.
	 */
	public boolean isReady() {
		return ready;
	}
	
	public static void panicStop() {
		try {
			Controller.getInstance().sendLog(1, "panicStop",
					("Bidder is shutting down *** NOW ****"));
			
			Controller.getInstance().sendShutdown();
			node.stop();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Set summary stats.
	 */
	public static void setSummaryStats() {
		if (xtime == 0)
			avgx = 0;
		else
			avgx = (nobid+bid)/(double)xtime;
		
		if (System.currentTimeMillis() - deltaTime < 30000)
			return;
		
		deltaWin = win - deltaWin;
		deltaClick = clicks - deltaClick;
		deltaPixel = pixels - deltaPixel;
		deltaBid = bid - deltaBid;
		deltaNobid = nobid - deltaNobid;
		
		//System.out.println("+++>"+win+", "+clicks+", "+pixels+", "+bid+", "+nobid);
		//System.out.println("--->"+deltaWin+", "+deltaClick+", "+deltaPixel+", "+deltaBid+", "+deltaNobid);
		
		qps = (deltaWin+deltaClick+deltaPixel+deltaBid+deltaNobid);
		long secs = (System.currentTimeMillis() - deltaTime)/1000;
		qps = qps / secs;
		deltaTime = System.currentTimeMillis();
	}
	
	/**
	 * Retrieve a summary of activity.
	 * @return String. JSON based stats of server performance.
	 */
	public static String getSummary() {
		setSummaryStats();
		Gson gson = new Gson();
		Map m = new HashMap();
		m.put("stopped",stopped);
		m.put("loglevel",Configuration.getInstance().logLevel);
{'fraction_non_alphanumeric': 0.11026105238568008, 'fraction_numerical': 0.007592127547742802, 'mean_word_length': 3.22085284693123, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '650969', 'n_tokens_mistral': 5711, 'n_tokens_neox': 5039, 'n_words': 1786}