1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
23 from gsh
.console
import console_output
25 CMD_PREFIX
= 'python -c "`echo "' + pity
.ENCODED
+ '"|' + \
27 'openssl base64 -d`" '
29 CMD_SEND
= CMD_PREFIX
+ '%d send "%s" "%s" "%s"\n'
30 CMD_FORWARD
= CMD_PREFIX
+ '%d forward "%s" "%s"\n'
31 CMD_RECEIVE
= CMD_PREFIX
+ '%d receive "%s" "%s"\n'
33 def received_cookie(dispatcher
, line
):
34 host_port
= line
[len(dispatcher
.file_transfer_cookie
) + 1:]
35 dispatcher
.file_transfer_cookie
= None
36 previous_shell
= get_previous_shell(dispatcher
)
37 previous_shell
.dispatch_write(host_port
+ '\n')
40 """Returns (nr_peers, first, last)"""
41 from gsh
import dispatchers
45 for i
in dispatchers
.all_instances():
51 return nr_peers
, first
, last
53 def get_previous_shell(shell
):
54 from gsh
import dispatchers
55 shells
= [i
for i
in dispatchers
.all_instances() if i
.enabled
]
56 current_pos
= shells
.index(shell
)
58 current_pos
= (current_pos
- 1) % len(shells
)
59 prev_shell
= shells
[current_pos
]
60 if prev_shell
.enabled
:
63 def replicate(shell
, path
):
64 from gsh
import dispatchers
65 from gsh
import remote_dispatcher
66 nr_peers
= len([i
for i
in dispatchers
.all_instances() if i
.enabled
])
68 console_output('No other remote shell to replicate files to\n')
70 receiver
= get_previous_shell(shell
)
71 for i
in dispatchers
.all_instances():
74 cookie1
= '[gsh file transfer ' + str(random
.random())[2:]
75 cookie2
= str(random
.random())[2:] + ']'
76 i
.file_transfer_cookie
= cookie1
+ cookie2
78 i
.dispatch_command(CMD_SEND
% (nr_peers
, path
, cookie1
, cookie2
))
80 i
.dispatch_command(CMD_FORWARD
% (nr_peers
, cookie1
, cookie2
))
82 i
.dispatch_command(CMD_RECEIVE
% (nr_peers
, cookie1
, cookie2
))
83 i
.change_state(remote_dispatcher
.STATE_RUNNING
)