<commit_before>package pl.edu.agh.amber.location;

import com.google.protobuf.ExtensionRegistry;

import pl.edu.agh.amber.common.AmberClient;
import pl.edu.agh.amber.common.AmberProxy;
import pl.edu.agh.amber.common.CyclicDataListener;
import pl.edu.agh.amber.common.proto.CommonProto.DriverMsg;
import pl.edu.agh.amber.common.FutureObject;
import pl.edu.agh.amber.common.proto.CommonProto;
import pl.edu.agh.amber.location.proto.LocationProto;
import pl.edu.agh.amber.location.proto.LocationProto.Location;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

public class LocationProxy extends AmberProxy {

	/* Magic-constant */
	private final static int DEVICE_TYPE = 6;

	private Map<Integer, FutureObject> futureObjectsMap = new ConcurrentHashMap<Integer, FutureObject>();
	private CyclicDataListener<String> stringListener;
	private final ReentrantLock listenerLock = new ReentrantLock();

	private int synNum = 100;
	private final ExtensionRegistry extensionRegistry;

	public LocationProxy(AmberClient amberClient, int deviceID) {
		super(DEVICE_TYPE, deviceID, amberClient, Logger
				.getLogger("LocationProxy"));

		logger.info("Starting and registering LocationProxy.");

		extensionRegistry = ExtensionRegistry.newInstance();
		LocationProto.registerAllExtensions(extensionRegistry);
	}

	public void sendLocationCommand(double x, double y, double alfa, double p,
			double timeStamp) throws IOException {
		logger.fine(String.format("Sending LocationCommand: %e %e %e %e %e.",
				x, y, alfa, p, timeStamp));

		DriverMsg.Builder driverMsgBuilder = DriverMsg.newBuilder();
		driverMsgBuilder.setType(DriverMsg.MsgType.DATA);

		Location.Builder commandBuilder = Location.newBuilder();

		commandBuilder.setX(x);
		commandBuilder.setY(y);
		commandBuilder.setAlfa(alfa);
		commandBuilder.setP(p);
		commandBuilder.setTimeStamp(timeStamp);

		Location location = commandBuilder.build();

		driverMsgBuilder.setExtension(LocationProto.currentLocation, location);
		driverMsgBuilder.setSynNum(getNextSynNum());

		amberClient.sendMessage(buildHeader(), driverMsgBuilder.build());
	}

	public LocationCurrent getCurrentLocation() throws IOException {
		logger.fine("Getting current location.");

		int synNum = getNextSynNum();

		DriverMsg currentLocationRequestMsg = buildCurrentLocationRequestMsg(synNum);

		LocationCurrent locationCurrent = new LocationCurrent();
		futureObjectsMap.put(synNum, locationCurrent);

		amberClient.sendMessage(buildHeader(), currentLocationRequestMsg);

		return locationCurrent;
	}

	@Override
	public void handleDataMsg(CommonProto.DriverHdr header,
			CommonProto.DriverMsg message) {
		logger.fine("Handling data message");

		if (!message.hasAckNum() || message.getAckNum() == 0) {
		} else {
			int ackNum = message.getAckNum();

			// TODO: automatically removing abandoned futureObjects<commit_msg>Fix condition in handling data message<commit_after>package pl.edu.agh.amber.location;

import com.google.protobuf.ExtensionRegistry;

import pl.edu.agh.amber.common.AmberClient;
import pl.edu.agh.amber.common.AmberProxy;
import pl.edu.agh.amber.common.CyclicDataListener;
import pl.edu.agh.amber.common.proto.CommonProto.DriverMsg;
import pl.edu.agh.amber.common.FutureObject;
import pl.edu.agh.amber.common.proto.CommonProto;
import pl.edu.agh.amber.location.proto.LocationProto;
import pl.edu.agh.amber.location.proto.LocationProto.Location;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

public class LocationProxy extends AmberProxy {

	/* Magic-constant */
	private final static int DEVICE_TYPE = 6;

	private Map<Integer, FutureObject> futureObjectsMap = new ConcurrentHashMap<Integer, FutureObject>();
	private CyclicDataListener<String> stringListener;
	private final ReentrantLock listenerLock = new ReentrantLock();

	private int synNum = 100;
	private final ExtensionRegistry extensionRegistry;

	public LocationProxy(AmberClient amberClient, int deviceID) {
		super(DEVICE_TYPE, deviceID, amberClient, Logger
				.getLogger("LocationProxy"));

		logger.info("Starting and registering LocationProxy.");

		extensionRegistry = ExtensionRegistry.newInstance();
		LocationProto.registerAllExtensions(extensionRegistry);
	}

	public void sendLocationCommand(double x, double y, double alfa, double p,
			double timeStamp) throws IOException {
		logger.fine(String.format("Sending LocationCommand: %e %e %e %e %e.",
				x, y, alfa, p, timeStamp));

		DriverMsg.Builder driverMsgBuilder = DriverMsg.newBuilder();
		driverMsgBuilder.setType(DriverMsg.MsgType.DATA);

		Location.Builder commandBuilder = Location.newBuilder();

		commandBuilder.setX(x);
		commandBuilder.setY(y);
		commandBuilder.setAlfa(alfa);
		commandBuilder.setP(p);
		commandBuilder.setTimeStamp(timeStamp);

		Location location = commandBuilder.build();

		driverMsgBuilder.setExtension(LocationProto.currentLocation, location);
		driverMsgBuilder.setSynNum(getNextSynNum());

		amberClient.sendMessage(buildHeader(), driverMsgBuilder.build());
	}

	public LocationCurrent getCurrentLocation() throws IOException {
		logger.fine("Getting current location.");

		int synNum = getNextSynNum();

		DriverMsg currentLocationRequestMsg = buildCurrentLocationRequestMsg(synNum);

		LocationCurrent locationCurrent = new LocationCurrent();
		futureObjectsMap.put(synNum, locationCurrent);

		amberClient.sendMessage(buildHeader(), currentLocationRequestMsg);

		return locationCurrent;
	}

	@Override
	public void handleDataMsg(CommonProto.DriverHdr header,
			CommonProto.DriverMsg message) {
		logger.fine("Handling data message");

		if (message.hasAckNum() && message.getAckNum() != 0) {
		} else {
			int ackNum = message.getAckNum();

			// TODO: automatically removing abandoned futureObjects
{'fraction_non_alphanumeric': 0.10414577064526914, 'fraction_numerical': 0.001671681711802073, 'mean_word_length': 5.973193473193473, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7490435', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1681, 'n_words': 356}