Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Advertisement.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_UPnP_SSDP_Advertisement_h_
5#define _Stroika_Frameworks_UPnP_SSDP_Advertisement_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
9#include <compare>
10#include <optional>
11
13#include "Stroika/Foundation/Common/Common.h"
14#include "Stroika/Foundation/Containers/Mapping.h"
15#include "Stroika/Foundation/DataExchange/ObjectVariantMapper.h"
16#include "Stroika/Foundation/Execution/LazyInitialized.h"
18#include "Stroika/Foundation/IO/Network/SocketAddress.h"
22
23/**
24 * \file
25 *
26 * See http://quimby.gnus.org/internet-drafts/draft-cai-ssdp-v1-03.txt
27 * for details on the SSDP specification.
28 *
29 * And http://www.upnp-hacks.org/upnp.html for more hints.
30 */
31namespace Stroika::Frameworks::UPnP::SSDP {
32
33 using Foundation::Characters::String;
34 using Foundation::Containers::Mapping;
35 using Foundation::IO::Network::URI;
36 using Foundation::Memory::BLOB;
37
38 /**
39 */
40 struct Advertisement {
41 optional<bool> fAlive; // else Bye notification, or empty if neither
42 String fUSN;
43 URI fLocation;
44 String fServer;
45 String fTarget; // usually ST header (or NT for notify)
46 Mapping<String, String> fRawHeaders;
47 optional<Foundation::Time::Duration> fMaxAge; // CACHE-CONTROL max-age: how long the advertisement is good for (Serialize writes kDefaultMaxAge if missing)
48
49 /**
50 * The network it arrived on - its interface's Interface::fInterfaceID - as a Client::Listener or Client::Search heard it
51 * (nullopt: not known). Not part of the message: Serialize ignores it, and DeSerialize leaves it nullopt.
52 */
53 optional<Foundation::IO::Network::Interface::SystemIDType> fReceivedOn;
54
55 bool operator== (const Advertisement&) const = default;
56
57 /**
58 * Mapper to facilitate serialization
59 */
60 static const Foundation::Execution::LazyInitialized<Foundation::DataExchange::ObjectVariantMapper> kMapper;
61
62 /**
63 * @see Characters::ToString ();
64 */
65 nonvirtual String ToString () const;
66
67 private:
68 static Foundation::DataExchange::ObjectVariantMapper kMapperGetter_ ();
69 };
70 inline const Foundation::Execution::LazyInitialized<Foundation::DataExchange::ObjectVariantMapper> Advertisement::kMapper{Advertisement::kMapperGetter_};
71
72 /**
73 */
74 static const String kTarget_UPNPRootDevice{"upnp:rootdevice"sv};
75
76 /**
77 */
78 static const String kTarget_SSDPAll{"ssdp:all"sv};
79
80 /**
81 * The CACHE-CONTROL max-age Serialize writes when an Advertisement has no fMaxAge. The UPnP Device Architecture says it
82 * SHOULD be at least 1800 seconds.
83 */
84 inline constexpr Foundation::Time::DurationSeconds kDefaultMaxAge{1800.0};
85
86 /**
87 */
88 enum class SearchOrNotify {
89 SearchResponse,
90 Notify
91 };
92
93 /**
94 * \brief The SSDP packet saying ad: a NOTIFY (ssdp:alive - or ssdp:byebye, given fAlive false), or an answer to a search
95 * (headLine "HTTP/1.1 200 OK")
96 *
97 * Each with the headers the UPnP Device Architecture gives it: a NOTIFY's HOST is notifyGroup - the multicast group it goes
98 * to, V4::kSocketAddress or V6::kSocketAddress (IPv4's if not given); an ssdp:byebye has no CACHE-CONTROL, LOCATION or
99 * SERVER; a search answer has EXT and DATE (now, as an HTTP date), and no HOST or NTS.
100 */
101 BLOB Serialize (const String& headLine, SearchOrNotify searchOrNotify, const Advertisement& ad,
102 const optional<Foundation::IO::Network::SocketAddress>& notifyGroup = nullopt);
103
104 /**
105 * \brief Parse any SSDP packet - a NOTIFY, an M-SEARCH, or a search response ("HTTP/1.1 200 OK")
106 *
107 * *headLine is the packet's first line - which says which of those it is; the caller checks it. fTarget is the NT header
108 * (a NOTIFY) or the ST header (a search or search response), fAlive the NTS header, and every header lands in fRawHeaders
109 * too. A Location that does not parse as a URI is treated as missing (traced, not thrown).
110 */
111 void DeSerialize (const BLOB& b, String* headLine, Advertisement* advertisement);
112
113}
114
115/*
116 ********************************************************************************
117 ***************************** Implementation Details ***************************
118 ********************************************************************************
119 */
120#include "Advertisement.inl"
121
122#endif /*_Stroika_Frameworks_UPnP_SSDP_Advertisement_h_*/
chrono::duration< double > DurationSeconds
chrono::duration<double> - a time span (length of time) measured in seconds, but high precision.
Definition Realtime.h:57
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
STRING_TYPE ToString(FLOAT_TYPE f, const ToStringOptions &options={})