<commit_before>#!/usr/bin/env python
import sys, json, ConfigParser
import MySQLdb
from datetime import datetime
from twisted.internet import reactor, stdio
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols import basic
from LHServerKeys import *

debug = True

# Class for getting input from the console
class Echo(basic.LineReceiver):
	from os import linesep as delimiter

	def lineReceived(self, line):
		consoleInput(line)

def consoleInput(line):
	if line == 'stop':
		stopServer()
	elif line == 'clients':
		onlineClients()
	elif line == 'online':
		onlineUsers()
	# elif line == 'broadcast'

	elif line == 'help' or line == '?':
		helpLog()

def stopServer():
	log("Stopping LHServer")
	sys.stdout.write("\r")
	sys.stdout.flush()
	reactor.stop()

def onlineClients():
	log("Online clients: %s" % factory.clients)

def onlineUsers():
	log("Online users: %s" % factory.users)

def broadcastServerChat(chat):
	log(chat)
	chatDict = {kChat:chat, kTimestamp:timestamp() + ' GMT', kChatType:kServerChat}

	for client in factory.clients:
		if client.username in factory.users:
			client.broadcast(chatDict, kChatBroadcast)

def helpLog():
	log("stop	 - Stops the server")
	log("clients	 - Shows current clients")
	log("online	 - Shows online users")
	log("help/?	 - Displays this help")

def log(message):
	if message != "":
		sys.stdout.write("\r")
		sys.stdout.flush()
		logString = "%s [LHEServer] %s" % (timestamp(), message)
		print logString

def timestamp():
	now = datetime.now()
	return now.strftime(TIMESTAMP_FORMAT)

# Class for setting up custom Twisted Factory object
class LHServerFactory(Factory):
	def __init__(self):
		self.clients = []
		self.users = {}
		self.protocol = LHServerProtocol

# Class for setting up custom Twisted Protocol object
class LHServerProtocol(Protocol):
	def __init__(self):
		self.username = ''

	def connectionMade(self):
		self.factory.clients.append(self)
		if debug:
			log("Client connected: %s" % self)

	def connectionLost(self, reason):
		try:
			self.factory.clients.remove(self)
		except :
			pass

		logoutUser(self, self.username)
		if debug:
			log("Client disconnected: %s" % self)

	def dataReceived(self, data):
		clientDict = {}
		loginPass = False
		try:
			jsonDict = json.loads(data)
		except ValueError as e:
			log(e)
			return
		command = jsonDict[kCommand]
		if debug:
			log("Command: " + command)

		if command:
			data = jsonDict[kData]

			if command == kChatBroadcast:
				broadcastChat(self, data, kChatBroadcast)
			elif command == kStartType:
				broadcastChat(self, data, kStartType)
			elif command == kEndType:
				broadcastChat(self, data, kEndType)
			elif command == kLogin:
				usr = str(data[kUsername]).lower()
				pswd = str(data[kPassword])
				loginPass = loginUserFromDatabase(usr, pswd)

				if loginPass:
					self.username = usr

					del loginPass[kPassword]

					try:
						clientDict = self.factory.users[self.username]
					except:
						pass

					self.broadcast(True, kLoginResponse)

					if clientDict == {}:
						clientDict = loginPass
						clientDict[kClientCount] = 1
						broadcastServerChat("%s has joined" % self.username)
					else:
						clientDict[kClientCount] = clientDict[kClientCount] +1

				else:
					self.broadcast(False, kLoginResponse)
			elif command == kLogout:
				logoutUser(self, str(data))

		if clientDict:
			self.factory.users[self.username] = clientDict

		if command == kLogin and loginPass:
			broadcastUserList()

	def broadcast(self, message, command):
		tempDict = {kCommand:command, kData: message}
		jsonString = json.dumps(tempDict)
		if debug:
			log("JSON string: %s" % jsonString)
		self.transport.write(jsonString + '\r\n')

def broadcastUserList():
	users = factory.users

	for client in factory.clients:
		if client.username in factory.users:
			client.broadcast(users, kUserList)

def broadcastChat(client, chatDict, command):
	for client in factory.clients:
		if client.username in factory.users:
			client.broadcast(chatDict, command)

def loginUserFromDatabase(usr, pswd):
	db = MySQLdb.connect(host = "localhost", user = dbUsername, passwd = dbPassword, db = dbName, port = 3306)
	db.query("SELECT * FROM users WHERE username='%s' AND password = '%s'" % (usr, p<PASSWORD>))
	result = db.store_result()
	data = result.fetch_row(0, 1)
	db.close()

	if len(data) == 1:<commit_msg>Move broadcastChat at factory level<commit_after>#!/usr/bin/env python
import sys, json, ConfigParser
import MySQLdb
from datetime import datetime
from twisted.internet import reactor, stdio
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols import basic
from LHServerKeys import *

debug = True

# Class for getting input from the console
class Echo(basic.LineReceiver):
	from os import linesep as delimiter

	def lineReceived(self, line):
		consoleInput(line)

def consoleInput(line):
	if line == 'stop':
		stopServer()
	elif line == 'clients':
		onlineClients()
	elif line == 'online':
		onlineUsers()
	# elif line == 'broadcast'

	elif line == 'help' or line == '?':
		helpLog()

def stopServer():
	log("Stopping LHServer")
	sys.stdout.write("\r")
	sys.stdout.flush()
	reactor.stop()

def onlineClients():
	log("Online clients: %s" % factory.clients)

def onlineUsers():
	log("Online users: %s" % factory.users)

def broadcastServerChat(chat):
	log(chat)
	chatDict = {kChat:chat, kTimestamp:timestamp() + ' GMT', kChatType:kServerChat}

	for client in factory.clients:
		if client.username in factory.users:
			client.broadcast(chatDict, kChatBroadcast)

def helpLog():
	log("stop	 - Stops the server")
	log("clients	 - Shows current clients")
	log("online	 - Shows online users")
	log("help/?	 - Displays this help")

def log(message):
	if message != "":
		sys.stdout.write("\r")
		sys.stdout.flush()
		logString = "%s [LHEServer] %s" % (timestamp(), message)
		print logString

def timestamp():
	now = datetime.now()
	return now.strftime(TIMESTAMP_FORMAT)

# Class for setting up custom Twisted Factory object
class LHServerFactory(Factory):
	def __init__(self):
		self.clients = []
		self.users = {}
		self.protocol = LHServerProtocol
	
	def broadcastChat(self, chatDict, command):
		for client in self.clients:
			if client.username in self.users:
				client.broadcast(chatDict, command)

# Class for setting up custom Twisted Protocol object
class LHServerProtocol(Protocol):
	def __init__(self):
		self.username = ''

	def connectionMade(self):
		self.factory.clients.append(self)
		if debug:
			log("Client connected: %s" % self)

	def connectionLost(self, reason):
		try:
			self.factory.clients.remove(self)
		except :
			pass

		logoutUser(self, self.username)
		if debug:
			log("Client disconnected: %s" % self)

	def dataReceived(self, data):
		clientDict = {}
		loginPass = False
		try:
			jsonDict = json.loads(data)
		except ValueError as e:
			log(e)
			return
		command = jsonDict[kCommand]
		if debug:
			log("Command: " + command)

		if command:
			data = jsonDict[kData]

			if command == kChatBroadcast:
				self.factory.broadcastChat(data, kChatBroadcast)
			elif command == kStartType:
				self.factory.broadcastChat(data, kStartType)
			elif command == kEndType:
				self.broadcastChat(data, kEndType)
			elif command == kLogin:
				usr = str(data[kUsername]).lower()
				pswd = str(data[kPassword])
				loginPass = loginUserFromDatabase(usr, pswd)

				if loginPass:
					self.username = usr

					del loginPass[kPassword]

					try:
						clientDict = self.factory.users[self.username]
					except:
						pass

					self.broadcast(True, kLoginResponse)

					if clientDict == {}:
						clientDict = loginPass
						clientDict[kClientCount] = 1
						broadcastServerChat("%s has joined" % self.username)
					else:
						clientDict[kClientCount] = clientDict[kClientCount] +1

				else:
					self.broadcast(False, kLoginResponse)
			elif command == kLogout:
				logoutUser(self, str(data))

		if clientDict:
			self.factory.users[self.username] = clientDict

		if command == kLogin and loginPass:
			broadcastUserList()

	def broadcast(self, message, command):
		tempDict = {kCommand:command, kData: message}
		jsonString = json.dumps(tempDict)
		if debug:
			log("JSON string: %s" % jsonString)
		self.transport.write(jsonString + '\r\n')

def broadcastUserList():
	users = factory.users

	for client in factory.clients:
		if client.username in factory.users:
			client.broadcast(users, kUserList)

def loginUserFromDatabase(usr, pswd):
	db = MySQLdb.connect(host = "localhost", user = dbUsername, passwd = dbPassword, db = dbName, port = 3306)
	db.query("SELECT * FROM users WHERE username='%s' AND password = '%s'" % (usr, pswd))
	result = db.store_result()
	data = result.fetch_row(0, 1)
	db.close()

	if len(data) == 1:
{'fraction_non_alphanumeric': 0.12037990616775375, 'fraction_numerical': 0.0020597322348094747, 'mean_word_length': 4.105140186915888, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 4, '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': '570306', 'n_tokens_mistral': 3233, 'n_tokens_neox': 2813, 'n_words': 756}