2008-09-17 20:52:15 +00:00
|
|
|
from __future__ import with_statement
|
|
|
|
import time
|
|
|
|
from socket import *
|
|
|
|
from WriteGPX import *
|
|
|
|
|
|
|
|
class TrackServer:
|
2008-09-17 21:13:13 +00:00
|
|
|
def __init__(self, host='localhost', port='49152', hashfile='hashfile', datadir = '.'):
|
2008-09-17 20:52:15 +00:00
|
|
|
self.InitSocker(host, port)
|
|
|
|
self.InitHashdb(hashfile)
|
|
|
|
self.InitTrackDict()
|
|
|
|
self.datadir = datadir
|
|
|
|
|
2008-09-17 21:09:46 +00:00
|
|
|
def InitHashdb(self, hashfile):
|
|
|
|
self.hashdb=[]
|
|
|
|
with open(hashfile, "r") as file:
|
|
|
|
for line in file:
|
|
|
|
if line:
|
|
|
|
self.hashdb.append((line.split()[0], \
|
|
|
|
line.split()[1]))
|
2008-09-17 20:52:15 +00:00
|
|
|
|
2008-09-17 21:09:46 +00:00
|
|
|
def InitTrackDict(self):
|
|
|
|
self.TrackDict={}
|
|
|
|
for data in self.hashdb:
|
|
|
|
self.TrackDict[data[0]] = ""
|
2008-09-17 20:52:15 +00:00
|
|
|
|
|
|
|
def InitSocket(self, host, port):
|
|
|
|
|
|
|
|
# Set the socket parameters
|
|
|
|
# e.g. host = "localhost"
|
|
|
|
# e.g. port = 49152
|
2008-09-17 21:09:46 +00:00
|
|
|
self.__addr = (str(host),int(port))
|
2008-09-17 20:52:15 +00:00
|
|
|
|
|
|
|
# Create socket and bind it to the address
|
|
|
|
self.__UDPSock = socket(AF_INET,SOCK_DGRAM)
|
|
|
|
self.__UDPSock.bind(sel.__addr)
|
|
|
|
|
|
|
|
# Debug message:
|
|
|
|
print "UDP Socket for %s at port %s created" % (host, port)
|
|
|
|
|
2008-09-17 21:09:46 +00:00
|
|
|
def VerifyUser(self, username, password_hash):
|
|
|
|
for data in self.hashdb:
|
|
|
|
if data[0] == username and data[1] == password_hash:
|
|
|
|
return 1
|
|
|
|
return 0
|
2008-09-17 20:52:15 +00:00
|
|
|
|
|
|
|
|
2008-09-17 21:09:46 +00:00
|
|
|
def Parser(self, stuff):
|
2008-09-17 20:52:15 +00:00
|
|
|
|
|
|
|
# Parses the complete data sent to UDP port
|
2008-09-17 21:09:46 +00:00
|
|
|
try:
|
2008-09-17 20:52:15 +00:00
|
|
|
username, password_hash, action, data = stuff.split(';')
|
|
|
|
|
|
|
|
# Verifies the user and password
|
|
|
|
if self.VerifyUser(username, password_hash):
|
|
|
|
if action == "START":
|
|
|
|
self.NewTrack(username)
|
|
|
|
if action == "STOP":
|
|
|
|
self.CloseTrack(username)
|
|
|
|
if action == "TRANSMIT":
|
|
|
|
self.AddToTrack(username, data)
|
|
|
|
print "Action", action,"received"
|
|
|
|
except:
|
|
|
|
print "Something went wrong.."
|
|
|
|
|
|
|
|
def NewTrack(self, username):
|
2008-09-17 21:00:27 +00:00
|
|
|
# if a track has already started it needs to be closed (finished)
|
|
|
|
if self.TrackDict[username]:
|
2008-09-17 21:09:46 +00:00
|
|
|
self.TrackDict[username].close()
|
2008-09-17 21:00:27 +00:00
|
|
|
# start the new track
|
2008-09-17 21:13:13 +00:00
|
|
|
self.TrackDict[username] = WriteGPX("%s%s%s" % (self.datadir, username, time.strftime("%Y%m%d%H%M%S")))
|
2008-09-17 21:09:46 +00:00
|
|
|
print "Created track", self.TrackDict[username]
|
2008-09-17 20:52:15 +00:00
|
|
|
|
|
|
|
def CloseTrack(self, username):
|
|
|
|
if self.TrackDict[username]:
|
|
|
|
self.TrackDict[username].close()
|
|
|
|
print "Closed track", self.TrackDict[username]
|
2008-09-17 21:00:27 +00:00
|
|
|
else:
|
|
|
|
print "ha, no track for %s exists!" % (username)
|
2008-09-17 20:52:15 +00:00
|
|
|
|
|
|
|
def AddToTrack(self, username, data):
|
|
|
|
lat, lon, ele, time = data.split(',')
|
|
|
|
self.TrackDict[username].write(lat, lon, ele, time)
|
|
|
|
|
|
|
|
|
|
|
|
buf = 1024
|
|
|
|
|
|
|
|
# Create instance of TrackServer
|
|
|
|
instance = TrackServer()
|
|
|
|
|
|
|
|
#Receive messages
|
|
|
|
while 1:
|
|
|
|
data,addr = UDPSock.recvfrom(1024)
|
|
|
|
print "Following data received:", data
|
|
|
|
instance.Parser(data)
|