Sipper 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/sipper/README.rb +26 -0
- data/sipper/b2bua_controller.rb +163 -0
- data/sipper/b2bua_session_mixin.rb +24 -0
- data/sipper/base_controller.rb +425 -0
- data/sipper/bin/common.rb +42 -0
- data/sipper/bin/generate.rb +70 -0
- data/sipper/bin/project.rb +44 -0
- data/sipper/bin/run.rb +85 -0
- data/sipper/bin/run_smoke.rb +8 -0
- data/sipper/config/log4r.xml +71 -0
- data/sipper/controller_class_loader.rb +29 -0
- data/sipper/controller_selector.rb +119 -0
- data/sipper/controllers/invite_controller.rb +30 -0
- data/sipper/controllers/order.yaml +3 -0
- data/sipper/custom_message.rb +4 -0
- data/sipper/detached_session.rb +11 -0
- data/sipper/docs/manual.txt +1621 -0
- data/sipper/generators/README +12 -0
- data/sipper/generators/gen_controller.rb +228 -0
- data/sipper/generators/gen_project.rb +45 -0
- data/sipper/generators/gen_test.rb +72 -0
- data/sipper/generators/project_template_dir/Rakefile +56 -0
- data/sipper/generators/project_template_dir/config/sipper.cfg +31 -0
- data/sipper/generators/project_template_dir/controllers/README.txt +2 -0
- data/sipper/generators/project_template_dir/dot_sipper.proj +2 -0
- data/sipper/generators/project_template_dir/logs/README.txt +2 -0
- data/sipper/generators/project_template_dir/tests/README.txt +2 -0
- data/sipper/lib/smc/statemap.rb +194 -0
- data/sipper/logs/dialog_info_store +0 -0
- data/sipper/logs/r.cmd +6 -0
- data/sipper/logs/r.sh +6 -0
- data/sipper/media/sipper_media_client.rb +268 -0
- data/sipper/media/sipper_media_event.rb +43 -0
- data/sipper/media/sipper_media_manager.rb +145 -0
- data/sipper/media/sipper_media_proxy.rb +60 -0
- data/sipper/media/sipper_offer_answer.rb +285 -0
- data/sipper/message.rb +512 -0
- data/sipper/modified_pattern_formatter.rb +119 -0
- data/sipper/proxy_controller.rb +143 -0
- data/sipper/registration.rb +52 -0
- data/sipper/request.rb +109 -0
- data/sipper/response.rb +123 -0
- data/sipper/ruby_ext/module.rb +27 -0
- data/sipper/ruby_ext/mutable_class.rb +17 -0
- data/sipper/ruby_ext/object.rb +38 -0
- data/sipper/ruby_ext/pqueue.rb +190 -0
- data/sipper/ruby_ext/snapshot.rb +201 -0
- data/sipper/ruby_ext/string.rb +18 -0
- data/sipper/ruby_ext/time.rb +9 -0
- data/sipper/run/run_sipper1.rb +28 -0
- data/sipper/run/run_sipper2.rb +56 -0
- data/sipper/sdp/sdp.rb +257 -0
- data/sipper/sdp/sdp_generator.rb +131 -0
- data/sipper/sdp/sdp_parser.rb +136 -0
- data/sipper/session.rb +1952 -0
- data/sipper/session_manager.rb +170 -0
- data/sipper/session_recorder.rb +190 -0
- data/sipper/session_state/DialogState.sm +54 -0
- data/sipper/session_state/DialogState_sm.rb +337 -0
- data/sipper/session_state/dialog_routes.rb +141 -0
- data/sipper/sip_headers/header.rb +632 -0
- data/sipper/sip_headers/sipuri.rb +352 -0
- data/sipper/sip_logger.rb +65 -0
- data/sipper/sip_message_router.rb +231 -0
- data/sipper/sip_test_driver_controller.rb +10 -0
- data/sipper/sipper.rb +329 -0
- data/sipper/sipper_assertions.rb +21 -0
- data/sipper/sipper_configurator.rb +376 -0
- data/sipper/sipper_http/sipper_http_request_dispatcher.rb +71 -0
- data/sipper/sipper_http/sipper_http_response.rb +25 -0
- data/sipper/stray_message_manager.rb +40 -0
- data/sipper/test_completion_signaling_helper.rb +77 -0
- data/sipper/transaction/Ict.sm +59 -0
- data/sipper/transaction/Ict_sm.rb +430 -0
- data/sipper/transaction/Ist.sm +74 -0
- data/sipper/transaction/Ist_sm.rb +460 -0
- data/sipper/transaction/Nict.sm +51 -0
- data/sipper/transaction/Nict_sm.rb +325 -0
- data/sipper/transaction/Nist.sm +59 -0
- data/sipper/transaction/Nist_sm.rb +356 -0
- data/sipper/transaction/invite_client_transaction.rb +274 -0
- data/sipper/transaction/invite_server_transaction.rb +319 -0
- data/sipper/transaction/non_invite_client_transaction.rb +230 -0
- data/sipper/transaction/non_invite_server_transaction.rb +263 -0
- data/sipper/transaction/state_machine_wrapper.rb +58 -0
- data/sipper/transaction/transaction.rb +212 -0
- data/sipper/transport/base_transport.rb +84 -0
- data/sipper/transport/rel_unrel.rb +19 -0
- data/sipper/transport/transport_and_route_resolver.rb +67 -0
- data/sipper/transport/udp_transport.rb +156 -0
- data/sipper/transport_manager.rb +33 -0
- data/sipper/udp_session.rb +17 -0
- data/sipper/util/command_element.rb +62 -0
- data/sipper/util/compact_converter.rb +50 -0
- data/sipper/util/counter.rb +26 -0
- data/sipper/util/digest/digest_authorizer.rb +204 -0
- data/sipper/util/expectation_parser.rb +164 -0
- data/sipper/util/locator.rb +31 -0
- data/sipper/util/message_fill.rb +58 -0
- data/sipper/util/persistence/ps_sipper_map.rb +63 -0
- data/sipper/util/persistence/sipper_map.rb +41 -0
- data/sipper/util/sipper_util.rb +305 -0
- data/sipper/util/timer/sip_timer_helper.rb +26 -0
- data/sipper/util/timer/timer_manager.rb +80 -0
- data/sipper/util/timer/timer_task.rb +56 -0
- data/sipper/util/validations.rb +44 -0
- data/sipper/version.rb +10 -0
- data/sipper_test/_test_media_uas.rb +79 -0
- data/sipper_test/base_test_case.rb +31 -0
- data/sipper_test/c_134.txt +7 -0
- data/sipper_test/driven_sip_test_case.rb +96 -0
- data/sipper_test/gold.txt +10 -0
- data/sipper_test/gold_res.txt +8 -0
- data/sipper_test/gold_sub.txt +9 -0
- data/sipper_test/hello_sipper.au +0 -0
- data/sipper_test/in_sipper.au +0 -0
- data/sipper_test/nonrr_proxy.rb +17 -0
- data/sipper_test/order_tests.yaml +4 -0
- data/sipper_test/rake_res.txt +399 -0
- data/sipper_test/rr_proxy.rb +17 -0
- data/sipper_test/run_test.cmd +94 -0
- data/sipper_test/sip_test_case.rb +104 -0
- data/sipper_test/test2xx_retransmission.rb +80 -0
- data/sipper_test/test2xx_retransmission_with_limit.rb +81 -0
- data/sipper_test/test2xx_retransmission_with_nist.rb +91 -0
- data/sipper_test/test2xx_retransmission_with_txns.rb +94 -0
- data/sipper_test/test_address_header.rb +66 -0
- data/sipper_test/test_b2bua1.rb +130 -0
- data/sipper_test/test_b2bua2.rb +120 -0
- data/sipper_test/test_b2bua3.rb +160 -0
- data/sipper_test/test_b2bua4.rb +130 -0
- data/sipper_test/test_base_controller.rb +110 -0
- data/sipper_test/test_base_transport.rb +37 -0
- data/sipper_test/test_cancel.rb +21 -0
- data/sipper_test/test_cancel_after2xx.rb +81 -0
- data/sipper_test/test_cancel_retransmission.rb +105 -0
- data/sipper_test/test_cancel_with481.rb +83 -0
- data/sipper_test/test_cancel_with487.rb +70 -0
- data/sipper_test/test_cancel_with_ist_without_nist.rb +99 -0
- data/sipper_test/test_cancel_with_nist.rb +84 -0
- data/sipper_test/test_cancel_without487.rb +77 -0
- data/sipper_test/test_command_element.rb +38 -0
- data/sipper_test/test_compact_converter.rb +33 -0
- data/sipper_test/test_controller_class_loader.rb +37 -0
- data/sipper_test/test_controller_selector.rb +53 -0
- data/sipper_test/test_controller_using_compact_headers.rb +74 -0
- data/sipper_test/test_controller_using_header_order.rb +85 -0
- data/sipper_test/test_controller_using_ict.rb +64 -0
- data/sipper_test/test_controller_using_ict_with_non_success.rb +72 -0
- data/sipper_test/test_controller_using_ict_with_tcbh.rb +24 -0
- data/sipper_test/test_controller_using_ict_with_tcbh_no_action.rb +24 -0
- data/sipper_test/test_controller_using_ist.rb +70 -0
- data/sipper_test/test_controller_using_ist_with_tcbh.rb +24 -0
- data/sipper_test/test_controller_using_nict.rb +115 -0
- data/sipper_test/test_controller_using_nict_with_tcbh.rb +24 -0
- data/sipper_test/test_controller_using_nist.rb +63 -0
- data/sipper_test/test_controller_using_pre_existing_rs0.rb +73 -0
- data/sipper_test/test_controller_using_pre_existing_rs1.rb +75 -0
- data/sipper_test/test_controller_using_pre_existing_rs2.rb +71 -0
- data/sipper_test/test_controller_using_route_set.rb +86 -0
- data/sipper_test/test_controller_with_sdp.rb +80 -0
- data/sipper_test/test_controllers/cancel/order.yaml +2 -0
- data/sipper_test/test_controllers/cancel/uac_cancel_controller.rb +35 -0
- data/sipper_test/test_controllers/cancel/uas_cancel_controller.rb +25 -0
- data/sipper_test/test_controllers/class_loading/ordered/first_ordered_controller.rb +5 -0
- data/sipper_test/test_controllers/class_loading/ordered/order.yaml +3 -0
- data/sipper_test/test_controllers/class_loading/ordered/recond_ordered_controller.rb +6 -0
- data/sipper_test/test_controllers/class_loading/ordered/second_ordered_controller.rb +6 -0
- data/sipper_test/test_controllers/class_loading/unordered/first_unordered_controller.rb +7 -0
- data/sipper_test/test_controllers/class_loading/unordered/second_unordered_controller.rb +6 -0
- data/sipper_test/test_controllers/ctrl_trhandler/lib/transport_filters/my_transport_handler.rb +22 -0
- data/sipper_test/test_controllers/ctrl_trhandler/uac_tr_handler_controller.rb +27 -0
- data/sipper_test/test_controllers/ctrl_trhandler/uas_tr_handler_controller.rb +21 -0
- data/sipper_test/test_controllers/ete/order.yaml +1 -0
- data/sipper_test/test_controllers/ete/uac_controller.rb +39 -0
- data/sipper_test/test_controllers/ete/uas_controller.rb +34 -0
- data/sipper_test/test_controllers/extensions/extension_uac_controller.rb +24 -0
- data/sipper_test/test_controllers/extensions/extension_uas_controller.rb +35 -0
- data/sipper_test/test_controllers/extensions/lib/sipper_extensions/my_from_extension.rb +16 -0
- data/sipper_test/test_controllers/ict_tcbh/lib/transaction_handlers/app_ict_handler.rb +23 -0
- data/sipper_test/test_controllers/ict_tcbh/uac_ict_tcbh_controller.rb +27 -0
- data/sipper_test/test_controllers/ict_tcbh/uac_ict_tcbh_no_action_controller.rb +28 -0
- data/sipper_test/test_controllers/ict_tcbh/uas_ict_tcbh_controller.rb +29 -0
- data/sipper_test/test_controllers/ict_tcbh/uas_ict_tcbh_no_action_controller.rb +31 -0
- data/sipper_test/test_controllers/ist_tcbh/lib/app_ist_handler.rb +13 -0
- data/sipper_test/test_controllers/ist_tcbh/uac_ist_tcbh_controller.rb +22 -0
- data/sipper_test/test_controllers/ist_tcbh/uas_ist_tcbh_controller.rb +31 -0
- data/sipper_test/test_controllers/multi_trhandlers/lib/transport_filters/in_order.yaml +2 -0
- data/sipper_test/test_controllers/multi_trhandlers/lib/transport_filters/my_transport_handler1.rb +21 -0
- data/sipper_test/test_controllers/multi_trhandlers/lib/transport_filters/my_transport_handler2.rb +21 -0
- data/sipper_test/test_controllers/multi_trhandlers/lib/transport_filters/out_order.yaml +2 -0
- data/sipper_test/test_controllers/multi_trhandlers/uac_multi_tr_handler_controller.rb +27 -0
- data/sipper_test/test_controllers/multi_trhandlers/uas_multi_tr_handler_controller.rb +29 -0
- data/sipper_test/test_controllers/multiple/lib/blank_test.rb +2 -0
- data/sipper_test/test_controllers/multiple/uac_info_controller.rb +21 -0
- data/sipper_test/test_controllers/multiple/uac_msg_controller.rb +20 -0
- data/sipper_test/test_controllers/multiple/uas_info_controller.rb +15 -0
- data/sipper_test/test_controllers/multiple/uas_msg_controller.rb +14 -0
- data/sipper_test/test_controllers/nict_tcbh/lib/transaction_handlers/app_nict_handler.rb +13 -0
- data/sipper_test/test_controllers/nict_tcbh/uac_nict_tcbh_controller.rb +26 -0
- data/sipper_test/test_controllers/nict_tcbh/uas_nict_tcbh_controller.rb +20 -0
- data/sipper_test/test_controllers/state_machine_based/lib/CreditControl.sm +43 -0
- data/sipper_test/test_controllers/state_machine_based/lib/CreditControl_sm.rb +194 -0
- data/sipper_test/test_controllers/state_machine_based/order.yaml +1 -0
- data/sipper_test/test_controllers/state_machine_based/uac_message_controller.rb +34 -0
- data/sipper_test/test_controllers/state_machine_based/uas_message_controller.rb +44 -0
- data/sipper_test/test_controllers/stray_message/lib/sipper_extensions/my_stray_handler.rb +9 -0
- data/sipper_test/test_controllers/stray_message/stray_uac_controller.rb +24 -0
- data/sipper_test/test_controllers/stray_message/stray_uas_controller.rb +31 -0
- data/sipper_test/test_controllers/string/order.yaml +1 -0
- data/sipper_test/test_controllers/string/uac_controller.rb +29 -0
- data/sipper_test/test_controllers/string/uas_controller.rb +22 -0
- data/sipper_test/test_controllers/test_controller.rb +24 -0
- data/sipper_test/test_detached_session1.rb +78 -0
- data/sipper_test/test_dialog_routes.rb +139 -0
- data/sipper_test/test_digest_challenge1.rb +89 -0
- data/sipper_test/test_digest_challenge2.rb +90 -0
- data/sipper_test/test_dynamic_parse.rb +249 -0
- data/sipper_test/test_empty_sdp.rb +89 -0
- data/sipper_test/test_ete.rb +37 -0
- data/sipper_test/test_expectation_parser.rb +76 -0
- data/sipper_test/test_extensions.rb +28 -0
- data/sipper_test/test_freeze.rb +81 -0
- data/sipper_test/test_generated.rb +90 -0
- data/sipper_test/test_header_parameters.rb +75 -0
- data/sipper_test/test_header_parse.rb +141 -0
- data/sipper_test/test_http_client1.rb +84 -0
- data/sipper_test/test_http_client2.rb +90 -0
- data/sipper_test/test_ict_with_timeout.rb +62 -0
- data/sipper_test/test_in_dialog_request.rb +61 -0
- data/sipper_test/test_inline_controller.rb +67 -0
- data/sipper_test/test_invite_client_transaction.rb +272 -0
- data/sipper_test/test_invite_replace.rb +147 -0
- data/sipper_test/test_invite_retransmission.rb +90 -0
- data/sipper_test/test_invite_server_transaction.rb +208 -0
- data/sipper_test/test_lower_cseq.rb +79 -0
- data/sipper_test/test_media.rb +52 -0
- data/sipper_test/test_message.rb +392 -0
- data/sipper_test/test_method_specific_response_handling.rb +81 -0
- data/sipper_test/test_multi_homed1.rb +127 -0
- data/sipper_test/test_multi_homed2.rb +109 -0
- data/sipper_test/test_multi_homed_duplicate.rb +87 -0
- data/sipper_test/test_multi_homed_with_detached.rb +88 -0
- data/sipper_test/test_multiple.rb +49 -0
- data/sipper_test/test_multiple_contacts.rb +89 -0
- data/sipper_test/test_non2xx_retransmission_with_ist.rb +87 -0
- data/sipper_test/test_non_invite_client_transaction.rb +210 -0
- data/sipper_test/test_non_invite_retransmission.rb +91 -0
- data/sipper_test/test_non_invite_server_transaction.rb +202 -0
- data/sipper_test/test_offer_answer_prack.rb +103 -0
- data/sipper_test/test_pickup.rb +258 -0
- data/sipper_test/test_prack.rb +105 -0
- data/sipper_test/test_prack_store_and_dispatch.rb +101 -0
- data/sipper_test/test_prack_timer.rb +105 -0
- data/sipper_test/test_ps_sipper_map.rb +56 -0
- data/sipper_test/test_re_invite_uas_ongoing_ict.rb +81 -0
- data/sipper_test/test_re_invite_uas_ongoing_ist.rb +84 -0
- data/sipper_test/test_re_invite_with_ongoing_ict.rb +101 -0
- data/sipper_test/test_re_invite_with_ongoing_ist.rb +89 -0
- data/sipper_test/test_recorder_swap.rb +157 -0
- data/sipper_test/test_refer.rb +121 -0
- data/sipper_test/test_registeration_clearing.rb +97 -0
- data/sipper_test/test_registrar.rb +109 -0
- data/sipper_test/test_registration_controller.rb +73 -0
- data/sipper_test/test_registration_timeout.rb +90 -0
- data/sipper_test/test_remote_controller.rb +39 -0
- data/sipper_test/test_remote_target_update_on2xx.rb +140 -0
- data/sipper_test/test_request.rb +106 -0
- data/sipper_test/test_response.rb +40 -0
- data/sipper_test/test_response_without_to_tag.rb +92 -0
- data/sipper_test/test_rport.rb +88 -0
- data/sipper_test/test_sdp.rb +91 -0
- data/sipper_test/test_sdp_parser.rb +145 -0
- data/sipper_test/test_session.rb +276 -0
- data/sipper_test/test_session_callback_handler.rb +68 -0
- data/sipper_test/test_session_lifetime.rb +66 -0
- data/sipper_test/test_session_manager.rb +141 -0
- data/sipper_test/test_session_recorder.rb +145 -0
- data/sipper_test/test_session_state_user_defined.rb +84 -0
- data/sipper_test/test_session_states.rb +91 -0
- data/sipper_test/test_simple_dialog_state.rb +86 -0
- data/sipper_test/test_simple_re_invite.rb +86 -0
- data/sipper_test/test_sip_uri.rb +234 -0
- data/sipper_test/test_sipper_util.rb +45 -0
- data/sipper_test/test_smc_controller.rb +17 -0
- data/sipper_test/test_smoke.rb +80 -0
- data/sipper_test/test_stray.rb +28 -0
- data/sipper_test/test_stray_inline.rb +89 -0
- data/sipper_test/test_stray_res_acked.rb +103 -0
- data/sipper_test/test_stray_respond.rb +104 -0
- data/sipper_test/test_stray_retry.rb +92 -0
- data/sipper_test/test_stray_retry_failure.rb +93 -0
- data/sipper_test/test_stray_retry_initial.rb +100 -0
- data/sipper_test/test_strict_router_with_angled.rb +84 -0
- data/sipper_test/test_string_record.rb +31 -0
- data/sipper_test/test_subscribe_notify.rb +141 -0
- data/sipper_test/test_subscribe_notify_client_timeout.rb +158 -0
- data/sipper_test/test_subscribe_notify_dialog.rb +146 -0
- data/sipper_test/test_subscribe_notify_expires.rb +155 -0
- data/sipper_test/test_subscribe_notify_multiple_subscription.rb +157 -0
- data/sipper_test/test_subscribe_notify_server_timeout.rb +144 -0
- data/sipper_test/test_subsequent_unsent.rb +88 -0
- data/sipper_test/test_target_refresh_proxy_detached.rb +128 -0
- data/sipper_test/test_target_refresh_proxy_udp.rb +130 -0
- data/sipper_test/test_target_refresh_rr_proxy_udp.rb +133 -0
- data/sipper_test/test_target_refresh_with_new_port.rb +158 -0
- data/sipper_test/test_target_refresh_with_proxy1.rb +113 -0
- data/sipper_test/test_target_refresh_with_rr_update_proxy.rb +145 -0
- data/sipper_test/test_target_refresh_with_update_proxy.rb +144 -0
- data/sipper_test/test_target_refresh_with_update_proxy2.rb +139 -0
- data/sipper_test/test_timer_manager.rb +71 -0
- data/sipper_test/test_timer_task.rb +60 -0
- data/sipper_test/test_transport_handler.rb +19 -0
- data/sipper_test/test_transport_multi_handler.rb +18 -0
- data/sipper_test/test_udp_transport.rb +119 -0
- data/sipper_test/test_unparsed.rb +70 -0
- data/sipper_test/test_validations.rb +69 -0
- data/sipper_test/test_with_rr_proxy.rb +111 -0
- data/sipper_test/testmediacontroller.rb +71 -0
- data/sipper_test/tracing.rb +23 -0
- data/sipper_test/transaction_test_helper.rb +176 -0
- data/sipper_test/transport_filters.rb +26 -0
- data/sipper_test/ts_sipper.rb +91 -0
- data/sipper_test/ts_smoke.rb +3 -0
- data/sipper_test/tt.cmd +20 -0
- metadata +455 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
# Extension for finding class names from a string with class definition
|
2
|
+
class Module
|
3
|
+
def all_class_names
|
4
|
+
class_names = []
|
5
|
+
constants.each do |const_name|
|
6
|
+
const = const_get(const_name)
|
7
|
+
case const
|
8
|
+
when Class
|
9
|
+
class_names << const.to_s.split(/::/,2)[1]
|
10
|
+
when Module
|
11
|
+
class_names += const.all_class_names
|
12
|
+
end
|
13
|
+
end
|
14
|
+
class_names.uniq
|
15
|
+
end
|
16
|
+
|
17
|
+
# Extension to allow for snap_fields directive in the class of which snapshot is to be
|
18
|
+
# taken. This creates a method by name __snap_fields used by snapshot.rb in the class
|
19
|
+
# being snapshotted.
|
20
|
+
def snap_fields(*symbols)
|
21
|
+
str = "["
|
22
|
+
symbols.each {|s| str << ":" << s.to_s << "," }
|
23
|
+
str << "]"
|
24
|
+
module_eval("def __snap_fields() #{str}; end")
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
# Extension to flatten the classname to make it amenable
|
3
|
+
# to logging.
|
4
|
+
|
5
|
+
class Object
|
6
|
+
def class_name
|
7
|
+
begin
|
8
|
+
self.classname
|
9
|
+
rescue NoMethodError
|
10
|
+
name = self.class.name.split("::").join("_")
|
11
|
+
self.class.class_eval do
|
12
|
+
define_method(:classname) {
|
13
|
+
name
|
14
|
+
}
|
15
|
+
end
|
16
|
+
retry
|
17
|
+
end #rescue
|
18
|
+
end #method
|
19
|
+
|
20
|
+
|
21
|
+
def is_request?
|
22
|
+
self.class == Request
|
23
|
+
end
|
24
|
+
|
25
|
+
def is_response?
|
26
|
+
self.class == Response
|
27
|
+
end
|
28
|
+
|
29
|
+
# clears the given instance variables as symbols arrays
|
30
|
+
# e.g o.clear_ivs([:@a, :@b]) sets the instance variable @a and @b to nil
|
31
|
+
# in the object o.
|
32
|
+
def clear_ivs(arr)
|
33
|
+
arr.each do |i|
|
34
|
+
instance_variable_set(i, nil)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
# = pqueue.rb
|
2
|
+
#
|
3
|
+
# == Copyright (c) 2005 K.Kodama
|
4
|
+
#
|
5
|
+
# GNU General Public License
|
6
|
+
#
|
7
|
+
# This program is free software; you can redistribute it and/or modify
|
8
|
+
# it under the terms of the GNU General Public License as published by
|
9
|
+
# the Free Software Foundation; either version 2 of the License, or (at
|
10
|
+
# your option) any later version.
|
11
|
+
#
|
12
|
+
# This program is distributed in the hope that it will be useful, but
|
13
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
14
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15
|
+
# General Public License for more details.
|
16
|
+
#
|
17
|
+
# You should have received a copy of the GNU General Public License
|
18
|
+
# along with this program; if not, write to the Free Software
|
19
|
+
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
20
|
+
#
|
21
|
+
# == Special Thanks
|
22
|
+
#
|
23
|
+
# Rick Bradley 2003/02/02, patch for Ruby 1.6.5. Thank you!
|
24
|
+
#
|
25
|
+
# == Author(s)
|
26
|
+
#
|
27
|
+
# * K.Kodama
|
28
|
+
|
29
|
+
# Author:: K.Kodama
|
30
|
+
# Copyright:: Copyright (c) 2005 K.Kodama
|
31
|
+
# License:: Ruby License
|
32
|
+
|
33
|
+
# = PQueue
|
34
|
+
#
|
35
|
+
# Priority queue with array based heap.
|
36
|
+
|
37
|
+
class PQueue
|
38
|
+
|
39
|
+
attr_accessor :qarray # format: [nil, e1, e2, ..., en]
|
40
|
+
attr_reader :size # number of elements
|
41
|
+
attr_reader :gt # compareProc
|
42
|
+
|
43
|
+
def initialize(compareProc=lambda{|x,y| x>y})
|
44
|
+
# By default, retrieves maximal elements first.
|
45
|
+
@qarray=[nil]; @size=0; @gt=compareProc; make_legal
|
46
|
+
end
|
47
|
+
private :initialize
|
48
|
+
|
49
|
+
def upheap(k)
|
50
|
+
k2=k.div(2); v=@qarray[k];
|
51
|
+
while ((k2>0)and(@gt[v,@qarray[k2]]));
|
52
|
+
@qarray[k]=@qarray[k2]; k=k2; k2=k2.div(2)
|
53
|
+
end;
|
54
|
+
@qarray[k]=v;
|
55
|
+
end
|
56
|
+
private :upheap
|
57
|
+
|
58
|
+
def downheap(k)
|
59
|
+
v=@qarray[k]; q2=@size.div(2)
|
60
|
+
loop{
|
61
|
+
if (k>q2); break; end;
|
62
|
+
j=k+k; if ((j<@size)and(@gt[@qarray[j+1],@qarray[j]])); j=j+1; end;
|
63
|
+
if @gt[v,@qarray[j]]; break; end;
|
64
|
+
@qarray[k]=@qarray[j]; k=j;
|
65
|
+
}
|
66
|
+
@qarray[k]=v;
|
67
|
+
end;
|
68
|
+
private :downheap
|
69
|
+
|
70
|
+
def make_legal
|
71
|
+
for k in 2..@size do; upheap(k); end;
|
72
|
+
end;
|
73
|
+
|
74
|
+
def empty?
|
75
|
+
return (0==@size)
|
76
|
+
end
|
77
|
+
|
78
|
+
def clear
|
79
|
+
@qarray.replace([nil]); @size=0;
|
80
|
+
end;
|
81
|
+
|
82
|
+
def replace_array(arr=[])
|
83
|
+
# Use push_array.
|
84
|
+
@qarray.replace([nil]+arr); @size=arr.size; make_legal
|
85
|
+
end;
|
86
|
+
|
87
|
+
def clone
|
88
|
+
q=new; q.qarray=@qarray.clone; q.size=@size; q.gt=@gt; return q;
|
89
|
+
end;
|
90
|
+
|
91
|
+
def push(v)
|
92
|
+
@size=@size+1; @qarray[@size]=v; upheap(@size);
|
93
|
+
end;
|
94
|
+
|
95
|
+
def push_array(arr=[])
|
96
|
+
@qarray[@size+1,arr.size]=arr; arr.size.times{@size+=1; upheap(@size)}
|
97
|
+
end;
|
98
|
+
|
99
|
+
def pop
|
100
|
+
# return top element. nil if queue is empty.
|
101
|
+
if @size>0;
|
102
|
+
res=@qarray[1]; @qarray[1]=@qarray[@size]; @size=@size-1;
|
103
|
+
downheap(1);
|
104
|
+
return res;
|
105
|
+
else return nil
|
106
|
+
end;
|
107
|
+
end;
|
108
|
+
|
109
|
+
def pop_array(n=@size)
|
110
|
+
# return top n-element as an sorted array. (i.e. The obtaining array is decreasing order.)
|
111
|
+
# See also to_a.
|
112
|
+
a=[]
|
113
|
+
n.times{a.push(pop)}
|
114
|
+
return a
|
115
|
+
end;
|
116
|
+
|
117
|
+
def to_a
|
118
|
+
# array sorted as increasing order.
|
119
|
+
# See also pop_array.
|
120
|
+
res=@qarray[1..@size];
|
121
|
+
res.sort!{|x,y| if @gt[x,y]; 1;elsif @gt[y,x]; -1; else 0; end;}
|
122
|
+
return res
|
123
|
+
end
|
124
|
+
|
125
|
+
def top
|
126
|
+
# top element. not destructive.
|
127
|
+
if @size>0; return @qarray[1]; else return nil; end;
|
128
|
+
end;
|
129
|
+
|
130
|
+
def replace_top_low(v)
|
131
|
+
# replace top element if v<top element.
|
132
|
+
if @size>0; @qarray[0]=v; downheap(0); return @qarray[0];
|
133
|
+
else @qarray[1]=v; return nil;
|
134
|
+
end;
|
135
|
+
end;
|
136
|
+
|
137
|
+
def replace_top(v)
|
138
|
+
# replace top element
|
139
|
+
if @size>0; res=@qarray[1]; @qarray[1]=v; downheap(1); return res;
|
140
|
+
else @qarray[1]=v; return nil;
|
141
|
+
end;
|
142
|
+
end;
|
143
|
+
|
144
|
+
def each_pop
|
145
|
+
# iterate pop. destructive. Use as self.each_pop{|x| ... }.
|
146
|
+
while(@size>0); yield self.pop; end;
|
147
|
+
end;
|
148
|
+
|
149
|
+
def each_with_index
|
150
|
+
# Not ordered. Use as self.each_with_index{|e,i| ... }.
|
151
|
+
for i in 1..@size do; yield @qarray[i],i; end;
|
152
|
+
end
|
153
|
+
|
154
|
+
end # class PQueue
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
# _____ _
|
159
|
+
# |_ _|__ ___| |_
|
160
|
+
# | |/ _ \/ __| __|
|
161
|
+
# | | __/\__ \ |_
|
162
|
+
# |_|\___||___/\__|
|
163
|
+
#
|
164
|
+
|
165
|
+
=begin test
|
166
|
+
|
167
|
+
require 'test/unit'
|
168
|
+
|
169
|
+
# TODO Expand on these tests.
|
170
|
+
|
171
|
+
class TC01 < Test::Unit::TestCase
|
172
|
+
|
173
|
+
def setup
|
174
|
+
@pq=PQueue.new(proc{|x,y| x>y})
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_01
|
178
|
+
@pq.push(2)
|
179
|
+
@pq.push(3)
|
180
|
+
@pq.push(4)
|
181
|
+
@pq.push(3)
|
182
|
+
@pq.push(2)
|
183
|
+
@pq.push(4)
|
184
|
+
@pq.push_array([3,5,4])
|
185
|
+
assert_equal( 9, @pq.size )
|
186
|
+
assert_equal( [2, 2, 3, 3, 3, 4, 4, 4, 5], @pq.to_a )
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
=end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
# = snapshot.rb
|
2
|
+
#
|
3
|
+
# == Copyright (c) 2004 Michael Neumann
|
4
|
+
#
|
5
|
+
# Ruby License
|
6
|
+
#
|
7
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
8
|
+
# software under the same terms as Ruby.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
11
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
12
|
+
# FOR A PARTICULAR PURPOSE.
|
13
|
+
#
|
14
|
+
# == Author(s)
|
15
|
+
#
|
16
|
+
# * Michael Neumann
|
17
|
+
#
|
18
|
+
# == Developer Notes
|
19
|
+
#
|
20
|
+
# TODO Perhaps extend to offer multiple depths.
|
21
|
+
#
|
22
|
+
# TODO Should key consitancy be enforced? Currently
|
23
|
+
# Struct's will have symobl keys while other classes
|
24
|
+
# will have string keys in the form of "@name".
|
25
|
+
#
|
26
|
+
# TODO Add other core classes.
|
27
|
+
#
|
28
|
+
# TODO Convert to Kernel#to_data ?
|
29
|
+
|
30
|
+
# Author:: Michael Neumann
|
31
|
+
# Copyright:: Copyright (c) 2004 Michael Neumann
|
32
|
+
# License:: Ruby License
|
33
|
+
|
34
|
+
# = Snapshot
|
35
|
+
#
|
36
|
+
# A lightweight single-depth object state capture.
|
37
|
+
# The #take_snapshot method reads the object's state,
|
38
|
+
# which is generally it's collection of instance variables,
|
39
|
+
# and returns them in a hash. The state can be restored
|
40
|
+
# with #apply_snapshot.
|
41
|
+
#
|
42
|
+
# == Usage
|
43
|
+
#
|
44
|
+
# Customer = Struct.new("Customer", :name, :address, :zip)
|
45
|
+
# joe = Customer.new( "Joe Pitare", "1115 Lila Ln.", 47634 )
|
46
|
+
#
|
47
|
+
# # simple transactions
|
48
|
+
# joe_snap = joe.take_snapshot
|
49
|
+
# begin
|
50
|
+
# do_something_with( joe )
|
51
|
+
# rescue
|
52
|
+
# joe.apply_snapshot( joe_snap )
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# joe_snap[:name] => "Joe Pitare"
|
56
|
+
# joe_snap[:address] => "1115 Lila Ln."
|
57
|
+
# joe_snap[:zip] => 47634
|
58
|
+
#
|
59
|
+
# == Details
|
60
|
+
#
|
61
|
+
# Class Snapshot simply represents a collection of objects from
|
62
|
+
# which snapshots were taken via their methods #take_snapshot.
|
63
|
+
# It provides methods to add an object to a snapshot
|
64
|
+
# (Snapshot#add) as well as to restore all objects
|
65
|
+
# of the snapshot to their state stored in the snapshot (method
|
66
|
+
# Snapshot#restore).
|
67
|
+
#
|
68
|
+
# In Wee, this class is used to backtracking the state of
|
69
|
+
# components (or decorations/presenters). Components that want
|
70
|
+
# an undo-facility to be implemented (triggered for example by
|
71
|
+
# a browsers back-button), have to overwrite the
|
72
|
+
# Wee::Component#backtrack_state method.
|
73
|
+
|
74
|
+
class Snapshot
|
75
|
+
def initialize
|
76
|
+
@objects = Hash.new
|
77
|
+
end
|
78
|
+
|
79
|
+
def add(object)
|
80
|
+
oid = object.object_id
|
81
|
+
@objects[oid] = [object, object.take_snapshot] unless @objects.include?(oid)
|
82
|
+
end
|
83
|
+
|
84
|
+
def restore
|
85
|
+
@objects.each_value { |object, value| object.restore_snapshot(value) }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Simplist form of a snapshot.
|
90
|
+
#
|
91
|
+
#module SnapshotMixin
|
92
|
+
# def take_snapshot() dup end
|
93
|
+
# def restore_snapshot(snap) replace(snap) end
|
94
|
+
#end
|
95
|
+
|
96
|
+
# Implements a value holder. In Wee this is useful for
|
97
|
+
# backtracking the reference assigned to an instance variable
|
98
|
+
# (not the object itself!). An example where this is used is the
|
99
|
+
# <tt>@__decoration</tt> attribute of class Wee::Component.
|
100
|
+
|
101
|
+
class Snapshot::ValueHolder
|
102
|
+
attr_accessor :value
|
103
|
+
|
104
|
+
def initialize(value=nil)
|
105
|
+
@value = value
|
106
|
+
end
|
107
|
+
|
108
|
+
def take_snapshot
|
109
|
+
@value
|
110
|
+
end
|
111
|
+
|
112
|
+
def restore_snapshot(value)
|
113
|
+
@value = value
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
#--
|
119
|
+
# Extend some base classes of Ruby (Object, Array, String, Hash,
|
120
|
+
# Struct) for the two methods #take_snapshot and
|
121
|
+
# #restore_snapshot, required by Snapshot.
|
122
|
+
#++
|
123
|
+
|
124
|
+
class Object
|
125
|
+
def take_snapshot
|
126
|
+
snap = Hash.new
|
127
|
+
fields = self.__snap_fields if self.respond_to? :__snap_fields
|
128
|
+
fields = instance_variables unless fields
|
129
|
+
fields.each do |iv|
|
130
|
+
snap[iv] = instance_variable_get(iv)
|
131
|
+
end
|
132
|
+
snap
|
133
|
+
end
|
134
|
+
|
135
|
+
def restore_snapshot(snap)
|
136
|
+
fields = self.__snap_fields if self.respond_to? :__snap_fields
|
137
|
+
fields = instance_variables unless fields
|
138
|
+
fields.each do |iv|
|
139
|
+
instance_variable_set(iv, snap[iv])
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class Array
|
145
|
+
def take_snapshot() dup end
|
146
|
+
def restore_snapshot(snap) replace(snap) end
|
147
|
+
end
|
148
|
+
|
149
|
+
class String
|
150
|
+
def take_snapshot() dup end
|
151
|
+
def restore_snapshot(snap) replace(snap) end
|
152
|
+
end
|
153
|
+
|
154
|
+
class Hash
|
155
|
+
def take_snapshot() dup end
|
156
|
+
def restore_snapshot(snap) replace(snap) end
|
157
|
+
end
|
158
|
+
|
159
|
+
class Struct
|
160
|
+
def take_snapshot
|
161
|
+
snap = Hash.new
|
162
|
+
each_pair {|k,v| snap[k] = v}
|
163
|
+
snap
|
164
|
+
end
|
165
|
+
|
166
|
+
def restore_snapshot(snap)
|
167
|
+
snap.each_pair {|k,v| send(k.to_s + "=", v)}
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
# _____ _
|
174
|
+
# |_ _|__ ___| |_
|
175
|
+
# | |/ _ \/ __| __|
|
176
|
+
# | | __/\__ \ |_
|
177
|
+
# |_|\___||___/\__|
|
178
|
+
#
|
179
|
+
|
180
|
+
=begin testing
|
181
|
+
|
182
|
+
require 'test/unit'
|
183
|
+
|
184
|
+
class TC_Snapshot < Test::Unit::TestCase
|
185
|
+
|
186
|
+
def setup
|
187
|
+
customer = Struct.new("Customer", :name, :address, :zip)
|
188
|
+
joe = customer.new( "Joe Pitare", "1115 Lila Ln.", 47634 )
|
189
|
+
@joe_snap = joe.take_snapshot
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_storage
|
193
|
+
assert_equal( "Joe Pitare", @joe_snap[:name] )
|
194
|
+
assert_equal( "1115 Lila Ln.", @joe_snap[:address] )
|
195
|
+
assert_equal( 47634, @joe_snap[:zip] )
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
=end
|
201
|
+
|