<commit_before>package com.systemjx.ems;

import static com.systemjx.ems.SharedResource.logException;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import com.systemj.ipc.GenericSignalSender;

public class OutputSignal extends GenericSignalSender {
	private static final int PACKET_SIZE = 5;
	private String ip;
	private int port;
	private int actuatorId;
	private int groupId;
	private int nodeId;
	private boolean fsent = true;
	private int preVal = Integer.MAX_VALUE;
	private static Map<Thread, Map<String, Socket>> socketMap = new HashMap<>();
	private String urn;

	@SuppressWarnings({ "rawtypes" })
	@Override
	public void configure(Hashtable t) throws RuntimeException {
		this.ip = (String) t.get("IP");
		this.port = Integer.parseInt((String) t.get("Port"));
		this.actuatorId = Integer.parseInt((String) t.get("Actuator"), 16);
		this.groupId = Integer.parseInt((String) t.get("Group"), 16);
		this.nodeId = Integer.parseInt((String) t.get("Node"), 16);
		this.urn = ip + ":" + port;
		socketMap.putIfAbsent(Thread.currentThread(), new HashMap<>());
	}
	
	public Map<String, Socket> getSockets(){
		return socketMap.get(Thread.currentThread());
	}
	
	protected byte[] buildPacket(byte v) {
		ByteBuffer b = ByteBuffer.allocate(PACKET_SIZE + 3);
		b.putShort((short)0xAABB);
		b.put((byte)PACKET_SIZE);
		b.putShort((short)(groupId << 8 | nodeId));
		b.put((byte)0xA0); // Packet type -- fixed
		b.put((byte)actuatorId);
		b.put(v);
		b.position(0);
		byte[] bb = new byte[PACKET_SIZE + 3];
		b.get(bb);
		return bb;
	}

	@Override
	public boolean setup(Object[] b) {
		Socket s = getSockets().get(urn);
		if (s == null || !s.isConnected() || s.isClosed()) {
			s = new Socket();
			try {
				s.connect(new InetSocketAddress(ip, port), 50);
				s.setSoTimeout(50);
				getSockets().put(urn, s);
			} catch (SocketTimeoutException e) {
				return false;
			} catch (IOException e){
				logException(e);
				return false;
			}
			super.buffer = b;
		}
		return true;
	}

	private void sendPacket(byte[] b) {
		Socket s = getSockets().get(urn);
		try {
			DataOutputStream dos = new DataOutputStream(s.getOutputStream());
			dos.write(b);
		} catch (IOException e) {
			try {
				s.close();
				getSockets().remove(urn);
			} catch (IOException e1) {
				logException(e1);
			}
		}
	}
	
	@Override
	public void run() {
		int val;
		try {
			val = (int) super.buffer[1];
		} catch (NullPointerException e) {
			val = 1;
		}
		if(fsent || val != preVal) {
			byte[] b = buildPacket((byte) val);
			sendPacket(b);
			fsent = false;
			preVal = val;<commit_msg>bugfix: updating buffer inside setup method<commit_after>package com.systemjx.ems;

import static com.systemjx.ems.SharedResource.logException;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import com.systemj.ipc.GenericSignalSender;

public class OutputSignal extends GenericSignalSender {
	private static final int PACKET_SIZE = 5;
	private String ip;
	private int port;
	private int actuatorId;
	private int groupId;
	private int nodeId;
	private boolean fsent = true;
	private int preVal = Integer.MAX_VALUE;
	private static Map<Thread, Map<String, Socket>> socketMap = new HashMap<>();
	private String urn;

	@SuppressWarnings({ "rawtypes" })
	@Override
	public void configure(Hashtable t) throws RuntimeException {
		this.ip = (String) t.get("IP");
		this.port = Integer.parseInt((String) t.get("Port"));
		this.actuatorId = Integer.parseInt((String) t.get("Actuator"), 16);
		this.groupId = Integer.parseInt((String) t.get("Group"), 16);
		this.nodeId = Integer.parseInt((String) t.get("Node"), 16);
		this.urn = ip + ":" + port;
		socketMap.putIfAbsent(Thread.currentThread(), new HashMap<>());
	}
	
	public Map<String, Socket> getSockets(){
		return socketMap.get(Thread.currentThread());
	}
	
	protected byte[] buildPacket(byte v) {
		ByteBuffer b = ByteBuffer.allocate(PACKET_SIZE + 3);
		b.putShort((short)0xAABB);
		b.put((byte)PACKET_SIZE);
		b.putShort((short)(groupId << 8 | nodeId));
		b.put((byte)0xA0); // Packet type -- fixed
		b.put((byte)actuatorId);
		b.put(v);
		b.position(0);
		byte[] bb = new byte[PACKET_SIZE + 3];
		b.get(bb);
		return bb;
	}

	@Override
	public boolean setup(Object[] b) {
		Socket s = getSockets().get(urn);
		if (s == null || !s.isConnected() || s.isClosed()) {
			s = new Socket();
			try {
				s.connect(new InetSocketAddress(ip, port), 50);
				s.setSoTimeout(50);
				getSockets().put(urn, s);
			} catch (SocketTimeoutException e) {
				return false;
			} catch (IOException e){
				logException(e);
				return false;
			}
		}
		super.buffer = b;
		return true;
	}

	private void sendPacket(byte[] b) {
		Socket s = getSockets().get(urn);
		try {
			DataOutputStream dos = new DataOutputStream(s.getOutputStream());
			dos.write(b);
		} catch (IOException e) {
			try {
				s.close();
				getSockets().remove(urn);
			} catch (IOException e1) {
				logException(e1);
			}
		}
	}
	
	@Override
	public void run() {
		int val;
		try {
			val = (int) super.buffer[1];
		} catch (NullPointerException e) {
			val = 1;
		}
		if(fsent || val != preVal) {
			byte[] b = buildPacket((byte) val);
			sendPacket(b);
			fsent = false;
			preVal = val;
{'fraction_non_alphanumeric': 0.14811506164016439, 'fraction_numerical': 0.007861354296944793, 'mean_word_length': 4.020627802690583, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 13, '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': '364442', 'n_tokens_mistral': 2165, 'n_tokens_neox': 1926, 'n_words': 456}