ursprünglichen Entwicklungsstand als Python-Projekt konserviert
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
|
||||
|
||||
class ConnectDialog(Gtk.Window):
|
||||
|
||||
host = None
|
||||
port = None
|
||||
|
||||
def __init__(self):
|
||||
Gtk.Window.__init__(self, title="Connect to server")
|
||||
self.connect("delete-event", Gtk.main_quit)
|
||||
|
||||
grid = Gtk.Grid()
|
||||
self.add(grid)
|
||||
|
||||
self.host_label = Gtk.Label('Hostname or IP')
|
||||
|
||||
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('7668')
|
||||
|
||||
self.connect_btn = Gtk.Button(label="Connect")
|
||||
self.connect_btn.connect("clicked", self.finish)
|
||||
|
||||
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)
|
||||
|
||||
def abort(self, widget):
|
||||
Gtk.main_quit()
|
||||
|
||||
def run(self):
|
||||
self.show_all()
|
||||
Gtk.main()
|
||||
|
||||
def finish(self, widget):
|
||||
self.host = self.host_input.get_text()
|
||||
self.port = int(self.port_input.get_text())
|
||||
self.hide()
|
||||
Gtk.main_quit()
|
||||
@@ -1,52 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
|
||||
class FileDialog(Gtk.Window):
|
||||
|
||||
def __init__(self,title,hint):
|
||||
Gtk.Window.__init__(self, title=title)
|
||||
self.connect("delete-event", Gtk.main_quit)
|
||||
|
||||
self.hint = Gtk.Label(hint);
|
||||
self.dir_grid = Gtk.Grid()
|
||||
self.dir_input = Gtk.Entry();
|
||||
self.new_btn = Gtk.Button('Create')
|
||||
self.new_btn.connect('clicked',self.new_btn_clck)
|
||||
|
||||
self.y = 0
|
||||
|
||||
|
||||
def add_dir(self,name):
|
||||
btn = Gtk.Button(label=name)
|
||||
btn.connect("clicked",self.select_dir)
|
||||
self.dir_grid.attach(btn,0,self.y,1,1)
|
||||
self.y+=1
|
||||
|
||||
def new_btn_clck(self,widget):
|
||||
self.dir = self.dir_input.get_text()
|
||||
self.hide()
|
||||
Gtk.main_quit()
|
||||
|
||||
def run(self):
|
||||
scroll = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
|
||||
|
||||
|
||||
grid = Gtk.Grid()
|
||||
scroll.add(self.dir_grid)
|
||||
|
||||
grid.attach(self.hint, 0,0,2,1)
|
||||
grid.attach(scroll, 0,1,2,1)
|
||||
grid.attach(self.dir_input, 0,2,1,1)
|
||||
grid.attach(self.new_btn, 1,2,1,1)
|
||||
|
||||
self.add(grid)
|
||||
self.set_size_request( 300, 900)
|
||||
self.show_all()
|
||||
Gtk.main()
|
||||
|
||||
def select_dir(self,widget):
|
||||
self.dir = widget.get_label()
|
||||
self.hide()
|
||||
Gtk.main_quit()
|
||||
22
src/Plan.py
Normal file
22
src/Plan.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import gtk
|
||||
|
||||
|
||||
class Plan():
|
||||
def load(self,filename):
|
||||
file = open(filename,'r')
|
||||
lines = file.readlines()
|
||||
print(lines)
|
||||
|
||||
|
||||
dialog = gtk.FileChooserDialog("Open...",
|
||||
None,
|
||||
gtk.FILE_CHOOSER_ACTION_OPEN,
|
||||
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
|
||||
|
||||
response = dialog.run();
|
||||
|
||||
if response == gtk.RESPONSE_OK:
|
||||
plan = Plan();
|
||||
plan.load(dialog.get_filename())
|
||||
dialog.destroy()
|
||||
@@ -1,63 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
from Tiles import *
|
||||
|
||||
class TileSelector(Gtk.Window):
|
||||
def __init__(self,connection):
|
||||
Gtk.Window.__init__(self)
|
||||
self.connect("destroy", Gtk.main_quit)
|
||||
|
||||
self.grid = Gtk.Grid()
|
||||
|
||||
x0=0
|
||||
x1=0
|
||||
x2=0
|
||||
|
||||
if connection in {Tile.LEFT,Tile.RIGHT,None}:
|
||||
self.add_tile(StraightH,x0,0)
|
||||
x0+=1
|
||||
|
||||
if connection in {Tile.TOP,Tile.BOTTOM,None}:
|
||||
self.add_tile(StraightV,x0,0)
|
||||
x0+=1
|
||||
|
||||
if connection in {Tile.TOP,Tile.LEFT,None}:
|
||||
self.add_tile(Diag_TL,x1,1)
|
||||
x1+=1
|
||||
|
||||
if connection in {Tile.TOP,Tile.RIGHT,None}:
|
||||
self.add_tile(Diag_TR,x1,1)
|
||||
x1+=1
|
||||
|
||||
if connection in {Tile.BOTTOM,Tile.RIGHT,None}:
|
||||
self.add_tile(Diag_BR,x1,1)
|
||||
x1+=1
|
||||
|
||||
if connection in {Tile.BOTTOM,Tile.LEFT,None}:
|
||||
self.add_tile(Diag_BL,x1,1)
|
||||
x1+=1
|
||||
|
||||
if connection in {Tile.BOTTOM,Tile.LEFT,Tile.RIGHT,None}:
|
||||
self.add_tile(TO_BRL,0,2)
|
||||
self.add_tile(TO_BLR,1,2)
|
||||
|
||||
if connection in {Tile.TOP,Tile.LEFT,Tile.RIGHT,None}:
|
||||
self.add_tile(TO_TRL,0,3)
|
||||
self.add_tile(TO_TLR,1,3)
|
||||
|
||||
self.add(self.grid)
|
||||
|
||||
self.show_all()
|
||||
Gtk.main()
|
||||
|
||||
def add_tile(self,tile_class,x,y):
|
||||
tile = tile_class()
|
||||
tile.connect('button-press-event',self.select)
|
||||
self.grid.attach(tile,x,y,1,1)
|
||||
|
||||
def select(self,btn,evt):
|
||||
self.tile = btn.__class__
|
||||
self.hide()
|
||||
Gtk.main_quit()
|
||||
8
src/Tiles/Tile.py
Normal file
8
src/Tiles/Tile.py
Normal file
@@ -0,0 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
class Tile():
|
||||
def x(self):
|
||||
raise NotImplementedError('Tile.x')
|
||||
|
||||
def y(self):
|
||||
raise NotImplementedError('Tile.x')
|
||||
@@ -1,87 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
from TileSelector import *
|
||||
import os
|
||||
|
||||
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))
|
||||
else:
|
||||
print(" " * level + "%s -> %s:" % (key, value.__class__.__name__))
|
||||
dump_obj(value, key, level + 2)
|
||||
|
||||
class TrackPlan(Gtk.Window):
|
||||
def __init__(self,json,server_sock):
|
||||
Gtk.Window.__init__(self)
|
||||
|
||||
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)
|
||||
tile = tileSelector.tile()
|
||||
if origin != None:
|
||||
if connection == Tile.TOP:
|
||||
origin.bottom = tile
|
||||
if connection == Tile.RIGHT:
|
||||
origin.left = tile
|
||||
if connection == Tile.BOTTOM:
|
||||
origin.top = tile
|
||||
if connection == Tile.LEFT:
|
||||
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('?')
|
||||
btn.x = x
|
||||
btn.y = y
|
||||
btn.connect('clicked',self.select_tile,origin,connection)
|
||||
self.grid.attach(btn,x,y,1,1)
|
||||
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:
|
||||
self.putButton(x-1,y,tile,Tile.RIGHT)
|
||||
if tile.connects_right():
|
||||
right = self.grid.get_child_at(x+1,y)
|
||||
if right == None:
|
||||
self.putButton(x+1,y,tile,Tile.LEFT)
|
||||
if tile.connects_up():
|
||||
top = self.grid.get_child_at(x,y-1)
|
||||
if top == None:
|
||||
self.putButton(x,y-1,tile,Tile.BOTTOM)
|
||||
if tile.connects_down():
|
||||
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')
|
||||
self.server_sock.send('UPDATE PLAN')
|
||||
self.server_sock.send(seed_tile.json())
|
||||
print('Sent plan')
|
||||
@@ -1,32 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
|
||||
class YesNoDialog(Gtk.Window):
|
||||
def __init__(self,question):
|
||||
Gtk.Window.__init__(self)
|
||||
self.connect("delete-event", Gtk.main_quit)
|
||||
|
||||
grid = Gtk.Grid()
|
||||
|
||||
question = Gtk.Label(question);
|
||||
yes = Gtk.Button('Yes')
|
||||
yes.connect('clicked',self.evaluate)
|
||||
no = Gtk.Button('No')
|
||||
no.connect('clicked',self.evaluate)
|
||||
|
||||
grid.attach(question,0,0,2,1)
|
||||
grid.attach(yes,0,1,1,1)
|
||||
grid.attach(no,1,1,1,1)
|
||||
self.add(grid)
|
||||
|
||||
self.answer = None
|
||||
|
||||
self.show_all()
|
||||
Gtk.main()
|
||||
|
||||
def evaluate(self,widget):
|
||||
self.answer=widget.get_label() == 'Yes'
|
||||
self.hide()
|
||||
Gtk.main_quit()
|
||||
@@ -1,59 +0,0 @@
|
||||
'''
|
||||
Simple socket server using threads
|
||||
'''
|
||||
|
||||
import socket
|
||||
import sys
|
||||
from thread import *
|
||||
|
||||
HOST = '' # Symbolic name meaning all available interfaces
|
||||
PORT = 8888 # Arbitrary non-privileged port
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
print 'Socket created'
|
||||
|
||||
#Bind socket to local host and port
|
||||
try:
|
||||
s.bind((HOST, PORT))
|
||||
except socket.error as msg:
|
||||
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
|
||||
sys.exit()
|
||||
|
||||
print 'Socket bind complete'
|
||||
|
||||
#Start listening on socket
|
||||
s.listen(10)
|
||||
print 'Socket now listening'
|
||||
|
||||
#Function for handling connections. This will be used to create threads
|
||||
def clientthread(conn):
|
||||
#Sending message to connected client
|
||||
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
|
||||
|
||||
#infinite loop so that function do not terminate and thread do not end.
|
||||
while True:
|
||||
|
||||
#Receiving from client
|
||||
data = conn.recv(1024).strip()
|
||||
if data == 'exit':
|
||||
conn.sendall('Bye!')
|
||||
break
|
||||
reply = 'OK...' + data
|
||||
if not data:
|
||||
break
|
||||
|
||||
conn.sendall(reply)
|
||||
|
||||
#came out of loop
|
||||
conn.close()
|
||||
|
||||
#now keep talking with the client
|
||||
while 1:
|
||||
#wait to accept a connection - blocking call
|
||||
conn, addr = s.accept()
|
||||
print 'Connected with ' + addr[0] + ':' + str(addr[1])
|
||||
|
||||
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
|
||||
start_new_thread(clientthread ,(conn,))
|
||||
|
||||
s.close()
|
||||
@@ -1,72 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import socket
|
||||
from ConnectDialog import *
|
||||
from FileDialog import *
|
||||
from TrackPlan import *
|
||||
from YesNoDialog import *
|
||||
|
||||
|
||||
def readline(socket):
|
||||
buff = socket.recv(4096)
|
||||
buffering = True
|
||||
while buffering:
|
||||
if "\n" in buff:
|
||||
(line, buff) = buff.split("\n", 1)
|
||||
yield line
|
||||
else:
|
||||
more = socket.recv(4096)
|
||||
if not more:
|
||||
buffering = False
|
||||
else:
|
||||
buff += more
|
||||
if buff:
|
||||
yield buff
|
||||
|
||||
print ('starting')
|
||||
conn_dlg = ConnectDialog()
|
||||
conn_dlg.run()
|
||||
|
||||
if conn_dlg.port is not None:
|
||||
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
client_sock.connect((conn_dlg.host, conn_dlg.port))
|
||||
client = readline(client_sock)
|
||||
current_dir = ''
|
||||
|
||||
while True:
|
||||
line = client.next()
|
||||
|
||||
if line.startswith('current dir'):
|
||||
current_dir = line
|
||||
continue
|
||||
|
||||
if line.startswith('select one'):
|
||||
file_dialog = FileDialog(line, current_dir)
|
||||
while True:
|
||||
line = client.next()
|
||||
if line.startswith('--'):
|
||||
break
|
||||
file_dialog.add_dir(line.strip())
|
||||
file_dialog.run()
|
||||
|
||||
client_sock.send(file_dialog.dir + "\n")
|
||||
continue
|
||||
|
||||
if line == 'PLAN:':
|
||||
json = client.next()
|
||||
plan = TrackPlan(json, client_sock)
|
||||
plan.run()
|
||||
client_sock.send('EXIT')
|
||||
break
|
||||
continue
|
||||
|
||||
if 'does not exist. Create' in line:
|
||||
directory = line.split('does not exist')[0].strip()
|
||||
dialog = YesNoDialog(line)
|
||||
if dialog.answer:
|
||||
client_sock.send("yes\n")
|
||||
else:
|
||||
client_sock.send("no\n")
|
||||
continue
|
||||
|
||||
print(line)
|
||||
print "Test"
|
||||
@@ -1,107 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import socket
|
||||
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)
|
||||
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
print('Socket created.')
|
||||
|
||||
try:
|
||||
self.socket.bind(('', port))
|
||||
except socket.error as msg:
|
||||
print('Bind failed. Error (' + str(msg[0]) + '): ' + str(msg[1]))
|
||||
sys.exit(-1)
|
||||
|
||||
print('Bound to socket at port ' + str(port))
|
||||
|
||||
def client(self, conn):
|
||||
if self.system is None:
|
||||
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, 0o755)
|
||||
json_file = open(path + '/plan.json', 'w+')
|
||||
json_file.close()
|
||||
|
||||
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 = '/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")
|
||||
contents = sorted(os.listdir(path))
|
||||
|
||||
conn.send("select one from\n")
|
||||
for entry in contents:
|
||||
conn.send(' ' + entry + "\n")
|
||||
conn.send("--\n")
|
||||
entry = conn.recv(1024).strip()
|
||||
if entry in contents:
|
||||
if os.path.isfile(path + entry):
|
||||
break
|
||||
path += entry + '/'
|
||||
else:
|
||||
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 response == 'abort':
|
||||
path = None
|
||||
break
|
||||
path = '/'
|
||||
|
||||
if path is None:
|
||||
return
|
||||
|
||||
return path
|
||||
|
||||
def start(self):
|
||||
self.socket.listen(10)
|
||||
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 = 7668
|
||||
|
||||
server = web4rail_server(port)
|
||||
server.start()
|
||||
Reference in New Issue
Block a user