<commit_before>package com.github.kubatatami.judonetworking.internals.wear;

import android.content.Context;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.kubatatami.judonetworking.exceptions.ConnectionException;
import com.github.kubatatami.judonetworking.wear.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.wearable.CapabilityApi;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by Kuba on 28/09/15.
 */
public class MessageUtils {

    protected Random random = new Random();

    protected ObjectMapper objectMapper = new ObjectMapper();

    protected Context context;

    protected final static String MSG_PATH = "/jj/requestProxy/";

    static Map<String, Object> waitObjects = new HashMap<>();

    static Map<String, byte[]> resultObjects = new HashMap<>();

    protected GoogleApiClient googleClient;

    protected long connectionTimeout = 10000;

    protected long sendTimeout = 15000;

    protected long readTimeout = 15000;

    public MessageUtils(Context context) {
        this.context = context.getApplicationContext();
        googleClient = new GoogleApiClient.Builder(context.getApplicationContext())
                .addApiIfAvailable(Wearable.API)
                .build();
        googleClient.connect();
    }

    public <T> T sendMessageAndReceive(Object msg, int operationTimeout, Class<T> clazz) throws IOException {
        makeSureIsConnected();
        Set<Node> nodes = Wearable.CapabilityApi.getCapability(
                googleClient, context.getString(R.string.jj_request_proxy),
                CapabilityApi.FILTER_REACHABLE).await().getCapability().getNodes();
        String id = generateUniqId();
        Object waitObject = new Object();
        waitObjects.put(id, waitObject);
        sendMessage(id, pickBestNodeId(nodes), msg);
        try {
            synchronized (waitObject) {
                waitObject.wait(readTimeout + operationTimeout);
            }
        } catch (InterruptedException e) {
            throw new ConnectionException("GoogleApiClient timeout");
        } finally {
            waitObjects.remove(id);
        }
        byte[] message = resultObjects.remove(id);
        if (message != null) {
            return readObject(message, clazz);
        } else {
            throw new ConnectionException("GoogleApiClient timeout");
        }
    }

    public <T> T readObject(byte[] msg, Class<T> clazz) throws IOException {
        return objectMapper.readValue(msg, clazz);
    }

    public void sendMessage(String msgId, String nodeId, Object msg) throws IOException {
        byte[] message = objectMapper.writeValueAsBytes(msg);
        MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient,
                nodeId, MSG_PATH + msgId, message).await(sendTimeout, TimeUnit.MILLISECONDS);
        Status status = result.getStatus();
        if (!status.isSuccess()) {
            throw new ConnectionException("GoogleApiClient failed to send message: "
                    + status.getStatusMessage() + "(" + status.getStatusCode() + ")");
        }
    }

    private String pickBestNodeId(Set<Node> nodes) {
        String bestNodeId = null;
        for (Node node : nodes) {
            if (node.isNearby()) {
                return node.getId();
            }
            bestNodeId = node.getId();
        }
        return bestNodeId;
    }

    protected void makeSureIsConnected() {
        ConnectionResult connectionResult = googleClient.blockingConnect(connectionTimeout, TimeUnit.MILLISECONDS);
        if (!connectionResult.isSuccess()) {
            throw new ConnectionException("GoogleApiClient error" + connectionResult.getErrorCode());
        }
    }

    protected String generateUniqId() {
        return System.currentTimeMillis() + "" + random.nextInt(10000);<commit_msg>bug fix<commit_after>package com.github.kubatatami.judonetworking.internals.wear;

import android.content.Context;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.kubatatami.judonetworking.exceptions.ConnectionException;
import com.github.kubatatami.judonetworking.wear.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.wearable.CapabilityApi;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.Wearable;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by Kuba on 28/09/15.
 */
public class MessageUtils {

    protected Random random = new Random();

    protected ObjectMapper objectMapper = new ObjectMapper();

    protected Context context;

    protected final static String MSG_PATH = "/jj/requestProxy/";

    static Map<String, Object> waitObjects = new HashMap<>();

    static Map<String, byte[]> resultObjects = new HashMap<>();

    protected GoogleApiClient googleClient;

    protected long connectionTimeout = 10000;

    protected long sendTimeout = 15000;

    protected long readTimeout = 15000;

    public MessageUtils(Context context) {
        this.context = context.getApplicationContext();
        googleClient = new GoogleApiClient.Builder(context.getApplicationContext())
                .addApiIfAvailable(Wearable.API)
                .build();
        googleClient.connect();
    }

    public <T> T sendMessageAndReceive(Object msg, int operationTimeout, Class<T> clazz) throws IOException {
        makeSureIsConnected();
        Set<Node> nodes = Wearable.CapabilityApi.getCapability(
                googleClient, context.getString(R.string.jj_request_proxy),
                CapabilityApi.FILTER_REACHABLE).await().getCapability().getNodes();
        String id = generateUniqId();
        Object waitObject = new Object();
        waitObjects.put(id, waitObject);
        sendMessage(id, pickBestNodeId(nodes), msg);
        try {
            synchronized (waitObject) {
                waitObject.wait(readTimeout + operationTimeout);
            }
        } catch (InterruptedException e) {
            throw new ConnectionException("GoogleApiClient timeout");
        } finally {
            waitObjects.remove(id);
        }
        byte[] message = resultObjects.remove(id);
        if (message != null) {
            return readObject(message, clazz);
        } else {
            throw new ConnectionException("GoogleApiClient timeout");
        }
    }

    public <T> T readObject(byte[] msg, Class<T> clazz) throws IOException {
        return objectMapper.readValue(msg, clazz);
    }

    public void sendMessage(String msgId, String nodeId, Object msg) throws IOException {
        byte[] message = new byte[0];
        if(msg != null) {
            message = objectMapper.writeValueAsBytes(msg);
        }
        MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(googleClient,
                nodeId, MSG_PATH + msgId, message).await(sendTimeout, TimeUnit.MILLISECONDS);
        Status status = result.getStatus();
        if (!status.isSuccess()) {
            throw new ConnectionException("GoogleApiClient failed to send message: "
                    + status.getStatusMessage() + "(" + status.getStatusCode() + ")");
        }
    }

    private String pickBestNodeId(Set<Node> nodes) {
        String bestNodeId = null;
        for (Node node : nodes) {
            if (node.isNearby()) {
                return node.getId();
            }
            bestNodeId = node.getId();
        }
        return bestNodeId;
    }

    protected void makeSureIsConnected() {
        ConnectionResult connectionResult = googleClient.blockingConnect(connectionTimeout, TimeUnit.MILLISECONDS);
        if (!connectionResult.isSuccess()) {
            throw new ConnectionException("GoogleApiClient error" + connectionResult.getErrorCode());
        }
    }

    protected String generateUniqId() {
        return System.currentTimeMillis() + "" + random.nextInt(10000);
{'fraction_non_alphanumeric': 0.10251021599532983, 'fraction_numerical': 0.006187974314068885, 'mean_word_length': 2.956581986143187, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '2281621', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2276, 'n_words': 495}