summaryrefslogtreecommitdiffstats
path: root/tupkg/server/tupkgs
blob: b4a3e867c97347270b3d6051e2a71e2b708c8c04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python -O
#
# Description:
# ------------
# This is the server-side portion of the Trusted User package
# manager.  This program will receive uploads from its client-side
# couterpart, tupkg.  Once a package is received and verified, it
# is placed in a specified temporary incoming directory where
# a separate script will handle migrating it to the AUR.  For
# more information, see the ../README.txt file.
#
# Python Indentation:
# -------------------
# For a vim: line to be effective, it must be at the end of the
# file.  See the end of the file for more information.

import sys
import socket
import threading
import select

class ClientSocket(threading.Thread):
	def __init__(self, socket, **other):
		threading.Thread.__init__(self, *other)
		self.socket = socket

	def close(self):
		pass

	def run(self):
		while len(self.socket.recv(1)) != 0:
			pass

class ServerSocket(threading.Thread):
	def __init__(self, port=1034, maxqueue=5, **other):
		threading.Thread.__init__(self, *other)
		self.running = 1
		self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.socket.bind((socket.gethostname(), port))
		self.socket.listen(maxqueue)
		self.clients = []

	def _clean(self, client):
		if not client.isAlive():
			return 0
		return 1

	def close(self):
		self.socket.close()

	def run(self):
		while self.running:
			sread, swrite, serror = select.select([self.socket],[self.socket],[self.socket],5)
			if sread:
				(clientsocket, address) = self.socket.accept()
				ct = ClientSocket(clientsocket)
				ct.start()
				self.clients.append(ct)

			print len(self.clients)
			self.clients = filter(self._clean, self.clients)
			print len(self.clients)
		self.socket.close()
		[x.close() for x in self.clients]
		[x.join() for x in self.clients]

def main(argv=None):
	if argv is None:
		argv = sys.argv

	running = 1

	servsock = ServerSocket()
	servsock.start()

	try:
		while running:
			# Maybe do stuff here?
			pass
	except KeyboardInterrupt:
		running = 0

	print "Just cleaning up stuff"

	servsock.running = 0

	servsock.join()

	return 0

if __name__ == "__main__":
		sys.exit(main())

# Python Indentation:
# -------------------
# Use tabs not spaces.  If you use vim, the following comment will
# configure it to use tabs.
#
# vim:noet:ts=2 sw=2 ft=python