<commit_before>package com.brunocapezzali;

import com.brunocapezzali.Command.CommandReceiver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Rapresents the life-cycle of a mobile device connected to this daemon.
 * Every time a device try to connect to the daemon and send a valid 
 * <i>welcome json</i> the daemon create an instance of {@code Device}.
 * So we have a {@code Device} instance for every connected (and still alive) 
 * device. This class have a running thread that continuously check the 
 * following:
 * <li>Verifiy a <i>KeepAlive</i> mechanism for trust that the persistent connection
 * is still alive. If a device doesn't send a {@code KeepAlive} every definied
 * interval we will set it as a zombie connection.</li>
 * <li>Verify for new commands to send to the device.</li>
 * <li>Verify for new command's reply.</li>
 * 
 * @author <NAME>
 * @see NewDeviceThread
 * @see ScriptThread
 * @since 1.0.0
 */
public class Device extends Thread {
   private static final String TAG = "Device";

   private MainServer mServer;

   private String mIdentifier;
   private final Socket mSock;
   private final BufferedReader mReader;
   private final PrintWriter mWriter;
   
   /* Statistics & informations (useful for futures plug-ins) */
   private String mNetworkType;
   private String mModel;
   private long mTimestampConnected;
   private long mTimestampLastKeepAlive;
   private String mLastCommand;
   
   private final HashMap<String, Command> mCommands;
   private boolean mStopThread;
   private long mKeepAliveInterval;
   private int mCommandTimeoutCount;

   public Device(MainServer server, Socket s) throws IOException {
      mServer = server;
      
      mSock = s;
      mSock.setKeepAlive(true);
      mSock.setSoTimeout(Config.kDaemonSockTimeout); /* Non blocking socket*/
      mReader = new BufferedReader(
              new InputStreamReader(s.getInputStream(), "UTF-8"));
      mWriter = new PrintWriter(new BufferedWriter(
              new OutputStreamWriter(s.getOutputStream(), "UTF-8")), true);
      
      mTimestampConnected = System.currentTimeMillis();
      mTimestampLastKeepAlive = mTimestampConnected;
      
      mStopThread = false;<commit_msg>- Added method stopServer()<commit_after>package com.brunocapezzali;

import com.brunocapezzali.Command.CommandReceiver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Rapresents the life-cycle of a mobile device connected to this daemon.
 * Every time a device try to connect to the daemon and send a valid 
 * <i>welcome json</i> the daemon create an instance of {@code Device}.
 * So we have a {@code Device} instance for every connected (and still alive) 
 * device. This class have a running thread that continuously check the 
 * following:
 * <li>Verifiy a <i>KeepAlive</i> mechanism for trust that the persistent connection
 * is still alive. If a device doesn't send a {@code KeepAlive} every definied
 * interval we will set it as a zombie connection.</li>
 * <li>Verify for new commands to send to the device.</li>
 * <li>Verify for new command's reply.</li>
 * 
 * @author <NAME>
 * @see NewDeviceThread
 * @see ScriptThread
 * @since 1.0.0
 */
public class Device extends Thread {
   private static final String TAG = "Device";

   private final MainServer mServer;

   private String mIdentifier;
   private final Socket mSock;
   private final BufferedReader mReader;
   private final PrintWriter mWriter;
   
   /* Statistics & informations (useful for futures plug-ins) */
   private String mNetworkType;
   private String mModel;
   private final long mTimestampConnected;
   private long mTimestampLastKeepAlive;
   private String mLastCommand;
   
   private final HashMap<String, Command> mCommands;
   private boolean mStopThread;
   private long mKeepAliveInterval;
   private int mCommandTimeoutCount;

   public Device(MainServer server, Socket s) throws IOException {
      mServer = server;
      
      mSock = s;
      mSock.setKeepAlive(true);
      mSock.setSoTimeout(Config.kDaemonSockTimeout); /* Non blocking socket*/
      mReader = new BufferedReader(
              new InputStreamReader(s.getInputStream(), "UTF-8"));
      mWriter = new PrintWriter(new BufferedWriter(
              new OutputStreamWriter(s.getOutputStream(), "UTF-8")), true);
      
      mTimestampConnected = System.currentTimeMillis();
      mTimestampLastKeepAlive = mTimestampConnected;
      
      mStopThread = false;
{'fraction_non_alphanumeric': 0.08299676898222941, 'fraction_numerical': 0.0020193861066235864, 'mean_word_length': 3.813411078717201, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '2969218', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1367, 'n_words': 480}