<commit_before>#!/usr/bin/python
#This the library for python

import socket
import json
import struct
import time
import types
import threading

#Eventbus constructor
#	input parameters
#		1) host	- String
#		2) port	- integer(>2^10-1)
#		3) TimeOut - float- receive TimeOut
#		
#	inside parameters
#		1) socket
#		2) handlers - List<String address,array<functions/handler>> 
#		3) state -integer
#		4) ReplyHandler - <address,function>
#		5) socket_is_using - boolean {1: sendFrame, 0: receiving
#		
#Eventbus state
#	0 - not connected/failed
#	1 - connecting
#	2 - connected /open
#	3 - closing
#	4 - closed

class Eventbus:
	('TCP eventbus client for python')
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	Handlers = {}
	state = 0
	ReplyHandler={}
	socket_is_using=True
	safe_close=True
	
	#constructor
	def __init__(self, host='localhost', port=7000, TimeOut=3.0,TimeInterval=0.1):
		self.host = host
		self.port = port
		if TimeOut <0.1:
			self.TimeOut = 0.1
		else:
			self.TimeOut = TimeOut
		
		#connect
		try:
			self.state = 1
			self.sock.connect((self.host, self.port))
			self.sock.settimeout(self.TimeOut)
			t1 = threading.Thread(target = self.receivingThread )
			t1.start()
			self.state = 2 
		except IOError as e:
			print( str(e))
		except Exception as e:
			print( str(e))
			
#Connection send and receive--------------------------------------------------------------------

	def isConnected(self):
		if self.state is 2:
			return True
		return False
	
	
	def sendFrame(self, message_s):
		message=message_s.encode('utf-8');
		frame = struct.pack('!I', len(message))+message
		self.sock.sendall(frame)
		
	def receive(self):
		try:
			if self.state <3:#closing socket
				len_str = Eventbus.sock.recv(4)
			else :
				return False
			len1 = struct.unpack("!i", len_str)[0]
			if self.state <3:#closing socket
				payload = Eventbus.sock.recv(len1)
			else: 
				return False
			json_message=payload.decode('utf-8');
			message=json.loads(json_message)
			#check
			#print(message)
			if message['type']== 'message':	
				#handlers
				try:
					if self.Handlers[message['address']] != None:
						for handler in self.Handlers[message['address']]:
							handler(self,message)
					
				except KeyError:	<commit_msg>change reply handler behaviour<commit_after>#!/usr/bin/python
#This the library for python

import socket
import json
import struct
import time
import types
import threading

#Eventbus constructor
#	input parameters
#		1) host	- String
#		2) port	- integer(>2^10-1)
#		3) TimeOut - float- receive TimeOut
#		
#	inside parameters
#		1) socket
#		2) handlers - List<String address,array<functions/handler>> 
#		3) state -integer
#		4) ReplyHandler - <address,function>
#		5) socket_is_using - boolean {1: sendFrame, 0: receiving
#		
#Eventbus state
#	0 - not connected/failed
#	1 - connecting
#	2 - connected /open
#	3 - closing
#	4 - closed

class Eventbus:
	('TCP eventbus client for python')
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	Handlers = {}
	state = 0
	ReplyHandler={}
	socket_is_using=True
	safe_close=True
	
	#constructor
	def __init__(self, host='localhost', port=7000, TimeOut=3.0,TimeInterval=0.1):
		self.host = host
		self.port = port
		if TimeOut <0.1:
			self.TimeOut = 0.1
		else:
			self.TimeOut = TimeOut
		
		#connect
		try:
			self.state = 1
			self.sock.connect((self.host, self.port))
			self.sock.settimeout(self.TimeOut)
			t1 = threading.Thread(target = self.receivingThread )
			t1.start()
			self.state = 2 
		except IOError as e:
			print( str(e))
		except Exception as e:
			print( str(e))
			
#Connection send and receive--------------------------------------------------------------------

	def isConnected(self):
		if self.state is 2:
			return True
		return False
	
	
	def sendFrame(self, message_s):
		message=message_s.encode('utf-8');
		frame = struct.pack('!I', len(message))+message
		self.sock.sendall(frame)
		
	def receive(self):
		try:
			if self.state <3:#closing socket
				len_str = Eventbus.sock.recv(4)
			else :
				return False
			len1 = struct.unpack("!i", len_str)[0]
			if self.state <3:#closing socket
				payload = Eventbus.sock.recv(len1)
			else: 
				return False
			json_message=payload.decode('utf-8');
			message=json.loads(json_message)
			#check
			#print(message)
			if message['type']== 'message':	
				#handlers
				try:
					if self.Handlers[message['address']] != None:
						for handler in self.Handlers[message['address']]:
							handler(self,message)
							
					#reply handlers		
					if self.ReplyHandler['address']== message['address']:
							self.ReplyHandler['replyHandler'](self,None,message)
							del self.ReplyHandler['address']
							del self.ReplyHandler['replyHandler']
					
				except KeyError:	
{'fraction_non_alphanumeric': 0.15061001262095078, 'fraction_numerical': 0.018931426167437947, 'mean_word_length': 3.039932030586236, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '1994155', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1608, 'n_words': 397}