updated to comply with python 3
This commit is contained in:
@@ -4,66 +4,67 @@ from ConnectDialog import *
|
|||||||
from FileDialog import *
|
from FileDialog import *
|
||||||
from TrackPlan import *
|
from TrackPlan import *
|
||||||
from YesNoDialog import *
|
from YesNoDialog import *
|
||||||
|
|
||||||
|
|
||||||
def readline(socket):
|
def readline(socket):
|
||||||
buffer = socket.recv(4096)
|
buff = socket.recv(4096)
|
||||||
buffering = True
|
buffering = True
|
||||||
while buffering:
|
while buffering:
|
||||||
if "\n" in buffer:
|
if "\n" in buff:
|
||||||
(line, buffer) = buffer.split("\n", 1)
|
(line, buff) = buff.split("\n", 1)
|
||||||
yield line
|
yield line
|
||||||
else:
|
else:
|
||||||
more = socket.recv(4096)
|
more = socket.recv(4096)
|
||||||
if not more:
|
if not more:
|
||||||
buffering = False
|
buffering = False
|
||||||
else:
|
else:
|
||||||
buffer += more
|
buff += more
|
||||||
if buffer:
|
if buff:
|
||||||
yield buffer
|
yield buff
|
||||||
|
|
||||||
conn_dlg = ConnectDialog()
|
conn_dlg = ConnectDialog()
|
||||||
conn_dlg.run()
|
conn_dlg.run()
|
||||||
|
|
||||||
if conn_dlg.port != None:
|
if conn_dlg.port is not None:
|
||||||
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
client_sock.connect((conn_dlg.host,conn_dlg.port))
|
client_sock.connect((conn_dlg.host, conn_dlg.port))
|
||||||
client = readline(client_sock)
|
client = readline(client_sock)
|
||||||
current_dir = ''
|
current_dir = ''
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line = client.next()
|
line = client.next()
|
||||||
|
|
||||||
if line.startswith('current dir'):
|
if line.startswith('current dir'):
|
||||||
current_dir = line
|
current_dir = line
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line.startswith('select one'):
|
if line.startswith('select one'):
|
||||||
file_dialog = FileDialog(line,current_dir)
|
file_dialog = FileDialog(line, current_dir)
|
||||||
while True:
|
while True:
|
||||||
line = client.next()
|
line = client.next()
|
||||||
if line.startswith('--'):
|
if line.startswith('--'):
|
||||||
break
|
break
|
||||||
file_dialog.add_dir(line.strip())
|
file_dialog.add_dir(line.strip())
|
||||||
file_dialog.run()
|
file_dialog.run()
|
||||||
|
|
||||||
client_sock.send(file_dialog.dir+"\n")
|
client_sock.send(file_dialog.dir + "\n")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if line == 'PLAN:':
|
if line == 'PLAN:':
|
||||||
json = client.next()
|
json = client.next()
|
||||||
plan = TrackPlan(json)
|
plan = TrackPlan(json)
|
||||||
plan.run()
|
plan.run()
|
||||||
break
|
break
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if 'does not exist. Create' in line:
|
if 'does not exist. Create' in line:
|
||||||
dir = line.split('does not exist')[0].strip()
|
directory = line.split('does not exist')[0].strip()
|
||||||
dialog = YesNoDialog(line)
|
dialog = YesNoDialog(line)
|
||||||
if dialog.answer:
|
if dialog.answer:
|
||||||
client_sock.send("yes\n")
|
client_sock.send("yes\n")
|
||||||
else:
|
else:
|
||||||
client_sock.send("no\n")
|
client_sock.send("no\n")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print line
|
print(line)
|
||||||
print "Test"
|
print "Test"
|
||||||
@@ -1,73 +1,76 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import socket,sys,os
|
import socket
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
from thread import *
|
from thread import *
|
||||||
|
|
||||||
|
|
||||||
class web4rail_server:
|
class web4rail_server:
|
||||||
system = None
|
system = None
|
||||||
|
|
||||||
def __init__(self,port):
|
def __init__(self, port):
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
print 'Socket created.'
|
print 'Socket created.'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.socket.bind(('',port))
|
self.socket.bind(('', port))
|
||||||
except socket.error as msg:
|
except socket.error as msg:
|
||||||
print 'Bind failed. Error ('+str(msg[0])+'): '+str(msg[1])
|
print('Bind failed. Error (' + str(msg[0]) + '): ' + str(msg[1]))
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
print 'Bound to socket at port '+str(port)
|
print('Bound to socket at port ' + str(port))
|
||||||
|
|
||||||
def client(self,conn):
|
def client(self, conn):
|
||||||
if self.system == None:
|
if self.system is None:
|
||||||
self.select_system(conn)
|
self.select_system(conn)
|
||||||
conn.close
|
conn.close
|
||||||
|
|
||||||
def create_system(self,path):
|
def create_system(self, path):
|
||||||
print 'creating new system at '+path
|
print('creating new system at ' + path)
|
||||||
os.mkdir(path, 0755)
|
os.mkdir(path, 0755)
|
||||||
file = open(path+'/plan.json', 'w+')
|
json_file = open(path + '/plan.json', 'w+')
|
||||||
|
json_file.close()
|
||||||
def load_system(self,path,conn):
|
|
||||||
print 'loading system from '+path
|
|
||||||
with open(path+'/plan.json', 'r') as plan_file:
|
|
||||||
plan=plan_file.read()
|
|
||||||
conn.send("PLAN:\n")
|
|
||||||
conn.send(plan+"\n")
|
|
||||||
|
|
||||||
def select_system(self,conn):
|
def load_system(self, path, conn):
|
||||||
path='/'
|
print('loading system from ' + path)
|
||||||
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n");
|
with open(path + '/plan.json', 'r') as plan_file:
|
||||||
|
plan = plan_file.read()
|
||||||
|
conn.send("PLAN:\n")
|
||||||
|
conn.send(plan + "\n")
|
||||||
|
|
||||||
|
def select_system(self, conn):
|
||||||
|
path = '/'
|
||||||
|
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n")
|
||||||
while True:
|
while True:
|
||||||
conn.send('current dir: '+path+"\n")
|
conn.send('current dir: ' + path + "\n")
|
||||||
contents = sorted(os.listdir(path))
|
contents = sorted(os.listdir(path))
|
||||||
|
|
||||||
conn.send("select one from\n")
|
conn.send("select one from\n")
|
||||||
for entry in contents:
|
for entry in contents:
|
||||||
conn.send(' '+entry+"\n")
|
conn.send(' ' + entry + "\n")
|
||||||
conn.send("--\n")
|
conn.send("--\n")
|
||||||
entry = conn.recv(1024).strip()
|
entry = conn.recv(1024).strip()
|
||||||
if entry in contents:
|
if entry in contents:
|
||||||
if os.path.isfile(path+entry):
|
if os.path.isfile(path + entry):
|
||||||
break
|
break
|
||||||
path += entry+'/'
|
path += entry + '/'
|
||||||
else:
|
else:
|
||||||
conn.send(path+entry+' does not exist. Create (yes/no/abort)?\n')
|
conn.send(path + entry + ' does not exist. Create (yes/no/abort)?\n')
|
||||||
input = conn.recv(1024).strip()
|
response = conn.recv(1024).strip()
|
||||||
if input == 'yes':
|
if response == 'yes':
|
||||||
path += entry
|
path += entry
|
||||||
self.create_system(path)
|
self.create_system(path)
|
||||||
break
|
break
|
||||||
if input == 'abort':
|
if response == 'abort':
|
||||||
path = None
|
path = None
|
||||||
break
|
break
|
||||||
path = '/'
|
path = '/'
|
||||||
|
|
||||||
if path == None:
|
|
||||||
return
|
|
||||||
|
|
||||||
self.load_system(path,conn)
|
|
||||||
|
|
||||||
|
if path is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.load_system(path, conn)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.socket.listen(10)
|
self.socket.listen(10)
|
||||||
@@ -75,7 +78,7 @@ class web4rail_server:
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
conn, add = self.socket.accept()
|
conn, add = self.socket.accept()
|
||||||
start_new_thread(self.client,(conn,))
|
start_new_thread(self.client, (conn,))
|
||||||
|
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
port = int(sys.argv[1])
|
port = int(sys.argv[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user