Files
PrimAITE/src/primaite/simulator/network/protocols/ntp.py

37 lines
1.0 KiB
Python
Raw Normal View History

2025-01-03 14:40:00 +00:00
# © Crown-owned copyright 2025, Defence Science and Technology Laboratory UK
2023-11-13 17:39:27 +00:00
from __future__ import annotations
2023-11-15 10:57:56 +00:00
from datetime import datetime
2023-11-13 17:39:27 +00:00
from typing import Optional
2023-11-15 10:57:56 +00:00
2023-11-13 17:39:27 +00:00
from pydantic import BaseModel
2023-11-15 10:57:56 +00:00
2023-11-13 17:39:27 +00:00
from primaite.simulator.network.protocols.packet import DataPacket
class NTPReply(BaseModel):
"""Represents a NTP Reply packet."""
ntp_datetime: datetime
"NTP datetime object set by NTP Server."
2023-11-13 17:39:27 +00:00
class NTPPacket(DataPacket):
"""
Represents the NTP layer of a network frame.
:param ntp_request: NTPRequest packet from NTP client.
:param ntp_reply: NTPReply packet from NTP Server.
"""
ntp_reply: Optional[NTPReply] = None
2023-11-20 18:08:55 +00:00
def generate_reply(self, ntp_server_time: datetime) -> NTPPacket:
2023-11-15 10:57:56 +00:00
"""Generate a NTPPacket containing the time in a NTPReply object.
:param time: datetime object representing the time from the NTP server.
:return: A new NTPPacket object.
"""
2023-11-20 18:08:55 +00:00
self.ntp_reply = NTPReply(ntp_datetime=ntp_server_time)
return self