Skip to content

Commit

Permalink
v0.2
Browse files Browse the repository at this point in the history
Added:
    - Start command to main loop
    - Show log and show errorlog commands
    - exit command to exit CLI
    - stop command to stop server without exiting CLI
Fixed:
    - Various bugs
  • Loading branch information
Tapawingo authored Feb 5, 2020
1 parent 1804027 commit ed9996b
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions ServerExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import os

# TAK connection defaults
HostAddress = "172.21.1.166"
HostAddress = "10.0.0.115"
HostPort = 8087


# Define function to validate IP
def validateIP(s):
if ":" in s:
Expand All @@ -28,17 +29,11 @@ def validateIP(s):
def clearScreen():
os.system('cls' if os.name=='nt' else 'clear')

# Prompt for server start
while True:
print('Type "start" to start the server')
Inp = input("@TAKServer# ").upper()
if Inp == "START":
TAKSock = TAK(HostAddress, HostPort)
print("Server starting...")
time.sleep(2)
break
# Create isRunning variable
isRunning = False

# Main loop
print('To start server type "start"')
while True:
# Grab user input and convert it to uppercase
Inp = input("@TAKServer# ").upper()
Expand All @@ -47,10 +42,22 @@ def clearScreen():
# display help information
if Inp[0] == "HELP":
print('Available commands:')
print('show - shows specified information')
print('send - Send data to clients')
print('set - Sets specified variable')
print('shutdown - Stops server')
print('Start - starts the server')
print('Show - shows specified information')
print('Send - Send data to clients')
print('Set - Sets specified variable')
print('Shutdown - initiate shutdown')
print('Stop - Cleanly stop server')
print('Exit - Exit this Command Line Interface')

elif Inp[0] == "START":
if not isRunning:
isRunning = True
TAKSock = TAK(HostAddress, HostPort)
print("Server starting...")
time.sleep(2)
else:
print("Server already running")

# Handle show command
elif Inp[0] == "SHOW":
Expand All @@ -64,6 +71,14 @@ def clearScreen():
elif Inp[1] == "CLIENTS":
print(str(TAKSock.getClients()))

# Return errorlog
elif Inp[1] == "ERRORLOG":
print(TAKSock.getErrorLog())

# Return log
elif Inp[1] == "LOG":
print(TAKSock.getLog())

# Return fileno of socket
elif Inp[1] == "FILENO":
if len(Inp) >= 3:
Expand All @@ -78,7 +93,7 @@ def clearScreen():
else:
print("invalid parameter:", Inp[1])
else:
print("Show requires more parameters:\nthreads - Shows currently running threads\nclients - Shows currently connected clients\nFileno - Shows fileno of specified socket")
print("Show requires more parameters:\nthreads - Shows currently running threads\nclients - Shows currently connected clients\nFileno - Shows fileno of specified socket\nErrorlog - Shows the errorlog\nLog - Shows the log")

# Clear screen
elif Inp[0] == "CLEAR":
Expand All @@ -91,7 +106,7 @@ def clearScreen():
# Send data to all clients
if Inp[1] == "ALL":
xmlData = input("XML String: ")
Success, e = TAKSock.sendToAll(xmlData, Inp[1])
Success, e = TAKSock.sendToAll(xmlData)
if Success:
print("Sent Data")
else:
Expand Down Expand Up @@ -126,14 +141,35 @@ def clearScreen():
else:
print("set requires more parameters:\ndebuglevel - Sets current debug level")


# Cleanly stop server
elif Inp[0] == "STOP":
if isRunning:
YN = input("Are you sure(y/n)? ")
if YN.upper() == "Y":
TAKSock.close()
isRunning = False
print("Server shutting Down...")
time.sleep(2)
else:
print("Server is already stopped")

# Cleanly shutdown server
elif Inp[0] == "SHUTDOWN":
YN = input("Are you sure(y/n)? ")
if YN.upper() == "Y":
TAKSock.close()
print("Server shutting Down...")
time.sleep(2)
if isRunning:
YN = input("Are you sure(y/n)? ")
if YN.upper() == "Y":
TAKSock.close()
isRunning = False
print("Server shutting Down...")
time.sleep(2)
break
else:
break

elif Inp[0] == "EXIT":
if not isRunning:
break
else:
print("Can't exit while the server is running")
else:
print('invalid input:', Inp[0])

0 comments on commit ed9996b

Please sign in to comment.