// File: billDrett[P]Bulletin-Boards/boardClient.cpp
// Function: BoardClient::readMessages()
void BoardClient::readMessages()
{
    char* tmp;
    char* tmp1;
    int fp = -1;
    int nread;
    char buffer [BUFFERSIZE];
    states state = waitingMessage;
    string  tmpTag;

    while((nread = read(pipeServer2Client, buffer + tmpTag.size(), BUFFERSIZE - tmpTag.size())) > 0) //diavazoume thn apanthsh tou server mexri na vroume to <end><File>
    {                                                                                                //an se proigoumeno read diavase meros tou tag to prosthetei sthn arxh tou buffer kai diavazei ligotera stoixeia
        strncpy(buffer, tmpTag.c_str(), tmpTag.size()); //prostetw sthn arxh tou buffer to proigoumeno tag efoson yparxei
        nread += tmpTag.size();
        tmpTag = "";
        tmp = buffer;

        if(findSubstring(tmp, "<NONE>", nread) != NULL)//ama lavw to NONE tote dn yparxei to chanel
        {
            cout<<"The chanel doesnt exists"<<endl;
            return;
        }

        if(state == waitingMessage) //prwta to message tag
        {
            tmp1 = getAfterTag(tmp, "<Message>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL) // mporei na pira meros tou tag, to apothikeuw kai to prosthetw sthn arxh tou neou read
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = MessageWaitStart;
        }

        if(state == MessageWaitStart) //meta perimeno to start tag
        {
            tmp1 = getAfterTag(tmp, "<start>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = MessageWaitEnd;
            cout<<"Message :"<<endl;
        }

        if(state == MessageWaitEnd)
        {
            int copyNread = nread;
            if(getBeforeTag(tmp, "<end>", copyNread) != NULL)
            {
                state = waitingEndMessage;
            }

            printf("%.*s", copyNread, tmp); //mporei to string na mhn teleiwnei me \0 gia auto xrhsimopoiw auth thn print
        }

        if(state == waitingEndMessage)
        {
            cout<<endl;
            tmp1 = getAfterTag(tmp, "<Message>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = waitingFile;
        }

        if(state == waitingFile) //teleiwsa me to Message perimenw ta files twra
        {
            tmp1 = getAfterTag(tmp, "<File>", nread);
            if(tmp1 == NULL)
            {
                if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                {
                    tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                }
                continue;
            }
            tmp = tmp1;
            state = FileWaitName;
        }

        if(state < FileWaitName) continue; //dn thelw na mpw sto loop enw dn exw ftasei se ena apo ta parakatw stadia giati tote dn tha termatistei pote
        while(1) //dn kserw posa files tha diavasei gia auto tha stamathsei otan dei to <File>
        {
            if(state == FileWaitName)
            {
                string fileName;
                int lenght;

                tmp1 = getAfterTag(tmp, "<:", nread); //pairnw to onoma tou file
                lenght = nread;
                if((tmp1 == NULL) || getBeforeTag(tmp1, ">", lenght) == NULL)
                {
                    if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                    {
                        tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                    }
                    break;
                }
                tmp = tmp1;

                fileName.assign(tmp, lenght);
                if(fileName == " ")//keno file, to chanel dn eixe arxeia
                {
                    cout<<"No files to download"<<endl;
                    state = FileWaitStart;
                    continue;
                }
                close(fp);

                fileNameExists(fileName); //elenxo an yparxei hdh to onoma

                fp=open(fileName.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0644); //anoigw to arxeio gia grapsimo
                if(fp == -1)
                {
                    perror("problem opening file");
                }

                state = FileWaitStart;
                cout<<fileName<<"is downloaded"<<endl;
            }

            if(state == FileWaitStart)
            {
                tmp1 = getAfterTag(tmp, "<start>", nread);
                if(tmp1 == NULL)
                {
                    if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                    {
                        tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                    }
                    break;
                }
                tmp = tmp1;
                state = FileWaitEnd;
            }

            if(state == FileWaitEnd)
            {
                int copyNread = nread;
                if(getBeforeTag(tmp, "<end>", copyNread) != NULL)
                {
                    state = waitingEndFile;
                }

                write(fp, tmp, copyNread);
                if(state == FileWaitEnd) break; //an exw kai alla na diavastoun pairnw nea dodomena apo to pipe
            }

            if(state == waitingEndFile)
            {
                if(findSubstring(tmp, "<:", nread) != NULL) //an yparxei akoma arxeio epistrofi pisw
                {
                    state = FileWaitName;
                    close(fp);
                    continue;
                }
                close(fp);
                tmp = getAfterTag(tmp, "<File>", nread);
                if(tmp1 == NULL)
                {
                    if((tmp1 = getAfterTag(tmp, "<", nread)) != NULL)
                    {
                        tmpTag.assign(tmp1-1, nread+1); // -1 giati thelw na krathsw kai to "< kai +1 sto read gia na to symperilavw
                    }
                    break;
                }
                return;
            }
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5a0, %rsp            # imm = 0x5A0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x490(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x424(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x33e0
movq -0x490(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x4a4(%rbp)
leaq -0x448(%rbp), %rdi
movq %rdi, -0x4b0(%rbp)
callq 0x3210
movq -0x4b0(%rbp), %rdi
leaq -0x420(%rbp,%rax), %rax
movq %rax, -0x4a0(%rbp)
callq 0x3210
movl -0x4a4(%rbp), %edi
movq -0x4a0(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
subq %rax, %rdx
callq 0x3400
movq %rax, -0x498(%rbp)
jmp 0x4cb8
movq -0x498(%rbp), %rax
movl %eax, -0x20(%rbp)
cmpl $0x0, %eax
jle 0x557d
leaq -0x448(%rbp), %rdi
movq %rdi, -0x4b8(%rbp)
callq 0x3130
movq -0x4b8(%rbp), %rdi
movq %rax, -0x4c0(%rbp)
callq 0x3210
movq -0x4c0(%rbp), %rsi
movq %rax, %rdx
leaq -0x420(%rbp), %rdi
callq 0x31c0
movq -0x4b8(%rbp), %rdi
callq 0x3210
movq -0x4b8(%rbp), %rdi
movq %rax, %rcx
movl -0x20(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x20(%rbp)
leaq 0x64c9(%rip), %rsi      # 0xb1f5
callq 0x33d0
jmp 0x4d33
leaq -0x420(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %edx
leaq 0x63a7(%rip), %rsi      # 0xb0f3
callq 0x8d20
movq %rax, -0x4c8(%rbp)
jmp 0x4d5a
movq -0x4c8(%rbp), %rax
cmpq $0x0, %rax
je 0x4dbe
movq 0xa262(%rip), %rdi      # 0xefd0
leaq 0x6385(%rip), %rsi      # 0xb0fa
callq 0x32c0
movq %rax, -0x4d0(%rbp)
jmp 0x4d83
movq -0x4d0(%rbp), %rdi
movq 0xa22f(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x4d98
movl $0x1, -0x458(%rbp)
jmp 0x5587
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
jmp 0x559c
cmpl $0x0, -0x424(%rbp)
jne 0x4e5e
movq -0x10(%rbp), %rdi
leaq 0x6313(%rip), %rsi      # 0xb0e9
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x4d8(%rbp)
jmp 0x4de8
movq -0x4d8(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x4e4c
movq -0x10(%rbp), %rdi
leaq 0x630e(%rip), %rsi      # 0xb113
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x4e0(%rbp)
jmp 0x4e17
movq -0x4e0(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x4e47
movq -0x18(%rbp), %rsi
decq %rsi
movl -0x20(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq -0x448(%rbp), %rdi
callq 0x33c0
jmp 0x4e45
jmp 0x4e47
jmp 0x4c57
movq -0x18(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x1, -0x424(%rbp)
cmpl $0x1, -0x424(%rbp)
jne 0x4f31
movq -0x10(%rbp), %rdi
leaq 0x6263(%rip), %rsi      # 0xb0d9
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x4e8(%rbp)
jmp 0x4e88
movq -0x4e8(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x4eec
movq -0x10(%rbp), %rdi
leaq 0x626e(%rip), %rsi      # 0xb113
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x4f0(%rbp)
jmp 0x4eb7
movq -0x4f0(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x4ee7
movq -0x18(%rbp), %rsi
decq %rsi
movl -0x20(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq -0x448(%rbp), %rdi
callq 0x33c0
jmp 0x4ee5
jmp 0x4ee7
jmp 0x4c57
movq -0x18(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x2, -0x424(%rbp)
movq 0xa0cb(%rip), %rdi      # 0xefd0
leaq 0x6209(%rip), %rsi      # 0xb115
callq 0x32c0
movq %rax, -0x4f8(%rbp)
jmp 0x4f1a
movq -0x4f8(%rbp), %rdi
movq 0xa098(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x4f2f
jmp 0x4f31
cmpl $0x2, -0x424(%rbp)
jne 0x4f96
movl -0x20(%rbp), %eax
movl %eax, -0x45c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x62ca(%rip), %rsi      # 0xb218
leaq -0x45c(%rbp), %rdx
callq 0x8e70
movq %rax, -0x500(%rbp)
jmp 0x4f63
movq -0x500(%rbp), %rax
cmpq $0x0, %rax
je 0x4f7a
movl $0x3, -0x424(%rbp)
movl -0x45c(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq 0x6194(%rip), %rdi      # 0xb11f
xorl %eax, %eax
callq 0x3050
jmp 0x4f94
jmp 0x4f96
cmpl $0x3, -0x424(%rbp)
jne 0x504b
movq 0xa026(%rip), %rdi      # 0xefd0
movq 0xa00f(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x4fb8
movq -0x10(%rbp), %rdi
leaq 0x6126(%rip), %rsi      # 0xb0e9
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x508(%rbp)
jmp 0x4fd5
movq -0x508(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5039
movq -0x10(%rbp), %rdi
leaq 0x6121(%rip), %rsi      # 0xb113
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x510(%rbp)
jmp 0x5004
movq -0x510(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x5034
movq -0x18(%rbp), %rsi
decq %rsi
movl -0x20(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq -0x448(%rbp), %rdi
callq 0x33c0
jmp 0x5032
jmp 0x5034
jmp 0x4c57
movq -0x18(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x4, -0x424(%rbp)
cmpl $0x4, -0x424(%rbp)
jne 0x50eb
movq -0x10(%rbp), %rdi
leaq 0x60c1(%rip), %rsi      # 0xb124
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x518(%rbp)
jmp 0x5075
movq -0x518(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x50d9
movq -0x10(%rbp), %rdi
leaq 0x6081(%rip), %rsi      # 0xb113
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x520(%rbp)
jmp 0x50a4
movq -0x520(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x50d4
movq -0x18(%rbp), %rsi
decq %rsi
movl -0x20(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq -0x448(%rbp), %rdi
callq 0x33c0
jmp 0x50d2
jmp 0x50d4
jmp 0x4c57
movq -0x18(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x5, -0x424(%rbp)
cmpl $0x5, -0x424(%rbp)
jge 0x50f9
jmp 0x4c57
jmp 0x50fb
jmp 0x50fd
cmpl $0x5, -0x424(%rbp)
jne 0x5382
leaq -0x480(%rbp), %rdi
callq 0x33e0
movq -0x10(%rbp), %rdi
leaq 0x600a(%rip), %rsi      # 0xb12b
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x528(%rbp)
jmp 0x5133
movq -0x528(%rbp), %rax
movq %rax, -0x18(%rbp)
movl -0x20(%rbp), %eax
movl %eax, -0x484(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x517b
movq -0x18(%rbp), %rdi
leaq 0x5f9f(%rip), %rsi      # 0xb0f8
leaq -0x484(%rbp), %rdx
callq 0x8e70
movq %rax, -0x530(%rbp)
jmp 0x516e
movq -0x530(%rbp), %rax
cmpq $0x0, %rax
jne 0x51fa
movq -0x10(%rbp), %rdi
leaq 0x5f8d(%rip), %rsi      # 0xb113
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x538(%rbp)
jmp 0x5198
movq -0x538(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x51eb
movq -0x18(%rbp), %rsi
decq %rsi
movl -0x20(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq -0x448(%rbp), %rdi
callq 0x33c0
jmp 0x51c6
jmp 0x51eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x450(%rbp)
movl %eax, -0x454(%rbp)
leaq -0x480(%rbp), %rdi
callq 0x31f0
jmp 0x559c
movl $0x5, -0x458(%rbp)
jmp 0x534c
movq -0x18(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rsi
movslq -0x484(%rbp), %rdx
leaq -0x480(%rbp), %rdi
callq 0x33c0
jmp 0x521b
leaq 0x5eac(%rip), %rsi      # 0xb0ce
leaq -0x480(%rbp), %rdi
callq 0x5a80
movb %al, -0x539(%rbp)
jmp 0x5236
movb -0x539(%rbp), %al
testb $0x1, %al
jne 0x5242
jmp 0x528c
movq 0x9d87(%rip), %rdi      # 0xefd0
leaq 0x5ede(%rip), %rsi      # 0xb12e
callq 0x32c0
movq %rax, -0x548(%rbp)
jmp 0x525e
movq -0x548(%rbp), %rdi
movq 0x9d54(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x5273
movl $0x6, -0x424(%rbp)
movl $0x4, -0x458(%rbp)
jmp 0x534c
movl -0x1c(%rbp), %edi
callq 0x3520
jmp 0x5296
leaq -0x480(%rbp), %rdi
callq 0x8ee0
jmp 0x52a4
leaq -0x480(%rbp), %rdi
callq 0x3130
movq %rax, %rdi
xorl %eax, %eax
movl $0x241, %esi            # imm = 0x241
movl $0x1a4, %edx            # imm = 0x1A4
callq 0x3230
movl %eax, -0x54c(%rbp)
jmp 0x52cc
movl -0x54c(%rbp), %eax
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x52eb
leaq 0x5e61(%rip), %rdi      # 0xb143
callq 0x3260
jmp 0x52e9
jmp 0x52eb
movl $0x6, -0x424(%rbp)
movq 0x9cd4(%rip), %rdi      # 0xefd0
leaq -0x480(%rbp), %rsi
callq 0x3290
movq %rax, -0x558(%rbp)
jmp 0x5311
movq -0x558(%rbp), %rdi
leaq 0x5e39(%rip), %rsi      # 0xb158
callq 0x32c0
movq %rax, -0x560(%rbp)
jmp 0x532d
movq -0x560(%rbp), %rdi
movq 0x9c85(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x5342
movl $0x0, -0x458(%rbp)
leaq -0x480(%rbp), %rdi
callq 0x31f0
movl -0x458(%rbp), %eax
movl %eax, -0x564(%rbp)
testl %eax, %eax
je 0x5380
jmp 0x536a
movl -0x564(%rbp), %eax
subl $0x4, %eax
je 0x50fb
jmp 0x537b
jmp 0x5578
jmp 0x5382
cmpl $0x6, -0x424(%rbp)
jne 0x5422
movq -0x10(%rbp), %rdi
leaq 0x5d3f(%rip), %rsi      # 0xb0d9
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x570(%rbp)
jmp 0x53ac
movq -0x570(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5410
movq -0x10(%rbp), %rdi
leaq 0x5d4a(%rip), %rsi      # 0xb113
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x578(%rbp)
jmp 0x53db
movq -0x578(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x540b
movq -0x18(%rbp), %rsi
decq %rsi
movl -0x20(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq -0x448(%rbp), %rdi
callq 0x33c0
jmp 0x5409
jmp 0x540b
jmp 0x5578
movq -0x18(%rbp), %rax
movq %rax, -0x10(%rbp)
movl $0x7, -0x424(%rbp)
cmpl $0x7, -0x424(%rbp)
jne 0x5490
movl -0x20(%rbp), %eax
movl %eax, -0x488(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x5dd9(%rip), %rsi      # 0xb218
leaq -0x488(%rbp), %rdx
callq 0x8e70
movq %rax, -0x580(%rbp)
jmp 0x5454
movq -0x580(%rbp), %rax
cmpq $0x0, %rax
je 0x546b
movl $0x8, -0x424(%rbp)
movl -0x1c(%rbp), %edi
movq -0x10(%rbp), %rsi
movslq -0x488(%rbp), %rdx
callq 0x3110
jmp 0x5480
cmpl $0x7, -0x424(%rbp)
jne 0x548e
jmp 0x5578
jmp 0x5490
cmpl $0x8, -0x424(%rbp)
jne 0x5573
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %edx
leaq 0x5c80(%rip), %rsi      # 0xb12b
callq 0x8d20
movq %rax, -0x588(%rbp)
jmp 0x54b9
movq -0x588(%rbp), %rax
cmpq $0x0, %rax
je 0x54df
movl $0x5, -0x424(%rbp)
movl -0x1c(%rbp), %edi
callq 0x3520
jmp 0x54da
jmp 0x50fb
movl -0x1c(%rbp), %edi
callq 0x3520
jmp 0x54e9
movq -0x10(%rbp), %rdi
leaq 0x5c30(%rip), %rsi      # 0xb124
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x590(%rbp)
jmp 0x5506
movq -0x590(%rbp), %rax
movq %rax, -0x10(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x5567
movq -0x10(%rbp), %rdi
leaq 0x5bf0(%rip), %rsi      # 0xb113
leaq -0x20(%rbp), %rdx
callq 0x8de0
movq %rax, -0x598(%rbp)
jmp 0x5535
movq -0x598(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x5565
movq -0x18(%rbp), %rsi
decq %rsi
movl -0x20(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq -0x448(%rbp), %rdi
callq 0x33c0
jmp 0x5563
jmp 0x5565
jmp 0x5578
movl $0x1, -0x458(%rbp)
jmp 0x5587
jmp 0x50fb
jmp 0x4c57
movl $0x0, -0x458(%rbp)
leaq -0x448(%rbp), %rdi
callq 0x31f0
addq $0x5a0, %rsp            # imm = 0x5A0
popq %rbp
retq
leaq -0x448(%rbp), %rdi
callq 0x31f0
movq -0x450(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)