indexing description: "Represents an advertisement" license: "MIT license (see ../license.txt)" author: "Beat Strasser " date: "$Date$" revision: "$Revision$" deferred class P2P_ADVERTISEMENT inherit P2P_XML_CACHE redefine initialize end P2P_CREATORS_SHARED undefine out end DT_SHARED_SYSTEM_CLOCK export {NONE} all undefine out end feature -- Access index_elements: DS_LIST [TUPLE [STRING, STRING]] is -- Indexable elements require Adv_valid: is_valid deferred ensure Result_set: Result /= Void end unique_id: STRING is -- Unique id. Void, if advertisement shouldn't/can't be indexed. require Valid: is_valid deferred end expiration_time: INTEGER_64 is -- Remote expiration time in ms (not included in document) local now: DT_DATE_TIME do if lifetime_absolute /= Void then -- hasn't advertisement expired? now := utc_system_clock.date_time_now -- if expiration life time is longer than the life time, calculate max possible expiration Result := possible_expiration_time.min ((lifetime_absolute - now).millisecond_count) if Result < 0 then Result := 0 end else Result := possible_expiration_time end ensure Result_positive: Result >= 0 end lifetime_absolute: DT_DATE_TIME -- Absolute lifetime (not included in document) creation_time: DT_DATE_TIME -- Absolute creation time (not included in document) has_expired: BOOLEAN is -- Has advertisement exceeded its lifetime? do if lifetime_absolute /= Void then Result := utc_system_clock.date_time_now >= lifetime_absolute end end is_newer_than (a_time: DT_DATE_TIME): BOOLEAN is -- Is advertisement newer than the given `a_time'? require Time_valid: a_time /= Void do if creation_time /= Void then Result := creation_time > a_time end end feature -- Element change set_expiration_time (a_time_span: like expiration_time) is -- Set remote expiration `a_time_span' in ms require Time_span_valid: a_time_span >= 0 do possible_expiration_time := a_time_span ensure Time_set: expiration_time <= a_time_span end set_lifetime_relative (a_time_span: like expiration_time) is -- Set lifetime (`a_time_span' in ms) relative to now require Time_valid: a_time_span >= 0 do lifetime_absolute := utc_system_clock.date_time_now lifetime_absolute.add_milliseconds (a_time_span.as_integer_32) ensure Time_set: lifetime_absolute /= Void end set_lifetime_absolute (seconds_since_epoch: INTEGER) is -- Set absolute lifetime (`a_time' milli seconds since epoch) do create lifetime_absolute.make_from_epoch (seconds_since_epoch) ensure Time_set: lifetime_absolute /= Void end set_lifetime_from (other: P2P_ADVERTISEMENT) is -- Set absolute lifetime (`a_time' milli seconds since epoch) require Other_valid: other /= Void and other.lifetime_absolute /= Void do lifetime_absolute := other.lifetime_absolute.twin ensure Time_set: lifetime_absolute /= Void end unset_lifetime is -- Unset the lifetime, make advertisement living forever do lifetime_absolute := Void ensure Time_unset: lifetime_absolute = Void end feature -- Comparison describe_same_identity (other: like Current): BOOLEAN is -- Do `Current' and `other' describe the same identity? require Valid: is_valid Other_valid: other /= Void and other.is_valid do Result := unique_id.is_equal (other.unique_id) end feature {NONE} -- Implementation possible_expiration_time: like expiration_time -- Theoretical possible expiration time (`expiration_time' is restricted to life time) attribute_handler (a_root_attribute: XM_ATTRIBUTE) is -- Advertisements don't have any attributes in the root element do end initialize is -- Set creation time do Precursor creation_time := utc_system_clock.date_time_now possible_expiration_time := {P2P_DISCOVERY_SERVICE}.default_expiration_time end end