# 
# Packets.py
#  
# Author(s):
#       Alessio Parma <alessio.parma@gmail.com>
#       Artur Tolstenco <tartur88@gmail.com>
# 
# Copyright (c) 2012-2016 Alessio Parma <alessio.parma@gmail.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

class CodePacket:
    """A struct representing the packet generated by the erasure code."""

    def __init__(self, owner, keeper, len):
        self.owner = owner
        self.keeper = keeper
        self.len = len

    def __str__(self):
        str = "CodePacket[owner: {0}; keeper: {1}; len: {2}]"
        return str.format(self.owner, self.keeper, self.len)

class RequestPacket:
    """
    At low level a server receive a certain amount of UDPPackets.
    The UDPPackets must then be put together in order to make up the data.
    So, the server receives the UDPPackets, aggregate them to make one or more RequestPackets.
    """

    def __init__(self, owner, sessionId):
        self.owner = owner
        self.sessionId = sessionId

    def __str__(self):
        str = "RequestPacket[owner: {0} sessiondId: {1}]"
        return str.format(self.owner, self.sessionId)

class UdpPacket:
    """A struct representing the information contained in the header of an UDP packet."""

    def __init__(self, sessionId, src, dst, len, type, count):
        self.sessionId = sessionId
        self.src = src
        self.dst = dst
        self.len = len
        self.type = type
        self.count = count

    def __str__(self):
        str = "UdpPacket[sessionId: {0}; src: {1}; dst: {2}; type: {3}]"
        return str.format(self.sessionId, self.src, self.dst, self.type)