123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <string.h>
- #include "Client.h"
- #include "String.h"
- Client client;
- void interruptHandler(int signal)
- {
- clientRemove(&client);
- exit(EXIT_SUCCESS);
- }
- int readNumber(const char* text)
- {
- while(1)
- {
- int number;
- printf("%s", text);
- int result = scanf("%d", &number);
- char c = getchar();
- while(c != EOF && c != '\n')
- {
- c = getchar();
- }
-
- if(result != 0)
- {
- return number;
- }
- }
- return -1;
- }
- void waitForData()
- {
- while(1)
- {
- printf("Type a command: ");
- String s;
- stringInit(&s);
- stringRead(&s);
-
- if(stringCompare(&s, "SEND"))
- {
- printf("Sending a mail to the server...\n");
-
- //outgoing stream
- Stream out;
- streamInit(&out, 128);
- streamWriteInt(&out, 0);
-
- // sender
- printf("Type a sender with at most 8 characters: ");
- String sender;
- stringInit(&sender);
- stringRead(&sender);
- int l = stringGetLength(&sender);
- while(l < 1 || l > 8)
- {
- printf("Please type a valid sender: ");
- stringRemove(&sender);
- stringInit(&sender);
- stringRead(&sender);
- l = stringGetLength(&sender);
- }
- streamWriteChars(&out, sender.data);
- streamWriteChar(&out, '\n');
- stringRemove(&sender);
-
- // receiver
- printf("Type a receiver with at most 8 characters: ");
- String receiver;
- stringInit(&receiver);
- stringRead(&receiver);
- l = stringGetLength(&receiver);
- while(l < 1 || l > 8)
- {
- printf("Please type a valid receiver: ");
- stringRemove(&receiver);
- stringInit(&receiver);
- stringRead(&receiver);
- l = stringGetLength(&receiver);
- }
- streamWriteChars(&out, receiver.data);
- streamWriteChar(&out, '\n');
- stringRemove(&receiver);
-
- // subject
- printf("Type a subject with at most 80 characters: ");
- String subject;
- stringInit(&subject);
- stringRead(&subject);
- l = stringGetLength(&subject);
- while(l < 1 || l > 80)
- {
- printf("Please type a valid receiver: ");
- stringRemove(&subject);
- stringInit(&subject);
- stringRead(&subject);
- l = stringGetLength(&subject);
- }
- streamWriteChars(&out, subject.data);
- streamWriteChar(&out, '\n');
- stringRemove(&subject);
-
- // message
- printf("Type a message: ");
- String message;
- stringInit(&message);
- stringRead(&message);
- streamWriteChars(&out, message.data);
- streamWriteChars(&out, "\n.\n");
- stringRemove(&message);
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompare(&s, "LIST"))
- {
- //outgoing stream
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 1);
-
- // user
- printf("Type a user with at most 8 characters: ");
- String sender;
- stringInit(&sender);
- stringRead(&sender);
- int l = stringGetLength(&sender);
- while(l < 1 || l > 8)
- {
- printf("Please type a valid user: ");
- stringRemove(&sender);
- stringInit(&sender);
- stringRead(&sender);
- l = stringGetLength(&sender);
- }
- streamWriteChars(&out, sender.data);
- streamWriteChar(&out, '\n');
- stringRemove(&sender);
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompare(&s, "READ"))
- {
- //outgoing stream
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 2);
-
- // user
- printf("Type a user with at most 8 characters: ");
- String sender;
- stringInit(&sender);
- stringRead(&sender);
- int l = stringGetLength(&sender);
- while(l < 1 || l > 8)
- {
- printf("Please type a valid user: ");
- stringRemove(&sender);
- stringInit(&sender);
- stringRead(&sender);
- l = stringGetLength(&sender);
- }
- streamWriteChars(&out, sender.data);
- streamWriteChar(&out, '\n');
- stringRemove(&sender);
-
- // message number
- int number = readNumber("Type a message number: ");
- streamWriteInt(&out, number);
- streamWriteChar(&out, '\n');
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompare(&s, "DEL"))
- {
- //outgoing stream
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 3);
-
- // user
- printf("Type a user with at most 8 characters: ");
- String sender;
- stringInit(&sender);
- stringRead(&sender);
- int l = stringGetLength(&sender);
- while(l < 1 || l > 8)
- {
- printf("Please type a valid user: ");
- stringRemove(&sender);
- stringInit(&sender);
- stringRead(&sender);
- l = stringGetLength(&sender);
- }
- streamWriteChars(&out, sender.data);
- streamWriteChar(&out, '\n');
- stringRemove(&sender);
-
- // message number
- int number = readNumber("Type a message number: ");
- streamWriteInt(&out, number);
- streamWriteChar(&out, '\n');
-
- // send and clear stream
- clientSendData(&client, &out);
- streamRemove(&out);
- }
- else if(stringCompare(&s, "QUIT"))
- {
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 4);
- clientSendData(&client, &out);
- streamRemove(&out);
- }
-
- stringRemove(&s);
-
- /*if(type == -1)
- {
- break;
- }
- else if(type >= 0 && type < 4)
- {
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, type);
-
- switch(type)
- {
- case 0:
- printf("first number: ");
- streamWriteInt(&out, readInt());
- printf("second number: ");
- streamWriteInt(&out, readInt());
- break;
- case 1:
- printf("text: ");
- char buffer[128];
- fgets(buffer, 128, stdin);
- if(strlen(buffer) == 127 && buffer[126] != '\n')
- {
- clearInput();
- }
- streamWriteChars(&out, buffer);
- break;
- case 2:
- for(int i = 0; i < 260; i++)
- {
- streamWriteInt(&out, i);
- }
- break;
- }
-
- if(clientSendData(&client, &out) == -1)
- {
- streamRemove(&out);
- break;
- }
- streamRemove(&out);
- }
- else
- {
- printf("%d is not a valid package number\n", type);
- }*/
- }
- }
- void packageAnswer(Stream* in)
- {
- char buffer[32];
- streamGetChars(in, buffer, 32);
- printf("%s\n", buffer);
- }
- void packageList(Stream* in)
- {
- int number;
- if(streamGetInt(in, &number) == -1)
- {
- return;
- }
-
- char c;
- while(streamGetChar(in, &c) != -1)
- {
- printf("%c", c);
- }
- printf("\n");
- }
- int main(int argc, char** argv)
- {
- if(argc < 2)
- {
- printf("Usage: %s server_address\n", argv[0]);
- return EXIT_FAILURE;
- }
-
- clientInitDefaults(&client);
-
- signal(SIGINT, interruptHandler);
- signal(SIGKILL, interruptHandler);
-
- if(clientInit(&client, argv[1], 6543) == -1)
- {
- return EXIT_FAILURE;
- }
-
- clientRegisterHandler(&client, packageAnswer);
- clientRegisterHandler(&client, packageList);
- waitForData();
- clientRemove(&client);
- return EXIT_SUCCESS;
- }
|