Proxy work
[galtack.git] / galtack_proxy.py
blobe5fe4e4756f1a29f13b9efd8b2db1f94819aba9b
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 GalTacK Proxy - No need to leave a game when restarting the client.
5 """
6 # Copyright (C) 2007 Michael Carter
7 # Copyright (C) 2007 Felix Rabe <public@felixrabe.textdriven.com>
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 from galtack_client import *
32 import galtack.net
33 class GaltackProxyClient( # to the server
34 galtack.net.GaltackClientRecvCmdLoggerMixin,
35 galtack.net.GaltackClientSendCmdLoggerMixin,
36 galtack.net.GaltackClientUniverseTrackerMixin,
37 galtack.net.GaltackClientHousekeeperMixin,
38 galtack.net.GaltackClientBase,
39 ): pass
42 class GaltackServerHousekeeperMixin(
43 galtack.net.GaltackClientAckExpectingMixin,
44 galtack.net.GaltackClientPingSenderMixin,
45 galtack.net.GaltackClientAckSenderMixin,
46 galtack.net.GaltackClientSendCmdBaseMixin,
48 """
49 GaltackServer that interacts properly with a Galcon server.
51 It takes the responsibility for setting up and shutting down the
52 connection.
54 It (implicitly) defines an imaginary server-side command name
55 "[CLOSE]-[ACK]" to which callbacks with the following signature may be
56 registered:
57 def callback() # *no* argument
58 It gets called whenever a logout could be completed successfully.
60 Alternatively, you may connect to the deferred returned by logout(),
61 which receives None as an argument.
62 """
64 class LogoutAborted(Exception): pass
66 def __init__(self, *a, **kw):
67 super(GaltackServerHousekeeperMixin, self).__init__(*a, **kw)
68 self.register_command_callback("version", self.__cmd_version)
69 self.register_command_callback("logout", self.__cmd_logout)
70 self.__logout_deferred = None
72 def startProtocol(self):
73 pass
75 def __cmd_version(self, command):
76 self.send_commands((1, "version", command[3]))
78 def __cmd_logout(self, command):
79 self.send_commands((1, "[CLOSE]", command[3]))
83 class GaltackProxyServer( # to the client
84 GaltackServerHousekeeperMixin,
85 ): pass
88 class GaltackProxyWindow(WindowF12RawConsoleMixin):
89 """
90 GalTacK Proxy control.
91 """
93 def __init__(self, *a, **kw):
94 super(GaltackProxyWindow, self).__init__()
95 self.set_title("GalTacK Proxy")
97 (outer, inner) = gnome_hig(self)
98 table = gnome_hig(inner(Table(), False, False))
100 xo = {"xoptions": 0}
102 table.add_rows()
103 table.attach_cell(LeftLabel("Listening Port:"), **xo)
104 self.__port_entry = table.attach_cell(Entry())
106 table.add_rows()
107 self.__listen_button = table.attach_row(Button("Listen"))
108 self.__listen_button.connect("clicked", self.__cb_listen_clicked)
110 C = GaltackClient
111 self.galtack_client = C(self.__options,
112 self.__user_info, self.__server_info)
114 def __cb_listen_clicked(self, button):
115 print "listen"
118 class GaltackServerListWindow(GaltackServerListWindow):
120 Window displaying the list of Galcon servers.
123 def __init__(self, *a, **kw):
124 super(GaltackServerListWindow, self).__init__(*a, **kw)
125 self.set_title("Galcon Server List - GalTacK Proxy")
127 def join_with_server_info(self, server_info):
128 chat_window = GaltackProxyWindow(self.__options,
129 self.__user_info, server_info)
130 gobject.idle_add(self.hide)
133 class GaltackLoginWindow(GaltackLoginWindow):
135 NEXT_CLASS = GaltackServerListWindow
137 def __init__(self, *a, **kw):
138 super(GaltackLoginWindow, self).__init__(*a, **kw)
139 self.set_title("GalTacK Proxy Login")
142 def main(argv):
143 option_parser = optparse.OptionParser(prog = "GalTacK Proxy",
144 version = "%%prog %s" %
145 galtack_version_str)
146 option_parser = GaltackLoginWindow._modify_option_parser(option_parser)
147 option_parser = GaltackClient._modify_option_parser(option_parser)
148 options, arguments = option_parser.parse_args(argv[1:])
149 GaltackLoginWindow._instance = GaltackLoginWindow(options)
150 main_loop_run()
151 return 0
153 if __name__ == "__main__":
154 import sys
155 sys.exit(main(sys.argv))