|
|
|
@ -1,73 +1,76 @@
@@ -1,73 +1,76 @@
|
|
|
|
|
#!/usr/bin/python |
|
|
|
|
|
|
|
|
|
import socket,sys,os |
|
|
|
|
import socket |
|
|
|
|
import sys |
|
|
|
|
import os |
|
|
|
|
from thread import * |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class web4rail_server: |
|
|
|
|
system = None |
|
|
|
|
|
|
|
|
|
def __init__(self,port): |
|
|
|
|
def __init__(self, port): |
|
|
|
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
|
|
print 'Socket created.' |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
self.socket.bind(('',port)) |
|
|
|
|
self.socket.bind(('', port)) |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
print 'Bound to socket at port '+str(port) |
|
|
|
|
print('Bound to socket at port ' + str(port)) |
|
|
|
|
|
|
|
|
|
def client(self,conn): |
|
|
|
|
if self.system == None: |
|
|
|
|
def client(self, conn): |
|
|
|
|
if self.system is None: |
|
|
|
|
self.select_system(conn) |
|
|
|
|
conn.close |
|
|
|
|
|
|
|
|
|
def create_system(self,path): |
|
|
|
|
print 'creating new system at '+path |
|
|
|
|
def create_system(self, path): |
|
|
|
|
print('creating new system at ' + path) |
|
|
|
|
os.mkdir(path, 0755) |
|
|
|
|
file = open(path+'/plan.json', 'w+') |
|
|
|
|
|
|
|
|
|
def load_system(self,path,conn): |
|
|
|
|
print 'loading system from '+path |
|
|
|
|
with open(path+'/plan.json', 'r') as plan_file: |
|
|
|
|
plan=plan_file.read() |
|
|
|
|
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") |
|
|
|
|
conn.send(plan + "\n") |
|
|
|
|
|
|
|
|
|
def select_system(self,conn): |
|
|
|
|
path='/' |
|
|
|
|
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n"); |
|
|
|
|
def select_system(self, conn): |
|
|
|
|
path = '/' |
|
|
|
|
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n") |
|
|
|
|
while True: |
|
|
|
|
conn.send('current dir: '+path+"\n") |
|
|
|
|
conn.send('current dir: ' + path + "\n") |
|
|
|
|
contents = sorted(os.listdir(path)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conn.send("select one from\n") |
|
|
|
|
for entry in contents: |
|
|
|
|
conn.send(' '+entry+"\n") |
|
|
|
|
conn.send(' ' + entry + "\n") |
|
|
|
|
conn.send("--\n") |
|
|
|
|
entry = conn.recv(1024).strip() |
|
|
|
|
if entry in contents: |
|
|
|
|
if os.path.isfile(path+entry): |
|
|
|
|
if os.path.isfile(path + entry): |
|
|
|
|
break |
|
|
|
|
path += entry+'/' |
|
|
|
|
path += entry + '/' |
|
|
|
|
else: |
|
|
|
|
conn.send(path+entry+' does not exist. Create (yes/no/abort)?\n') |
|
|
|
|
input = conn.recv(1024).strip() |
|
|
|
|
if input == 'yes': |
|
|
|
|
conn.send(path + entry + ' does not exist. Create (yes/no/abort)?\n') |
|
|
|
|
response = conn.recv(1024).strip() |
|
|
|
|
if response == 'yes': |
|
|
|
|
path += entry |
|
|
|
|
self.create_system(path) |
|
|
|
|
break |
|
|
|
|
if input == 'abort': |
|
|
|
|
if response == 'abort': |
|
|
|
|
path = None |
|
|
|
|
break |
|
|
|
|
path = '/' |
|
|
|
|
|
|
|
|
|
if path == None: |
|
|
|
|
|
|
|
|
|
if path is None: |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
self.load_system(path,conn) |
|
|
|
|
|
|
|
|
|
self.load_system(path, conn) |
|
|
|
|
|
|
|
|
|
def start(self): |
|
|
|
|
self.socket.listen(10) |
|
|
|
@ -75,7 +78,7 @@ class web4rail_server:
@@ -75,7 +78,7 @@ class web4rail_server:
|
|
|
|
|
|
|
|
|
|
while True: |
|
|
|
|
conn, add = self.socket.accept() |
|
|
|
|
start_new_thread(self.client,(conn,)) |
|
|
|
|
start_new_thread(self.client, (conn,)) |
|
|
|
|
|
|
|
|
|
if len(sys.argv) == 2: |
|
|
|
|
port = int(sys.argv[1]) |
|
|
|
|