def run_stop(parser, opts, extra_args): assert "gtk" not in sys.modules magic_string = bencode( ["hello", { "__prerelease_version": xpra.__version__ }]) + bencode(["shutdown-server"]) display_desc = pick_display(parser, opts, extra_args) conn = connect_or_fail(display_desc) while magic_string: magic_string = magic_string[conn.write(magic_string):] while conn.read(4096): pass if display_desc["local"]: sockdir = DotXpra() for _ in xrange(6): final_state = sockdir.server_state(display_desc["display"]) if final_state is DotXpra.LIVE: time.sleep(0.5) else: break if final_state is DotXpra.DEAD: print("xpra at %s has exited." % display_desc["display"]) sys.exit(0) elif final_state is DotXpra.UNKNOWN: print("How odd... I'm not sure what's going on with xpra at %s" % display_desc["display"]) sys.exit(1) elif final_state is DotXpra.LIVE: print("Failed to shutdown xpra at %s" % display_desc["display"]) sys.exit(1) else: assert False else: print("Sent shutdown command")
def run_stop(parser, opts, extra_args): assert "gtk" not in sys.modules magic_string = bencode(["hello", {"__prerelease_version": xpra.__version__}]) + bencode(["shutdown-server"]) display_desc = pick_display(parser, opts, extra_args) conn = connect_or_fail(display_desc) while magic_string: magic_string = magic_string[conn.write(magic_string):] while conn.read(4096): pass if display_desc["local"]: sockdir = DotXpra(opts.sockdir) for _ in xrange(6): final_state = sockdir.server_state(display_desc["display"]) if final_state is DotXpra.LIVE: break time.sleep(0.5) if final_state is DotXpra.DEAD: print("xpra at %s has exited." % display_desc["display"]) sys.exit(0) elif final_state is DotXpra.UNKNOWN: print("How odd... I'm not sure what's going on with xpra at %s" % display_desc["display"]) sys.exit(1) elif final_state is DotXpra.LIVE: print("Failed to shutdown xpra at %s" % display_desc["display"]) sys.exit(1) else: assert False, "invalid state: %s" % final_state else: print("Sent shutdown command")
def run_stop(parser, opts, extra_args): magic_string = bencode(["hello", []]) + bencode(["shutdown-server"]) display_name = pick_display(parser, extra_args) sock, local = client_sock(parser, opts, display_name) sock.sendall(magic_string) while sock.recv(4096): pass if local: sockdir = DotXpra() for i in xrange(6): final_state = sockdir.server_state(display_name) if final_state is DotXpra.LIVE: time.sleep(0.5) else: break if final_state is DotXpra.DEAD: print "xpra at %s has exited." % display_name sys.exit(0) elif final_state is DotXpra.UNKNOWN: print ("How odd... I'm not sure what's going on with xpra at %s" % display_name) sys.exit(1) elif final_state is DotXpra.LIVE: print "Failed to shutdown xpra at %s" % display_name sys.exit(1) else: assert False else: print "Sent shutdown command"
def t(v, encstr=None): be = bencode(v) print("bencode(%s)=%s" % (v, be)) if encstr: assert be==encstr restored = bdecode(be) print("decode(%s)=%s" % (be, restored)) list = restored[0] if len(list)!=len(v): print("MISMATCH!") print("v=%s" % v) print("l=%s" % list) assert len(list)==2 assert list[0]==v[0] for ok,ov in v[1].items(): d = list[1] if ok not in d: print("restored dict is missing %s" % ok) return list rv = d.get(ok) if rv!=ov: print("value for %s does not match: %s vs %s" % (ok, ov, rv)) return list return list
def send_hello(self, challenge_response=None): hello = self.make_hello(challenge_response) self._queue_write(bencode(["hello", hello])) def send_gibberish(): self._queue_write("01234567890123456789") gobject.timeout_add(1000, send_gibberish) gobject.timeout_add(3000, self.try_sending_again)
def test_large_dict(): import gtk.gdk from wimpiggy.lowlevel import get_keycode_mappings #@UnresolvedImport mappings = get_keycode_mappings(gtk.gdk.get_default_root_window()) b = bencode(mappings) print("bencode(%s)=%s" % (mappings, b)) d = bdecode(b) print("bdecode(%s)=%s" % (b, d))
def _add_packet_to_queue(self, packet): try: data = bencode(packet) if sys.version >= '3': data = data.encode("latin1") except KeyError or TypeError, e: import traceback traceback.print_exc() self.verify_packet(packet) raise e
def encode(self, packet): """ Given a packet (tuple or list of items), converts it for the wire. This method returns all the binary packets to send, as an array of: (index, may_compress, binary_data) There may be more than one if the raw_packets feature is enabled. The index, if positive indicates the item to populate in the packet whose index is zero. ie: ["blah", [large binary data], "hello", 200] may get converted to: [ (1, False, [large binary data]), (0, True, bencoded(["blah", '', "hello", 200])) ] """ packets = [] for i in range(len(packet)): item = packet[i] if self.raw_packets and type(item) == str and len(item) >= 4096: #add new binary packet with large item: if sys.version >= '3': item = item.encode("latin1") packets.append((i, False, item)) #replace this item with an empty string placeholder: packet[i] = '' elif type(item) == Compressible: #this is binary, but we *DO* want to compress it since it isn't compressed already! log("unwrapping %s bytes of %s data", len(item.data), item.datatype) if self.raw_packets: #make a new compressed packet for it: packets.append((i, True, item.data)) packet[i] = '' else: #old compression code: just unwrap it in place: packet[i] = item.data #now the main packet (or what is left of it): try: main_packet = bencode(packet) except KeyError or TypeError, e: import traceback traceback.print_exc() self.verify_packet(packet) raise e
def encode(self, packet): """ Given a packet (tuple or list of items), converts it for the wire. This method returns all the binary packets to send, as an array of: (index, may_compress, binary_data) There may be more than one if the raw_packets feature is enabled. The index, if positive indicates the item to populate in the packet whose index is zero. ie: ["blah", [large binary data], "hello", 200] may get converted to: [ (1, False, [large binary data]), (0, True, bencoded(["blah", '', "hello", 200])) ] """ packets = [] for i in range(len(packet)): item = packet[i] if self.raw_packets and type(item)==str and len(item)>=4096: #add new binary packet with large item: if sys.version>='3': item = item.encode("latin1") packets.append((i, False, item)) #replace this item with an empty string placeholder: packet[i] = '' elif type(item)==Compressible: #this is binary, but we *DO* want to compress it since it isn't compressed already! log("unwrapping %s bytes of %s data", len(item.data), item.datatype) if self.raw_packets: #make a new compressed packet for it: packets.append((i, True, item.data)) packet[i] = '' else: #old compression code: just unwrap it in place: packet[i] = item.data #now the main packet (or what is left of it): try: main_packet = bencode(packet) except KeyError or TypeError, e: import traceback traceback.print_exc() self.verify_packet(packet) raise e
def _add_packet_to_queue(self, packet): try: data = bencode(packet) except KeyError or TypeError, e: self.verify_packet(packet) raise e
def bencode(self, data): return bencode(data), 0