-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdCommands.py
90 lines (71 loc) · 2.63 KB
/
cmdCommands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import socket
import network_base
import _thread
import random
import config
def cmd_help():
print("============================")
print("help - show this text")
print("quit - stop discard'ing")
print("host - start server")
print("connect - connect to server")
print("ipv6 - use ipv6 mode")
print("ipv4 - use ipv4 mode")
print("============================")
def cmd_quit():
quit(0)
def cmd_host():
NET_INTERFACE = network_base.NetInter()
NET_INTERFACE.hostMode = True
NET_INTERFACE.updateGreetings()
print("============================")
print("Welcome to server setup wizard!")
port = int( input("*Enter desired port: ") )
if config.usingIpv6:
adr = socket.getaddrinfo(socket.gethostname(), port, socket.AF_INET6)[0][4][0]
else:
adr = socket.gethostbyname(socket.gethostname())
NET_INTERFACE.makeSocket(adr, port)
NET_INTERFACE.choose_name()
print("*Now hosting at "+NET_INTERFACE.address+":"+str(NET_INTERFACE.port))
_thread.start_new_thread( NET_INTERFACE.serverTick, () )
return NET_INTERFACE
def cmd_ipv6():
config.usingIpv6 = True
print("Now using IPv6!")
def cmd_ipv4():
config.usingIpv6 = False
print("Now using IPv4!")
def cmd_connect():
NET_INTERFACE = network_base.NetInter()
print("============================")
print("Welcome to server setup wizard!")
if config.usingIpv6:
NET_INTERFACE.makeSocket(socket.getaddrinfo(socket.gethostname(), 8080, socket.AF_INET6)[0][4][0], random.randint(1000, 9999) )
else:
NET_INTERFACE.makeSocket( socket.gethostbyname(socket.gethostname()), random.randint(1000, 9999) )
NET_INTERFACE.choose_name()
adr = input("*Enter desired address: ")
if config.usingIpv6:
print(adr[0:(len(adr)-5)],adr[(len(adr)-4):len(adr)])
NET_INTERFACE.connect(adr[0:(len(adr)-5)], int( adr[(len(adr)-4):len(adr)] ) )
else:
NET_INTERFACE.connect(adr.split(":")[0], int(adr.split(":")[1]))
_thread.start_new_thread( NET_INTERFACE.clientTick, () )
return NET_INTERFACE
class CmdCommand:
def __init__(self, __execute, __end_startup):
self.__end_startup = __end_startup
self.__execute = __execute # if startup should end after execution
def should_finish_startup(self):
return self.__end_startup
def execute(self):
return self.__execute()
commands = {
'help' : CmdCommand(cmd_help, False),
'quit' : CmdCommand(cmd_quit, True),
'host' : CmdCommand(cmd_host, True),
'connect' : CmdCommand(cmd_connect, True),
'ipv6' : CmdCommand(cmd_ipv6, False),
'ipv4' : CmdCommand(cmd_ipv4, False)
}