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