import repository from arizona
[raven.git] / apps / tempest / tempestpubsub.py
1 import os
2 import socket
3 import sys
4 import threading
5 import time
6
7 import ravenlib.stats
8 from ravenlib.pubsub.receiver import PubSubReceiver
9
10 REPO_PUBSUB_PORT = 4321
11
12 class TempestReceiver(PubSubReceiver):
13     def __init__(self, hostAddr, destFilename):
14         self.destFilename = destFilename
15         self.seqno = -1
16         self.version = -1
17         PubSubReceiver.__init__(self, hostAddr)
18
19     def msg_received(self, msg):
20         filename = msg.get("filename", None)
21         contents = msg.get("contents", None)
22         version = msg.get("version", 0)
23         seqno = msg.get("seqno", 0)
24
25         print >> sys.stdout, "update received from file", filename
26
27         open(self.destFilename + ".tmppubsub", "w").write(contents)
28
29         os.rename(self.destFilename + ".tmppubsub", self.destFilename)
30
31         print >> sys.stdout, "stored file in", self.destFilename
32
33         ravenlib.stats.update("tempestd_pubsub_recv")
34
35         # collect statistics on the sequence numbers
36         if (version != self.version):
37             self.version = version
38             ravenlib.stats.update("tempestd_pubsub_wrongversion")
39         elif (seqno < self.seqno):
40             ravenlib.stats.update("tempestd_pubsub_outoforder")
41         elif (seqno == self.seqno):
42             ravenlib.stats.update("tempestd_pubsub_dup")
43         elif (seqno > (self.seqno+1)):
44             ravenlib.stats.update("tempestd_pubsub_miss", seqno - (self.seqno+1))
45
46         self.seqno = seqno
47