erster Versuch
This commit is contained in:
@@ -3,6 +3,7 @@ import gi
|
|||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
|
|
||||||
|
|
||||||
class ConnectDialog(Gtk.Window):
|
class ConnectDialog(Gtk.Window):
|
||||||
|
|
||||||
host = None
|
host = None
|
||||||
@@ -16,13 +17,13 @@ class ConnectDialog(Gtk.Window):
|
|||||||
self.add(grid)
|
self.add(grid)
|
||||||
|
|
||||||
self.host_label = Gtk.Label('Hostname or IP')
|
self.host_label = Gtk.Label('Hostname or IP')
|
||||||
|
|
||||||
self.host_input = Gtk.Entry();
|
self.host_input = Gtk.Entry()
|
||||||
self.host_input.set_text('localhost')
|
self.host_input.set_text('localhost')
|
||||||
|
|
||||||
self.port_label = Gtk.Label('Port')
|
self.port_label = Gtk.Label('Port')
|
||||||
self.port_input = Gtk.Entry();
|
self.port_input = Gtk.Entry()
|
||||||
self.port_input.set_text('7887')
|
self.port_input.set_text('7668')
|
||||||
|
|
||||||
self.connect_btn = Gtk.Button(label="Connect")
|
self.connect_btn = Gtk.Button(label="Connect")
|
||||||
self.connect_btn.connect("clicked", self.finish)
|
self.connect_btn.connect("clicked", self.finish)
|
||||||
@@ -30,13 +31,12 @@ class ConnectDialog(Gtk.Window):
|
|||||||
self.abort_btn = Gtk.Button(label="Abort")
|
self.abort_btn = Gtk.Button(label="Abort")
|
||||||
self.abort_btn.connect("clicked", self.abort)
|
self.abort_btn.connect("clicked", self.abort)
|
||||||
|
|
||||||
grid.attach(self.host_label,0,0,1,1)
|
grid.attach(self.host_label, 0, 0, 1, 1)
|
||||||
grid.attach(self.host_input,1,0,1,1)
|
grid.attach(self.host_input, 1, 0, 1, 1)
|
||||||
grid.attach(self.port_label,0,1,1,1)
|
grid.attach(self.port_label, 0, 1, 1, 1)
|
||||||
grid.attach(self.port_input,1,1,1,1)
|
grid.attach(self.port_input, 1, 1, 1, 1)
|
||||||
grid.attach(self.abort_btn,0,2,1,1)
|
grid.attach(self.abort_btn, 0, 2, 1, 1)
|
||||||
grid.attach(self.connect_btn,1,2,1,1)
|
grid.attach(self.connect_btn, 1, 2, 1, 1)
|
||||||
|
|
||||||
|
|
||||||
def abort(self, widget):
|
def abort(self, widget):
|
||||||
Gtk.main_quit()
|
Gtk.main_quit()
|
||||||
|
|||||||
@@ -3,32 +3,27 @@ import gi
|
|||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
from TileSelector import *
|
from TileSelector import *
|
||||||
import types
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
def dump_obj(obj, key='',level=0):
|
def dump_obj(obj, key='', level = 0):
|
||||||
for key, value in obj.__dict__.items():
|
for key, value in obj.__dict__.items():
|
||||||
if isinstance(value, (int, float, str, unicode, list, dict, set)):
|
if isinstance(value, (int, float, str, unicode, list, dict, set)):
|
||||||
print " " * level + "%s -> %s" % (key, value)
|
print(" " * level + "%s -> %s" % (key, value))
|
||||||
else:
|
else:
|
||||||
print " " * level + "%s -> %s:" % (key, value.__class__.__name__)
|
print(" " * level + "%s -> %s:" % (key, value.__class__.__name__))
|
||||||
dump_obj(value, key, level + 2)
|
dump_obj(value, key, level + 2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TrackPlan(Gtk.Window):
|
class TrackPlan(Gtk.Window):
|
||||||
def __init__(self,json):
|
def __init__(self,json,server_sock):
|
||||||
Gtk.Window.__init__(self)
|
Gtk.Window.__init__(self)
|
||||||
self.connect("delete-event", Gtk.main_quit)
|
|
||||||
|
|
||||||
self.grid = Gtk.Grid()
|
|
||||||
|
|
||||||
self.putButton(0,0,None,None)
|
|
||||||
|
|
||||||
self.add(self.grid)
|
|
||||||
|
|
||||||
|
|
||||||
|
print(json)
|
||||||
|
self.server_sock = server_sock
|
||||||
|
self.connect("delete-event", Gtk.main_quit)
|
||||||
|
self.grid = Gtk.Grid()
|
||||||
|
self.putButton(0,0,None,None)
|
||||||
|
self.add(self.grid)
|
||||||
|
|
||||||
def select_tile(self,widget,origin,connection):
|
def select_tile(self,widget,origin,connection):
|
||||||
#print 'Button at ({},{}) pressed'.format(widget.x, widget.y)
|
#print 'Button at ({},{}) pressed'.format(widget.x, widget.y)
|
||||||
tileSelector = TileSelector(connection)
|
tileSelector = TileSelector(connection)
|
||||||
@@ -44,7 +39,7 @@ class TrackPlan(Gtk.Window):
|
|||||||
origin.right = tile
|
origin.right = tile
|
||||||
self.putTile(tile,widget.x,widget.y)
|
self.putTile(tile,widget.x,widget.y)
|
||||||
widget.destroy()
|
widget.destroy()
|
||||||
|
|
||||||
def putButton(self,x,y,origin,connection):
|
def putButton(self,x,y,origin,connection):
|
||||||
#print 'putButton({},{})'.format(x,y)
|
#print 'putButton({},{})'.format(x,y)
|
||||||
btn = Gtk.Button('?')
|
btn = Gtk.Button('?')
|
||||||
@@ -52,15 +47,15 @@ class TrackPlan(Gtk.Window):
|
|||||||
btn.y = y
|
btn.y = y
|
||||||
btn.connect('clicked',self.select_tile,origin,connection)
|
btn.connect('clicked',self.select_tile,origin,connection)
|
||||||
self.grid.attach(btn,x,y,1,1)
|
self.grid.attach(btn,x,y,1,1)
|
||||||
btn.show()
|
btn.show()
|
||||||
|
|
||||||
def putTile(self,tile,x,y):
|
def putTile(self,tile,x,y):
|
||||||
#print 'putTile({},{})'.format(x,y)
|
#print 'putTile({},{})'.format(x,y)
|
||||||
tile.x = x
|
tile.x = x
|
||||||
tile.y = y
|
tile.y = y
|
||||||
self.grid.attach(tile,x,y,1,1)
|
self.grid.attach(tile,x,y,1,1)
|
||||||
tile.show()
|
tile.show()
|
||||||
|
|
||||||
if tile.connects_left():
|
if tile.connects_left():
|
||||||
left = self.grid.get_child_at(x-1,y)
|
left = self.grid.get_child_at(x-1,y)
|
||||||
if left == None:
|
if left == None:
|
||||||
@@ -77,14 +72,16 @@ class TrackPlan(Gtk.Window):
|
|||||||
bottom = self.grid.get_child_at(x,y+1)
|
bottom = self.grid.get_child_at(x,y+1)
|
||||||
if bottom == None:
|
if bottom == None:
|
||||||
self.putButton(x,y+1,tile,Tile.TOP)
|
self.putButton(x,y+1,tile,Tile.TOP)
|
||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.show_all()
|
self.show_all()
|
||||||
Gtk.main()
|
Gtk.main()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
seed_tile = self.grid.get_child_at(0,0)
|
seed_tile = self.grid.get_child_at(0,0)
|
||||||
os.system('clear')
|
os.system('clear')
|
||||||
print seed_tile.json()
|
self.server_sock.send('UPDATE PLAN')
|
||||||
|
self.server_sock.send(seed_tile.json())
|
||||||
|
print('Sent plan')
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ def readline(socket):
|
|||||||
if buff:
|
if buff:
|
||||||
yield buff
|
yield buff
|
||||||
|
|
||||||
|
print ('starting')
|
||||||
conn_dlg = ConnectDialog()
|
conn_dlg = ConnectDialog()
|
||||||
conn_dlg.run()
|
conn_dlg.run()
|
||||||
|
|
||||||
@@ -52,8 +53,9 @@ if conn_dlg.port is not None:
|
|||||||
|
|
||||||
if line == 'PLAN:':
|
if line == 'PLAN:':
|
||||||
json = client.next()
|
json = client.next()
|
||||||
plan = TrackPlan(json)
|
plan = TrackPlan(json, client_sock)
|
||||||
plan.run()
|
plan.run()
|
||||||
|
client_sock.send('EXIT')
|
||||||
break
|
break
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
@@ -5,13 +5,14 @@ import sys
|
|||||||
import os
|
import os
|
||||||
from thread import *
|
from thread import *
|
||||||
|
|
||||||
|
|
||||||
class web4rail_server:
|
class web4rail_server:
|
||||||
system = None
|
system = None
|
||||||
|
halt = False
|
||||||
|
|
||||||
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.'
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
print('Socket created.')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.socket.bind(('', port))
|
self.socket.bind(('', port))
|
||||||
@@ -23,24 +24,34 @@ class web4rail_server:
|
|||||||
|
|
||||||
def client(self, conn):
|
def client(self, conn):
|
||||||
if self.system is None:
|
if self.system is None:
|
||||||
self.select_system(conn)
|
self.system = self.select_system(conn)
|
||||||
conn.close
|
self.load_system(conn)
|
||||||
|
while True:
|
||||||
|
response = conn.recv(1024).strip()
|
||||||
|
if response == 'UPDATE PLAN':
|
||||||
|
self.update_plan(conn)
|
||||||
|
elif response == 'EXIT':
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
print(response)
|
||||||
|
os._exit(0)
|
||||||
|
|
||||||
|
# creates a new json file at the selected dir
|
||||||
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, 0o755)
|
||||||
json_file = open(path + '/plan.json', 'w+')
|
json_file = open(path + '/plan.json', 'w+')
|
||||||
json_file.close()
|
json_file.close()
|
||||||
|
|
||||||
def load_system(self, path, conn):
|
def load_system(self, conn):
|
||||||
print('loading system from ' + path)
|
print('loading system from ' + self.system)
|
||||||
with open(path + '/plan.json', 'r') as plan_file:
|
with open(self.system + '/plan.json', 'r') as plan_file:
|
||||||
plan = plan_file.read()
|
plan = plan_file.read()
|
||||||
conn.send("PLAN:\n")
|
conn.send("PLAN:\n")
|
||||||
conn.send(plan + "\n")
|
conn.send(plan + "\n")
|
||||||
|
|
||||||
def select_system(self, conn):
|
def select_system(self, conn):
|
||||||
path = '/'
|
path = '/home/srichter/workspace/Web4Rail/'
|
||||||
conn.send("Welcome to the Web2Rail server. Please select a SYSTEM first:\n")
|
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")
|
||||||
@@ -70,20 +81,27 @@ class web4rail_server:
|
|||||||
if path is None:
|
if path is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.load_system(path, conn)
|
return path
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.socket.listen(10)
|
self.socket.listen(10)
|
||||||
print 'Server started.'
|
print('Server started.')
|
||||||
|
|
||||||
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,))
|
||||||
|
print('Closing server')
|
||||||
|
|
||||||
|
def update_plan(self,conn):
|
||||||
|
json = conn.recv(32768).strip()
|
||||||
|
json_file = open(self.system + '/plan.json', 'w')
|
||||||
|
json_file.write(json)
|
||||||
|
json_file.close()
|
||||||
|
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
port = int(sys.argv[1])
|
port = int(sys.argv[1])
|
||||||
else:
|
else:
|
||||||
port = 7965
|
port = 7668
|
||||||
|
|
||||||
server = web4rail_server(port)
|
server = web4rail_server(port)
|
||||||
server.start()
|
server.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user