Compare commits
2 Commits
b6dca2e255
...
9243978b0e
Author | SHA1 | Date | |
---|---|---|---|
|
9243978b0e | ||
|
1d58d87ef4 |
@ -82,7 +82,7 @@ def sendResponse(connection,data):
|
|||||||
|
|
||||||
senderThreadSleepMin = 0.0333
|
senderThreadSleepMin = 0.0333
|
||||||
senderThreadSleepMax = 1.0
|
senderThreadSleepMax = 1.0
|
||||||
senderThreadSleepIncr = 0.05
|
senderThreadSleepIncr = 0.01
|
||||||
|
|
||||||
class senderThread(threading.Thread):
|
class senderThread(threading.Thread):
|
||||||
def __init__(self,connectionThread):
|
def __init__(self,connectionThread):
|
||||||
@ -110,7 +110,7 @@ class senderThread(threading.Thread):
|
|||||||
with self.lock:
|
with self.lock:
|
||||||
sleepTime = self.sleep
|
sleepTime = self.sleep
|
||||||
|
|
||||||
print(sleepTime)
|
#print(sleepTime)
|
||||||
time.sleep(sleepTime)
|
time.sleep(sleepTime)
|
||||||
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
@ -153,9 +153,12 @@ class connectionThread(threading.Thread):
|
|||||||
print("thread closed: " +str(self.threadId)+ " (open: " +str(len(threads))+ ")")
|
print("thread closed: " +str(self.threadId)+ " (open: " +str(len(threads))+ ")")
|
||||||
self.closed = True
|
self.closed = True
|
||||||
|
|
||||||
def sendResponse(self,data):
|
def sendResponse(self,data,lock = True):
|
||||||
|
if lock == True:
|
||||||
with self.lock:
|
with self.lock:
|
||||||
self.senderThread.addToQueue([sendResponse,[self.connection,data],{}])
|
self.senderThread.addToQueue([sendResponse,[self.connection,data],{}])
|
||||||
|
else:
|
||||||
|
self.senderThread.addToQueue([sendResponse,[self.connection,data],{}])
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
|
29
modules/[text server]/[api]/communication/module.py
Normal file
29
modules/[text server]/[api]/communication/module.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
moduleDepends([
|
||||||
|
p("[text server]","[api]","commands"),
|
||||||
|
])
|
||||||
|
|
||||||
|
global textSend
|
||||||
|
def textSend(self,command,args):
|
||||||
|
if len(args) < 2:
|
||||||
|
return ["error","nonfatal","syntax","Correct syntax: " +command+ ",<user>,<command>,[argument 1],[argument 2],..."]
|
||||||
|
|
||||||
|
user = args[0].lower()
|
||||||
|
if len(user) < 1:
|
||||||
|
return ["error","nonfatal","name_too_short","Needs to be at least 1 character in length."]
|
||||||
|
|
||||||
|
me = ""
|
||||||
|
with self.lock:
|
||||||
|
me = self.user
|
||||||
|
|
||||||
|
if not me:
|
||||||
|
return ["error","nonfatal","not_logged_in"]
|
||||||
|
|
||||||
|
with threadsLock:
|
||||||
|
for threadId in threads:
|
||||||
|
thread = threads[threadId]
|
||||||
|
with thread.lock:
|
||||||
|
if thread.user != user: continue
|
||||||
|
thread.sendResponse(textListToCommand(["send",me] + args[1:]).encode("utf-8"),lock = False)
|
||||||
|
|
||||||
|
return ["ok"]
|
||||||
|
textCommandAddHandler("send",textSend)
|
@ -33,14 +33,6 @@ def textOnRequest(event,self,requestLength):
|
|||||||
|
|
||||||
text = data.decode("utf-8")
|
text = data.decode("utf-8")
|
||||||
print(":".join(map(str,self.address))+ " > " +text)
|
print(":".join(map(str,self.address))+ " > " +text)
|
||||||
if text == "close":
|
|
||||||
with threadsLock:
|
|
||||||
global close
|
|
||||||
close = True
|
|
||||||
|
|
||||||
self.closeThread()
|
|
||||||
return True
|
|
||||||
|
|
||||||
response = textCommandRun(self,textCommandToList(text))
|
response = textCommandRun(self,textCommandToList(text))
|
||||||
print("response: " +textListToCommand(response))
|
print("response: " +textListToCommand(response))
|
||||||
self.sendResponse(textListToCommand(response).encode("utf-8"))
|
self.sendResponse(textListToCommand(response).encode("utf-8"))
|
||||||
|
@ -19,6 +19,17 @@ sp = pUp(s)
|
|||||||
|
|
||||||
# script start
|
# script start
|
||||||
import socket
|
import socket
|
||||||
|
import threading
|
||||||
|
|
||||||
|
class receiverThread(threading.Thread):
|
||||||
|
def __init__(self,connection):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.connection = connection
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
response = getResponse(connection).decode("utf-8")
|
||||||
|
print("server: " +response)
|
||||||
|
|
||||||
def sendRequest(connection,data):
|
def sendRequest(connection,data):
|
||||||
connection.sendall(len(data).to_bytes(4,"big") + data)
|
connection.sendall(len(data).to_bytes(4,"big") + data)
|
||||||
@ -39,23 +50,19 @@ def main():
|
|||||||
global connection
|
global connection
|
||||||
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
connection.connect(("127.0.0.1",21779))
|
connection.connect(("127.0.0.1",21779))
|
||||||
|
thread = receiverThread(connection)
|
||||||
|
thread.start()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
text = input("data: ")
|
text = input()
|
||||||
data = text.encode("utf-8")
|
data = text.encode("utf-8")
|
||||||
|
|
||||||
|
connection.settimeout(15)
|
||||||
sendRequest(connection,data)
|
sendRequest(connection,data)
|
||||||
response = getResponse(connection).decode("utf-8")
|
connection.settimeout(None)
|
||||||
print("server: " +response)
|
|
||||||
|
|
||||||
if text == "exit":
|
if text == "exit":
|
||||||
connection.close()
|
connection.close()
|
||||||
break
|
break
|
||||||
|
|
||||||
if text == "close":
|
|
||||||
connection.close()
|
|
||||||
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
connection.connect(("127.0.0.1",21779))
|
|
||||||
connection.close()
|
|
||||||
break
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user