Felix departed
[galtack.git] / galtack / net_client_logger.py
bloba7841acdf69dfd2b433af1958f68cda22dc1c6f0
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 GalTacK networking - client-side logging.
5 """
6 # Copyright (C) 2007 Felix Rabe <public@felixrabe.textdriven.com>
7 # Copyright (C) 2007 Michael Carter
9 # Permission is hereby granted, free of charge, to any person obtaining a
10 # copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to permit
14 # persons to whom the Software is furnished to do so, subject to the
15 # following conditions:
17 # The above copyright notice and this permission notice shall be included
18 # in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
25 # OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
26 # THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Recommended line length or text width: 75 characters.
30 import time
31 from galtack.net_client_base import *
34 class GaltackClientCmdLoggerBaseMixin(GaltackClientBase):
35 """
36 GaltackClient base mixin for classes implementing traffic dumping.
37 """
39 def __init__(self, *a, **kw):
40 super(GaltackClientCmdLoggerBaseMixin, self).__init__(*a, **kw)
41 self._command_log_file = self._getopt("command_log_file", kw)
42 if isinstance(self._command_log_file, basestring):
43 self._command_log_file = file(self._command_log_file, "wb")
45 @classmethod
46 def _modify_option_parser(cls, option_parser):
47 C = GaltackClientCmdLoggerBaseMixin
48 option_parser = super(C, cls)._modify_option_parser(option_parser)
49 option_parser.add_option("-l", "--command-log-file",
50 dest = cls._mkopt("command_log_file"),
51 metavar = "FILE", default = None,
52 help = "log Galtack commands to a file")
53 return option_parser
56 class GaltackClientRecvCmdLoggerMixin(GaltackClientCmdLoggerBaseMixin):
57 """
58 GaltackClient that dumps received commands to self.command_dump_file.
59 """
61 def datagramReceived(self, datagram, host):
62 """
63 Override datagramReceived instead of _commands_received as we want
64 to log *everything* (resent commands as well).
65 """
66 f = self._command_log_file
67 if f is not None:
68 f.write("\n*** COMMANDS ** SERVER ** %f ***\n" % time.time())
69 f.write(datagram)
70 # Call parent class' datagramReceived method:
71 Cls = GaltackClientRecvCmdLoggerMixin
72 return super(Cls, self).datagramReceived(datagram, host)
75 class GaltackClientSendCmdLoggerMixin(GaltackClientCmdLoggerBaseMixin,
76 GaltackClientSendCmdBaseMixin):
77 """
78 GaltackClient that dumps sent commands to self.command_dump_file.
79 """
81 def _post_send_datagram_hook(self, datagram, **kw):
82 """
83 Post-process the sent datagram.
84 """
85 f = self._command_log_file
86 if f is not None:
87 f.write("\n*** COMMANDS ** CLIENT ** %f ***\n" % time.time())
88 f.write(datagram)
89 # Call parent class' datagramReceived method:
90 Cls = GaltackClientSendCmdLoggerMixin
91 return super(Cls, self)._post_send_datagram_hook(datagram, **kw)