From 8d1e105e51955ddd7637d3ce42bc35fa9666c420 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Wed, 17 May 2006 17:14:36 -0400 Subject: [PATCH] Actually hook up the action button --- sugar/browser/NotificationBar.py | 3 +- sugar/browser/browser.py | 2 + sugar/p2p/MostlyReliablePipe.py | 146 ++----------------------------- 3 files changed, 13 insertions(+), 138 deletions(-) diff --git a/sugar/browser/NotificationBar.py b/sugar/browser/NotificationBar.py index 01b5b73b..ee87031b 100644 --- a/sugar/browser/NotificationBar.py +++ b/sugar/browser/NotificationBar.py @@ -16,6 +16,7 @@ class NotificationBar(gtk.HBox): self._text_label.show() self._action_button = gtk.Button() + self._action_button.connect('clicked', self.__button_clicked) self.pack_start(self._action_button, False) self._action_button.show() @@ -26,5 +27,5 @@ class NotificationBar(gtk.HBox): self._action_id = action_id self._action_button.set_label(action_text) - def __action_button_clicked(self): + def __button_clicked(self): self.emit("action", self._action_id) diff --git a/sugar/browser/browser.py b/sugar/browser/browser.py index 6ff6ed3e..994165fa 100755 --- a/sugar/browser/browser.py +++ b/sugar/browser/browser.py @@ -173,9 +173,11 @@ class BrowserActivity(activity.Activity): self._mode = BrowserActivity.SOLO def __notif_bar_action_cb(self, bar, action_id): + print action_id if action_id == 'set_shared_address': address = self.embed.get_address() self._model.set_value('current_address', address) + print 'address set to ' + address elif action_id == 'goto_shared_address': address = self._model.get_value("current_address") self.embed.load_address(address) diff --git a/sugar/p2p/MostlyReliablePipe.py b/sugar/p2p/MostlyReliablePipe.py index 4a951e45..ae17c441 100644 --- a/sugar/p2p/MostlyReliablePipe.py +++ b/sugar/p2p/MostlyReliablePipe.py @@ -236,11 +236,6 @@ class RetransmitSegment(SegmentBase): # 20: total data sha1 # 2: segment number _RT_DATA_TEMPLATE = "! H20sH" - _RT_DATA_LEN = struct.calcsize(_RT_DATA_TEMPLATE) - - def data_template(): - return RetransmitSegment._RT_DATA_TEMPLATE - data_template = staticmethod(data_template) def __init__(self, segno, total_segs, msg_seq_num, master_sha): """Should not be called directly.""" @@ -248,37 +243,20 @@ class RetransmitSegment(SegmentBase): raise ValueError("Retransmission request messages must have only one segment.") SegmentBase.__init__(self, segno, total_segs, msg_seq_num, master_sha) - self._type = SegmentBase._SEGMENT_TYPE_RETRANSMIT - - def _verify_data(rt_msg_seq_num, rt_master_sha, rt_segment_number): - # Sanity checks on the message attributes - if not rt_segment_number or type(rt_segment_number) != type(1): - raise ValueError("RT Segment number must be in integer.") - if rt_segment_number < 1 or rt_segment_number > 65535: - raise ValueError("RT Segment number must be between 1 and 65535 inclusive.") - if not rt_msg_seq_num or type(rt_msg_seq_num) != type(1): - raise ValueError("RT Message sequnce number must be an integer.") - if rt_msg_seq_num < 1 or rt_msg_seq_num > 65535: - raise ValueError("RT Message sequence number must be between 1 and 65535 inclusive.") - if not rt_master_sha or type(rt_master_sha) != type("") or len(rt_master_sha) != 20: - raise ValueError("RT Message SHA1 checksum invalid.") - _verify_data = staticmethod(_verify_data) + self._type = SegmentBase._SEGMENT_TYPE_DATA def _make_rtms_data(rt_msg_seq_num, rt_master_sha, rt_segment_number): """Pack retransmission request payload.""" data = struct.pack(RetransmitSegment._RT_DATA_TEMPLATE, rt_msg_seq_num, rt_master_sha, rt_segment_number) - return (data, _sha_data(data)) + return (data, _sha_data(data), struct.calcsize(RetransmitSegment._RT_DATA_TEMPLATE)) _make_rtms_data = staticmethod(_make_rtms_data) def new_from_parts(msg_seq_num, rt_msg_seq_num, rt_master_sha, rt_segment_number): """Static constructor for creation from individual attributes.""" - - RetransmitSegment._verify_data(rt_msg_seq_num, rt_master_sha, rt_segment_number) - (data, data_sha) = RetransmitSegment._make_rtms_data(rt_msg_seq_num, - rt_master_sha, rt_segment_number) + (data, data_sha, data_len) = segment._make_rtms_data() segment = RetransmitSegment(1, 1, msg_seq_num, data_sha) - segment._data_len = RetransmitSegment._RT_DATA_LEN + segment._data_len = data_len segment._data = data segment._rt_msg_seq_num = rt_msg_seq_num @@ -288,14 +266,13 @@ class RetransmitSegment(SegmentBase): new_from_parts = staticmethod(new_from_parts) def _unpack_data(self, stream, data_len): - if data_len != self._RT_DATA_LEN: + if data_len != struct.calcsize(self._RT_DATA_TEMPLATE): raise ValueError("Retransmission request data had invalid length.") - data = stream.read(data_len) - (rt_msg_seq_num, rt_master_sha, rt_seg_no) = struct.unpack(self._RT_DATA_TEMPLATE, data) - RetransmitSegment._verify_data(rt_msg_seq_num, rt_master_sha, rt_seg_no) - - self._data = data self._data_len = data_len + (rt_msg_seq_num, rt_master_sha, rt_seg_no) = struct.unpack(self._RT_DATA_TEMPLATE, + stream.read(self._data_len)) + self._data = struct.pack(self._RT_DATA_TEMPLATE, rt_msg_seq_num, + rt_master_sha, rt_seg_no) self._rt_msg_seq_num = rt_msg_seq_num self._rt_master_sha = rt_master_sha self._rt_segment_number = rt_seg_no @@ -707,110 +684,6 @@ class DataSegmentTestCase(SegmentBaseTestCase): addToSuite = staticmethod(addToSuite) -class RetransmitSegmentTestCase(SegmentBaseTestCase): - """Test RetransmitSegment class specific initialization and stuff.""" - - def _test_init_fail(self, segno, total_segs, msg_seq_num, master_sha, fail_msg): - try: - seg = RetransmitSegment(segno, total_segs, msg_seq_num, master_sha) - except ValueError, exc: - pass - else: - self.fail("expected a ValueError for %s." % fail_msg) - - def testInit(self): - self._test_init_fail(0, 1, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, "invalid segment number") - self._test_init_fail(2, 1, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, "invalid segment number") - self._test_init_fail(1, 0, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, "invalid number of total segments") - self._test_init_fail(1, 2, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, "invalid number of total segments") - - # Something that's supposed to work - seg = RetransmitSegment(1, 1, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA) - assert seg.segment_type() == SegmentBase.type_retransmit(), "Segment wasn't a retransmit segment." - - def _test_new_from_parts_fail(self, msg_seq_num, rt_msg_seq_num, rt_master_sha, rt_segment_number, fail_msg): - try: - seg = RetransmitSegment.new_from_parts(msg_seq_num, rt_msg_seq_num, rt_master_sha, rt_segment_number) - except ValueError, exc: - pass - else: - self.fail("expected a ValueError for %s." % fail_msg) - - def testNewFromParts(self): - """Test RetransmitSegment's new_from_parts() functionality.""" - self._test_new_from_parts_fail(0, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid message sequence number") - self._test_new_from_parts_fail(65536, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid message sequence number") - self._test_new_from_parts_fail(None, self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid message sequence number") - self._test_new_from_parts_fail("", self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid message sequence number") - - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, 0, self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid retransmit message sequence number") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, 65536, self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid retransmit message sequence number") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, None, self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid retransmit message sequence number") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, "", self._DEF_MASTER_SHA, - self._DEF_SEGNO, "invalid retransmit message sequence number") - - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, "1" * 19, - self._DEF_SEGNO, "invalid retransmit message master SHA") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, "1" * 21, - self._DEF_SEGNO, "invalid retransmit message master SHA") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, None, - self._DEF_SEGNO, "invalid retransmit message master SHA") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, 1234, - self._DEF_SEGNO, "invalid retransmit message master SHA") - - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, - self._DEF_MASTER_SHA, 0, "invalid retransmit message segment number") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, - self._DEF_MASTER_SHA, 65536, "invalid retransmit message segment number") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, - self._DEF_MASTER_SHA, None, "invalid retransmit message segment number") - self._test_new_from_parts_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, - self._DEF_MASTER_SHA, "", "invalid retransmit message segment number") - - # Ensure message data is same as we stuff in after object is instantiated - seg = RetransmitSegment.new_from_parts(self._DEF_MSG_SEQ_NUM, self._DEF_MSG_SEQ_NUM, - self._DEF_MASTER_SHA, self._DEF_SEGNO) - assert seg.rt_msg_seq_num() == self._DEF_MSG_SEQ_NUM, "RT message sequence number after segment creation didn't match expected." - assert seg.rt_master_sha() == self._DEF_MASTER_SHA, "RT master SHA after segment creation didn't match expected." - assert seg.rt_segment_number() == self._DEF_SEGNO, "RT segment number after segment creation didn't match expected." - - def _test_new_from_data_fail(self, rt_msg_seq_num, rt_master_sha, rt_segment_number, fail_msg): - try: - payload = struct.pack(RetransmitSegment.data_template(), rt_msg_seq_num, rt_master_sha, rt_segment_number) - payload_sha = _sha_data(payload) - header_template = SegmentBase.header_template() - header = struct.pack(header_template, self._SEG_MAGIC, SegmentBase.type_retransmit(), 1, 1, - self._DEF_MSG_SEQ_NUM, payload_sha) - seg = SegmentBase.new_from_data(self._DEF_ADDRESS, header + payload) - except ValueError, exc: - pass - else: - self.fail("Expected a ValueError about %s." % fail_msg) - - def testNewFromData(self): - """Test DataSegment's new_from_data() functionality.""" - self._test_new_from_data_fail(0, self._DEF_MASTER_SHA, self._DEF_SEGNO, "invalid RT message sequence number") - self._test_new_from_data_fail(65536, self._DEF_MASTER_SHA, self._DEF_SEGNO, "invalid RT message sequence number") - - self._test_new_from_data_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, 0, "invalid RT segment number") - self._test_new_from_data_fail(self._DEF_MSG_SEQ_NUM, self._DEF_MASTER_SHA, 65536, "invalid RT segment number") - - # TODO: Ensure something that should work - - def addToSuite(suite): - suite.addTest(RetransmitSegmentTestCase("testInit")) - suite.addTest(RetransmitSegmentTestCase("testNewFromParts")) - suite.addTest(RetransmitSegmentTestCase("testNewFromData")) - addToSuite = staticmethod(addToSuite) - - class SHAUtilsTestCase(unittest.TestCase): def testSHA(self): data = "235jklqt3hjwasdv879wfe89723rqjh32tr3hwaejksdvd89udsv89dsgiougjktqjhk23tjht23hjt3qhjewagthjasgdgsd" @@ -837,7 +710,6 @@ def main(): suite = unittest.TestSuite() SegmentBaseInitTestCase.addToSuite(suite) DataSegmentTestCase.addToSuite(suite) - RetransmitSegmentTestCase.addToSuite(suite) SHAUtilsTestCase.addToSuite(suite) runner = unittest.TextTestRunner()