//
// Created by alex on 7/16/20.
//

#include <cstdio>
#include <arpa/inet.h>
#include <iostream>

#include "udp_transport.cuh"

UdpTransport::UdpTransport(string localAddr, string mcastAddr, eTransportRole role) {

    s_localAddr = localAddr;
    s_mcastAddr = mcastAddr;
    n_mcastPort = 6655; //TODO: does this matter?
    n_localPort = 6655; //TODO: does this matter?

    bzero( &g_localAddr, sizeof( g_localAddr ) );
    bzero( &g_mcastAddr, sizeof( g_mcastAddr ) );

    // Creating socket file descriptor
       if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        cerr << "ERROR UdpTransport - Failed to create socket " << errno << endl;
        exit(EXIT_FAILURE);
    }
    cout << "Created local UDP socket: " << sockfd << endl;

    if(role == eTransportRole::SENSOR)
    {
        memset((char *) &g_mcastAddr, 0, sizeof(g_mcastAddr));
        g_mcastAddr.sin_family = AF_INET;
        g_mcastAddr.sin_addr.s_addr = inet_addr(s_mcastAddr.c_str());
        g_mcastAddr.sin_port = htons(n_mcastPort);

        char loopch=0;

        if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP,
                (char *)&loopch,
                sizeof(loopch)) < 0) {
            cerr << "ERROR UdpTransport - disable loop back failed set opt " << errno << endl;
            close(sockfd);
            exit(EXIT_FAILURE);
        }

        inet_pton(AF_INET, s_localAddr.c_str(), &this->g_localAddr.sin_addr);
        if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF,
                       (char *)&g_localAddr,
                       sizeof(g_localAddr)) < 0) {
            cerr << "ERROR UdpTransport - Setting up local interface " << strerror(errno) << endl;
            exit(1);
        }

    } else if(role == eTransportRole::PROCESSOR) {

        {
            int reuse=1;

            if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
                           (char *)&reuse, sizeof(reuse)) < 0) {
                cerr << "ERROR UdpTransport - set option to allow socket reuse " << errno << endl;
                close(sockfd);
                exit(EXIT_FAILURE);
            }
        }

        //Create the SockAddr for the Local System
        memset(&g_localAddr, 0, sizeof(g_localAddr));
        g_localAddr.sin_family = AF_INET;
        g_localAddr.sin_port = htons(n_localPort);
        g_localAddr.sin_addr.s_addr = INADDR_ANY;

        cout << "Bind the socket local address: " << s_localAddr << endl;
        if (bind(sockfd, (const struct sockaddr *) &g_localAddr,
                 sizeof(g_localAddr)) < 0) {
            cerr << "ERROR UdpTransport - failed to bind to local socket " << errno << endl;
            exit(EXIT_FAILURE);
        }

        if (inet_pton(AF_INET, s_mcastAddr.c_str(), &mcastGroup.imr_multiaddr.s_addr) != 1) {
            cerr << "ERROR inet pton can't convert address " << errno << endl;
            close(sockfd);
            exit(EXIT_FAILURE);
        }
        if (inet_pton(AF_INET, s_localAddr.c_str(), &mcastGroup.imr_interface.s_addr) != 1) {
            cerr << "ERROR inet pton can't convert address " << errno << endl;
            close(sockfd);
            exit(EXIT_FAILURE);
        }

        if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                       (char *)&mcastGroup, sizeof(mcastGroup)) < 0) {
            cerr << "ERROR UdpTransport - couldn't join mcast group " << errno << endl;
            close(sockfd);
            exit(EXIT_FAILURE);
        }


    }

}

int UdpTransport::push(Message* m)
{
    if(sendto(sockfd, (const char *)m->buffer, m->bufferSize,0,
            (const struct sockaddr *) &this->g_mcastAddr,
                    sizeof(this->g_mcastAddr)) <= 0)
    {
        cerr << "ERROR UdpTransport Push - failed sendto operation " << errno << endl;
        close(sockfd);
        exit(EXIT_FAILURE);
    }
    DEBUG("To " << inet_ntoa(g_mcastAddr.sin_addr) << endl);
#ifdef DEBUG_BUILD
    printMessage(m, 32);
#endif
    return 0;
}

/*
*  Pulls a message from the transport and places it in the buffer
*/
int UdpTransport::pop(Message** m, int numReqMsg, int& numRetMsg, eTransportDest dest)
{
    uint8_t buffer[MSG_MAX_SIZE];    // receive buffer
    int recvlen;                         // num bytes received
    //struct sockaddr_in from;             // Sender's address. TODO: Don't need these, just waste perf
    //int fromlen;                         // Length of sender's address.

    DEBUG("waiting on socket " << this->n_localPort << endl);

    for(int i = 0; i < numReqMsg; i++)
    {
        //recvlen = sockfd, buffer, MSG_MAX_SIZE);
        recvlen = recv(this->sockfd, buffer, 1000, 0);

        if (recvlen > 0) {
            //cout << "received " << recvlen << " bytes " << "from " << inet_ntoa(from.sin_addr) << endl;
            m[i] = createMessage();
            m[i]->seqNumber = i;
            m[i]->interval = 0;
            m[i]->bufferSize = recvlen;
            memcpy(buffer,m[i]->buffer,recvlen); //TODO: smarter way than a copy?
            numRetMsg = numRetMsg + 1;
        } else if(recvlen == -1) {
            cerr << "ERROR UdpTransport Pop - failed mcast socket read " << errno << endl;
            close(sockfd);
            exit(EXIT_FAILURE);
        }
    }

    return 0;
}

Message* UdpTransport::createMessage() {
    Message* m = NULL;
    std::size_t t = sizeof(Message);
    m = static_cast<Message*>(malloc(t));
    return m;
}

int UdpTransport::freeMessage(Message* m)
{
    free(m);
    return 0;
}
	.file	"tmpxft_00285768_00000000-6_udp_transport.cudafe1.cpp"
	.text
#APP
	.globl _ZSt21ios_base_library_initv

#NO_APP
	.align 2
	.globl	_ZN12UdpTransport13createMessageEv
	.type	_ZN12UdpTransport13createMessageEv, @function
_ZN12UdpTransport13createMessageEv:
.LFB4129:
	.cfi_startproc
	endbr64
	movl	$1512, %edi
	jmp	malloc@PLT
	.cfi_endproc
.LFE4129:
	.size	_ZN12UdpTransport13createMessageEv, .-_ZN12UdpTransport13createMessageEv
	.align 2
	.globl	_ZN12UdpTransport11freeMessageEP7Message
	.type	_ZN12UdpTransport11freeMessageEP7Message, @function
_ZN12UdpTransport11freeMessageEP7Message:
.LFB4130:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	movq	%rsi, %rdi
	call	free@PLT
	xorl	%eax, %eax
	popq	%rdx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4130:
	.size	_ZN12UdpTransport11freeMessageEP7Message, .-_ZN12UdpTransport11freeMessageEP7Message
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB4133:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE4133:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.align 2
	.type	_ZNSolsEPFRSoS_E.isra.0, @function
_ZNSolsEPFRSoS_E.isra.0:
.LFB4853:
	.cfi_startproc
	jmp	*%rsi
	.cfi_endproc
.LFE4853:
	.size	_ZNSolsEPFRSoS_E.isra.0, .-_ZNSolsEPFRSoS_E.isra.0
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"ERROR UdpTransport Pop - failed mcast socket read "
	.text
	.align 2
	.globl	_ZN12UdpTransport3popEPP7MessageiRi14eTransportDest
	.type	_ZN12UdpTransport3popEPP7MessageiRi14eTransportDest, @function
_ZN12UdpTransport3popEPP7MessageiRi14eTransportDest:
.LFB4128:
	.cfi_startproc
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rdi, %r15
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	movl	%edx, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movq	%rcx, %rbp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	xorl	%ebx, %ebx
	subq	$1544, %rsp
	.cfi_def_cfa_offset 1600
	movq	%fs:40, %rax
	movq	%rax, 1528(%rsp)
	xorl	%eax, %eax
	leaq	28(%rsp), %rax
	movq	%rax, 8(%rsp)
.L7:
	cmpl	%ebx, %r12d
	jle	.L14
	movl	116(%r15), %edi
	movq	8(%rsp), %rsi
	xorl	%r8d, %r8d
	movl	$1500, %ecx
	movl	$1000, %edx
	call	__recv_chk@PLT
	movq	%rax, %r14
	testl	%eax, %eax
	jle	.L8
	movq	(%r15), %rax
	movq	%r15, %rdi
	call	*16(%rax)
	xorl	%edx, %edx
	movq	8(%rsp), %rdi
	movl	$1500, %ecx
	movq	%rax, 0(%r13,%rbx,8)
	leaq	12(%rax), %rsi
	movl	%edx, (%rax)
	movslq	%r14d, %rdx
	movl	%ebx, 8(%rax)
	movl	%r14d, 4(%rax)
	call	__memcpy_chk@PLT
	incl	0(%rbp)
	jmp	.L9
.L8:
	incl	%r14d
	jne	.L9
	leaq	.LC0(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rbx
	call	__errno_location@PLT
	movq	%rbx, %rdi
	movl	(%rax), %esi
	call	_ZNSolsEi@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	116(%r15), %edi
	call	close@PLT
	movl	$1, %edi
	call	exit@PLT
.L9:
	incq	%rbx
	jmp	.L7
.L14:
	movq	1528(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$1544, %rsp
	.cfi_def_cfa_offset 56
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4128:
	.size	_ZN12UdpTransport3popEPP7MessageiRi14eTransportDest, .-_ZN12UdpTransport3popEPP7MessageiRi14eTransportDest
	.section	.rodata.str1.1
.LC1:
	.string	"ERROR UdpTransport Push - failed sendto operation "
	.text
	.align 2
	.globl	_ZN12UdpTransport4pushEP7Message
	.type	_ZN12UdpTransport4pushEP7Message, @function
_ZN12UdpTransport4pushEP7Message:
.LFB4127:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	addq	$12, %rsi
	movl	$16, %r9d
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movq	%rdi, %rbx
	pushq	%rcx
	.cfi_def_cfa_offset 32
	movslq	-8(%rsi), %rdx
	xorl	%ecx, %ecx
	leaq	100(%rbx), %r8
	movl	116(%rdi), %edi
	call	sendto@PLT
	testq	%rax, %rax
	jg	.L16
	leaq	.LC1(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rbp
	call	__errno_location@PLT
	movq	%rbp, %rdi
	movl	(%rax), %esi
	call	_ZNSolsEi@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	116(%rbx), %edi
	call	close@PLT
	movl	$1, %edi
	call	exit@PLT
.L16:
	popq	%rdx
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4127:
	.size	_ZN12UdpTransport4pushEP7Message, .-_ZN12UdpTransport4pushEP7Message
	.section	.rodata.str1.1
.LC2:
	.string	"ERROR UdpTransport - Failed to create socket "
.LC3:
	.string	"Created local UDP socket: "
.LC4:
	.string	"ERROR UdpTransport - disable loop back failed set opt "
.LC5:
	.string	"ERROR UdpTransport - Setting up local interface "
.LC6:
	.string	"ERROR UdpTransport - set option to allow socket reuse "
.LC7:
	.string	"Bind the socket local address: "
.LC8:
	.string	"ERROR UdpTransport - failed to bind to local socket "
.LC9:
	.string	"ERROR inet pton can't convert address "
.LC10:
	.string	"ERROR UdpTransport - couldn't join mcast group "
	.text
	.align 2
	.globl	_ZN12UdpTransportC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole
	.type	_ZN12UdpTransportC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole, @function
_ZN12UdpTransportC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole:
.LFB4125:
	.cfi_startproc
	.cfi_personality 0x9b,DW.ref.__gxx_personality_v0
	.cfi_lsda 0x1b,.LLSDA4125
	endbr64
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	xorl	%r8d, %r8d
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	movq	%rdx, %r14
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	movl	%ecx, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movq	%rdi, %rbx
	leaq	48(%rbx), %r12
	leaq	8(%rbx), %r13
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 8(%rsp)
	xorl	%eax, %eax
	leaq	24(%rdi), %rax
	movq	%rax, 8(%rdi)
	leaq	64(%rbx), %rax
	xorl	%edi, %edi
	movq	%rax, 48(%rbx)
	leaq	16+_ZTV12UdpTransport(%rip), %rax
	movq	%rdi, 16(%rbx)
	movq	%r12, %rdi
	movb	$0, 24(%rbx)
	movq	%r8, 56(%rbx)
	movb	$0, 64(%rbx)
	movq	%rax, (%rbx)
.LEHB0:
	call	_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_assignERKS4_@PLT
	movq	%r14, %rsi
	movq	%r13, %rdi
	call	_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_assignERKS4_@PLT
	movl	$6655, 40(%rbx)
	xorps	%xmm0, %xmm0
	xorl	%edx, %edx
	leaq	84(%rbx), %r14
	movups	%xmm0, 84(%rbx)
	movl	$2, %esi
	movl	$2, %edi
	movl	$6655, 80(%rbx)
	movups	%xmm0, 100(%rbx)
	call	socket@PLT
	movl	%eax, 116(%rbx)
	testl	%eax, %eax
	jns	.L19
	leaq	.LC2(%rip), %rsi
.L59:
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rbp
	call	__errno_location@PLT
	movq	%rbp, %rdi
	movl	(%rax), %esi
	call	_ZNSolsEi@PLT
.L40:
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
.L22:
	movl	$1, %edi
	call	exit@PLT
.L19:
	leaq	_ZSt4cout(%rip), %r15
	leaq	.LC3(%rip), %rsi
	movq	%r15, %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movl	116(%rbx), %esi
	movq	%rax, %rdi
	call	_ZNSolsEi@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	testl	%ebp, %ebp
	jne	.L20
	xorl	%ecx, %ecx
	movw	$2, 100(%rbx)
	movq	8(%rbx), %rdi
	movq	%rcx, 102(%rbx)
	movq	%rcx, 108(%rbx)
	call	inet_addr@PLT
	movl	116(%rbx), %edi
	xorl	%esi, %esi
	leaq	3(%rsp), %rcx
	movl	%eax, 104(%rbx)
	movzwl	40(%rbx), %eax
	movl	$1, %r8d
	movl	$34, %edx
	movb	$0, 3(%rsp)
	xchgb	%ah, %al
	movw	%ax, 102(%rbx)
	call	setsockopt@PLT
	leaq	.LC4(%rip), %rsi
	testl	%eax, %eax
	js	.L61
	movq	48(%rbx), %rsi
	leaq	88(%rbx), %rdx
	movl	$2, %edi
	call	inet_pton@PLT
	movl	116(%rbx), %edi
	xorl	%esi, %esi
	movl	$16, %r8d
	movq	%r14, %rcx
	movl	$32, %edx
	call	setsockopt@PLT
	testl	%eax, %eax
	jns	.L18
	leaq	.LC5(%rip), %rsi
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rbp
	call	__errno_location@PLT
	movl	(%rax), %edi
	call	strerror@PLT
	movq	%rbp, %rdi
	movq	%rax, %rsi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	jmp	.L40
.L20:
	decl	%ebp
	jne	.L18
	movl	116(%rbx), %edi
	movl	$1, %esi
	leaq	4(%rsp), %rcx
	movl	$4, %r8d
	movl	$2, %edx
	movl	$1, 4(%rsp)
	call	setsockopt@PLT
	leaq	.LC6(%rip), %rsi
	testl	%eax, %eax
	js	.L61
	xorl	%eax, %eax
	xorl	%edx, %edx
	movw	$2, 84(%rbx)
	movq	%r15, %rdi
	movq	%rax, 88(%rbx)
	movzwl	80(%rbx), %eax
	leaq	.LC7(%rip), %rsi
	movl	%edx, 96(%rbx)
	xchgb	%ah, %al
	movw	%ax, 86(%rbx)
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rdi
	movq	%r12, %rsi
	call	_ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKNSt7__cxx1112basic_stringIS4_S5_T1_EE@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	116(%rbx), %edi
	movq	%r14, %rsi
	movl	$16, %edx
	call	bind@PLT
	leaq	.LC8(%rip), %rsi
	testl	%eax, %eax
	js	.L59
	movq	8(%rbx), %rsi
	leaq	124(%rbx), %rbp
	movl	$2, %edi
	movq	%rbp, %rdx
	call	inet_pton@PLT
	decl	%eax
	jne	.L65
	movq	48(%rbx), %rsi
	leaq	128(%rbx), %rdx
	movl	$2, %edi
	call	inet_pton@PLT
	decl	%eax
	je	.L29
.L65:
	leaq	.LC9(%rip), %rsi
	jmp	.L61
.L29:
	movl	116(%rbx), %edi
	xorl	%esi, %esi
	movl	$8, %r8d
	movq	%rbp, %rcx
	movl	$35, %edx
	call	setsockopt@PLT
	testl	%eax, %eax
	jns	.L18
	leaq	.LC10(%rip), %rsi
.L61:
	leaq	_ZSt4cerr(%rip), %rdi
	call	_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
	movq	%rax, %rbp
	call	__errno_location@PLT
	movq	%rbp, %rdi
	movl	(%rax), %esi
	call	_ZNSolsEi@PLT
	movq	_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rsi
	movq	%rax, %rdi
	call	_ZNSolsEPFRSoS_E.isra.0
	movl	116(%rbx), %edi
	call	close@PLT
.LEHE0:
	jmp	.L22
.L33:
	endbr64
	movq	%rax, %rbp
.L30:
	leaq	16+_ZTV10ITransport(%rip), %rax
	movq	%r12, %rdi
	movq	%rax, (%rbx)
	call	_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE10_M_disposeEv@PLT
	movq	%r13, %rdi
	call	_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE10_M_disposeEv@PLT
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	jne	.L50
	movq	%rbp, %rdi
.LEHB1:
	call	_Unwind_Resume@PLT
.LEHE1:
.L18:
	movq	8(%rsp), %rax
	subq	%fs:40, %rax
	je	.L32
.L50:
	call	__stack_chk_fail@PLT
.L32:
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE4125:
	.globl	__gxx_personality_v0
	.section	.gcc_except_table,"a",@progbits
.LLSDA4125:
	.byte	0xff
	.byte	0xff
	.byte	0x1
	.uleb128 .LLSDACSE4125-.LLSDACSB4125
.LLSDACSB4125:
	.uleb128 .LEHB0-.LFB4125
	.uleb128 .LEHE0-.LEHB0
	.uleb128 .L33-.LFB4125
	.uleb128 0
	.uleb128 .LEHB1-.LFB4125
	.uleb128 .LEHE1-.LEHB1
	.uleb128 0
	.uleb128 0
.LLSDACSE4125:
	.text
	.size	_ZN12UdpTransportC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole, .-_ZN12UdpTransportC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole
	.globl	_ZN12UdpTransportC1ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole
	.set	_ZN12UdpTransportC1ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole,_ZN12UdpTransportC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES5_14eTransportRole
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB4156:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE4156:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.weak	_ZTS10ITransport
	.section	.rodata._ZTS10ITransport,"aG",@progbits,_ZTS10ITransport,comdat
	.align 8
	.type	_ZTS10ITransport, @object
	.size	_ZTS10ITransport, 13
_ZTS10ITransport:
	.string	"10ITransport"
	.weak	_ZTI10ITransport
	.section	.data.rel.ro._ZTI10ITransport,"awG",@progbits,_ZTI10ITransport,comdat
	.align 8
	.type	_ZTI10ITransport, @object
	.size	_ZTI10ITransport, 16
_ZTI10ITransport:
	.quad	_ZTVN10__cxxabiv117__class_type_infoE+16
	.quad	_ZTS10ITransport
	.weak	_ZTS12UdpTransport
	.section	.rodata._ZTS12UdpTransport,"aG",@progbits,_ZTS12UdpTransport,comdat
	.align 8
	.type	_ZTS12UdpTransport, @object
	.size	_ZTS12UdpTransport, 15
_ZTS12UdpTransport:
	.string	"12UdpTransport"
	.weak	_ZTI12UdpTransport
	.section	.data.rel.ro._ZTI12UdpTransport,"awG",@progbits,_ZTI12UdpTransport,comdat
	.align 8
	.type	_ZTI12UdpTransport, @object
	.size	_ZTI12UdpTransport, 24
_ZTI12UdpTransport:
	.quad	_ZTVN10__cxxabiv120__si_class_type_infoE+16
	.quad	_ZTS12UdpTransport
	.quad	_ZTI10ITransport
	.weak	_ZTV10ITransport
	.section	.data.rel.ro._ZTV10ITransport,"awG",@progbits,_ZTV10ITransport,comdat
	.align 8
	.type	_ZTV10ITransport, @object
	.size	_ZTV10ITransport, 48
_ZTV10ITransport:
	.quad	0
	.quad	_ZTI10ITransport
	.quad	__cxa_pure_virtual
	.quad	__cxa_pure_virtual
	.quad	__cxa_pure_virtual
	.quad	__cxa_pure_virtual
	.weak	_ZTV12UdpTransport
	.section	.data.rel.ro.local._ZTV12UdpTransport,"awG",@progbits,_ZTV12UdpTransport,comdat
	.align 8
	.type	_ZTV12UdpTransport, @object
	.size	_ZTV12UdpTransport, 48
_ZTV12UdpTransport:
	.quad	0
	.quad	_ZTI12UdpTransport
	.quad	_ZN12UdpTransport4pushEP7Message
	.quad	_ZN12UdpTransport3popEPP7MessageiRi14eTransportDest
	.quad	_ZN12UdpTransport13createMessageEv
	.quad	_ZN12UdpTransport11freeMessageEP7Message
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.weak	__cxa_pure_virtual
	.hidden	DW.ref.__gxx_personality_v0
	.weak	DW.ref.__gxx_personality_v0
	.section	.data.rel.local.DW.ref.__gxx_personality_v0,"awG",@progbits,DW.ref.__gxx_personality_v0,comdat
	.align 8
	.type	DW.ref.__gxx_personality_v0, @object
	.size	DW.ref.__gxx_personality_v0, 8
DW.ref.__gxx_personality_v0:
	.quad	__gxx_personality_v0
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

//
// Created by alex on 7/16/20.
//

#ifndef SENSORSIM_UDP_TRANSPORT_CUH
#define SENSORSIM_UDP_TRANSPORT_CUH

#include <netinet/in.h>
#include <unistd.h>
#include <string>
#include <vector>

#include "../common.cuh"
#include "itransport.cuh"

using namespace std;

class UdpTransport: public ITransport {

public:
    UdpTransport(string localAddr, string mcastAddr, eTransportRole role);

private:
    int push(Message* msg);
    int pop(Message** msg, int numReqMsg, int& numRetMsg, eTransportDest dest);
    Message* createMessage();
    int freeMessage(Message* msg);

    struct ip_mreq        mcastGroup;

};


#endif //SENSORSIM_UDP_TRANSPORT_CUH

	.text
	.file	"udp_transport.hip"
                                        # Start of file scope inline assembly
	.globl	_ZSt21ios_base_library_initv

                                        # End of file scope inline assembly
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
