From: <tom...@us...> - 2012-06-10 21:26:56
|
Revision: 202 http://ptpd.svn.sourceforge.net/ptpd/?rev=202&view=rev Author: tomekktcz Date: 2012-06-10 21:26:50 +0000 (Sun, 10 Jun 2012) Log Message: ----------- Socket interface implemented Modified Paths: -------------- branches/tomasz-2012/tools/MgmtMsgClient/Help.cpp branches/tomasz-2012/tools/MgmtMsgClient/Help.h branches/tomasz-2012/tools/MgmtMsgClient/Makefile branches/tomasz-2012/tools/MgmtMsgClient/MgmtMsgClient.cpp branches/tomasz-2012/tools/MgmtMsgClient/const.h Added Paths: ----------- branches/tomasz-2012/tools/MgmtMsgClient/Client.cpp branches/tomasz-2012/tools/MgmtMsgClient/Client.h branches/tomasz-2012/tools/MgmtMsgClient/Network.cpp branches/tomasz-2012/tools/MgmtMsgClient/Network.h branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.cpp branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.h Added: branches/tomasz-2012/tools/MgmtMsgClient/Client.cpp =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/Client.cpp (rev 0) +++ branches/tomasz-2012/tools/MgmtMsgClient/Client.cpp 2012-06-10 21:26:50 UTC (rev 202) @@ -0,0 +1,44 @@ +/** + * @file Client.cpp + * @author Tomasz Kleinschmidt + * + * @brief Client class implementation. + * + * This class will be used as a glue for all of the functionalities that + * the application is supposed to deliver. + */ + +#include <stdio.h> + +#include "Client.h" +#include "Help.h" +#include "Network.h" +#include "OptBuffer.h" + +/** + * This method will be used to deliver all of the requested actions to the + * user. + * + * @param optBuf Buffer with arguments passed from the user. + */ +void mainClient(OptBuffer* optBuf) { + int sockFd; + + if (optBuf->help_print) { + printHelp(optBuf->help_arg); + return; + } + + if (optBuf->msg_print) { + printMgmtMsgsList(); + return; + } + + sockFd = initNetwork(optBuf->u_address, optBuf->u_port); + + /* send <--> receive will be here */ + + disableNetwork(sockFd); + + return; +} \ No newline at end of file Added: branches/tomasz-2012/tools/MgmtMsgClient/Client.h =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/Client.h (rev 0) +++ branches/tomasz-2012/tools/MgmtMsgClient/Client.h 2012-06-10 21:26:50 UTC (rev 202) @@ -0,0 +1,16 @@ +/** + * @file Client.h + * @author Tomasz Kleinschmidt + * + * @brief Client class definition. + */ + +#ifndef CLIENT_H +#define CLIENT_H + +#include "OptBuffer.h" + +void mainClient(OptBuffer* optBuf); + +#endif /* CLIENT_H */ + Modified: branches/tomasz-2012/tools/MgmtMsgClient/Help.cpp =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/Help.cpp 2012-06-09 12:35:08 UTC (rev 201) +++ branches/tomasz-2012/tools/MgmtMsgClient/Help.cpp 2012-06-10 21:26:50 UTC (rev 202) @@ -1,8 +1,10 @@ /** * @file Help.cpp - * This class will be used to print help messages + * @author Tomasz Kleinschmidt * * @brief Help class implementation + * + * This class will be used to print help messages. */ #include <stdio.h> @@ -20,9 +22,9 @@ " -a --address [address] Set IPv4 address of the server (default set to %s)\n" " -m --message [type] [action] <value> Send management message of given type and value\n" " -h --help Display this message\n" - " -p --port [port] Set port number the server is listening on (default set to %d)\n\n" + " -p --port [port] Set port number the server is listening on (default set to %s)\n\n" "Type '%s {-m --message} print' to print a list of management messages and assigned actions.\n", - appName, U_ADDRESS, U_PORT, appName); + appName, U_ADDRESS, PTP_GENERAL_PORT, appName); } /** Modified: branches/tomasz-2012/tools/MgmtMsgClient/Help.h =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/Help.h 2012-06-09 12:35:08 UTC (rev 201) +++ branches/tomasz-2012/tools/MgmtMsgClient/Help.h 2012-06-10 21:26:50 UTC (rev 202) @@ -1,5 +1,7 @@ /** * @file Help.h + * @author Tomasz Kleinschmidt + * * @brief Help class definition */ Modified: branches/tomasz-2012/tools/MgmtMsgClient/Makefile =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/Makefile 2012-06-09 12:35:08 UTC (rev 201) +++ branches/tomasz-2012/tools/MgmtMsgClient/Makefile 2012-06-10 21:26:50 UTC (rev 202) @@ -1,7 +1,7 @@ ## -*- Makefile -*- ## -## User: tomek -## Time: 2012-06-07 21:25:49 +## User: Tomasz Kleinschmidt +## Time: 2012-06-09 16:28:49 ## Makefile created by Oracle Solaris Studio. ## ## This file is generated automatically. @@ -24,11 +24,11 @@ all: $(TARGETDIR_MgmtMsgClient)/MgmtMsgClient ## Target: MgmtMsgClient -CPPFLAGS_MgmtMsgClient = \ - -IHelp.cpp \ - -IMgmtMsgClient.cpp OBJS_MgmtMsgClient = \ + $(TARGETDIR_MgmtMsgClient)/OptBuffer.o \ + $(TARGETDIR_MgmtMsgClient)/Client.o \ $(TARGETDIR_MgmtMsgClient)/Help.o \ + $(TARGETDIR_MgmtMsgClient)/Network.o \ $(TARGETDIR_MgmtMsgClient)/MgmtMsgClient.o USERLIBS_MgmtMsgClient = $(SYSLIBS_MgmtMsgClient) DEPLIBS_MgmtMsgClient = @@ -41,9 +41,18 @@ # Compile source files into .o files +$(TARGETDIR_MgmtMsgClient)/OptBuffer.o: $(TARGETDIR_MgmtMsgClient) OptBuffer.cpp + $(COMPILE.cc) $(CCFLAGS_MgmtMsgClient) $(CPPFLAGS_MgmtMsgClient) -o $@ OptBuffer.cpp + +$(TARGETDIR_MgmtMsgClient)/Client.o: $(TARGETDIR_MgmtMsgClient) Client.cpp + $(COMPILE.cc) $(CCFLAGS_MgmtMsgClient) $(CPPFLAGS_MgmtMsgClient) -o $@ Client.cpp + $(TARGETDIR_MgmtMsgClient)/Help.o: $(TARGETDIR_MgmtMsgClient) Help.cpp $(COMPILE.cc) $(CCFLAGS_MgmtMsgClient) $(CPPFLAGS_MgmtMsgClient) -o $@ Help.cpp +$(TARGETDIR_MgmtMsgClient)/Network.o: $(TARGETDIR_MgmtMsgClient) Network.cpp + $(COMPILE.cc) $(CCFLAGS_MgmtMsgClient) $(CPPFLAGS_MgmtMsgClient) -o $@ Network.cpp + $(TARGETDIR_MgmtMsgClient)/MgmtMsgClient.o: $(TARGETDIR_MgmtMsgClient) MgmtMsgClient.cpp $(COMPILE.cc) $(CCFLAGS_MgmtMsgClient) $(CPPFLAGS_MgmtMsgClient) -o $@ MgmtMsgClient.cpp @@ -53,7 +62,10 @@ clean: rm -f \ $(TARGETDIR_MgmtMsgClient)/MgmtMsgClient \ + $(TARGETDIR_MgmtMsgClient)/OptBuffer.o \ + $(TARGETDIR_MgmtMsgClient)/Client.o \ $(TARGETDIR_MgmtMsgClient)/Help.o \ + $(TARGETDIR_MgmtMsgClient)/Network.o \ $(TARGETDIR_MgmtMsgClient)/MgmtMsgClient.o $(CCADMIN) rm -f -r $(TARGETDIR_MgmtMsgClient) Modified: branches/tomasz-2012/tools/MgmtMsgClient/MgmtMsgClient.cpp =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/MgmtMsgClient.cpp 2012-06-09 12:35:08 UTC (rev 201) +++ branches/tomasz-2012/tools/MgmtMsgClient/MgmtMsgClient.cpp 2012-06-10 21:26:50 UTC (rev 202) @@ -1,8 +1,10 @@ /** * @file MgmtMsgClient.cpp - * Used as a glue for all of the client classes + * @author Tomasz Kleinschmidt * * @brief Main file for the PTPd Management Message Client + * + * Used as an interface between a user and the application. */ #include <cstdlib> @@ -11,7 +13,9 @@ #include <stdlib.h> #include <string.h> -#include "Help.h" +#include "Client.h" +#include "const.h" +#include "OptBuffer.h" using namespace std; @@ -24,6 +28,7 @@ */ int main(int argc, char** argv) { int c; + OptBuffer* optBuf = new OptBuffer(argv[0]); while (1) { @@ -65,23 +70,30 @@ break; case 'h': - printHelp(argv[0]); + printf ("option -h\n"); + optBuf->help_print = true; + //printHelp(argv[0]); + //return 0; break; case 'a': printf ("option -a with value `%s'\n", optarg); + memcpy(optBuf->u_address, optarg, MAX_ADDR_STR_LEN + 1); break; case 'm': printf ("option -m with value `%s'\n", optarg); + //Print messages list and exit if (strcmp(optarg, "print") == 0) - printMgmtMsgsList(); + optBuf->msg_print = true; + //printMgmtMsgsList(); break; case 'p': printf ("option -p with value `%s'\n", optarg); + memcpy(optBuf->u_port, optarg, MAX_PORT_STR_LEN + 1); break; case '?': @@ -107,6 +119,8 @@ printf ("%s ", argv[optind++]); putchar ('\n'); } + + mainClient(optBuf); return 0; } Added: branches/tomasz-2012/tools/MgmtMsgClient/Network.cpp =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/Network.cpp (rev 0) +++ branches/tomasz-2012/tools/MgmtMsgClient/Network.cpp 2012-06-10 21:26:50 UTC (rev 202) @@ -0,0 +1,97 @@ +/** + * @file Network.cpp + * @author Tomasz Kleinschmidt + * + * @brief Network class implementation. + * + * This class will be used to initialize and manage network connections + * between the client and the server. + */ + +#include <err.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <unistd.h> + +#include "Network.h" + +/** + * This method will be used to initialize socket communication between client + * and server. + * + * @param hostName Hostname or IP address of the server. + * @param port Port number that the server is listening on + */ +int initNetwork(char* hostName, char* port) { + int error, sockFd, yes = 1; + struct addrinfo sockHints, *sockRes; + + memset(&sockHints, 0, sizeof(sockHints)); + + sockHints.ai_socktype = SOCK_DGRAM; + sockHints.ai_family = AF_INET; + + //get host address data + error = getaddrinfo(hostName, port, &sockHints, &sockRes); + + if (error) { + errx(1, "%s", gai_strerror(error)); + exit(1); + } + + //make a socket with characteristics identical to server's + sockFd = socket(sockRes->ai_family, sockRes->ai_socktype, sockRes->ai_protocol); + + if (sockFd == -1) { + perror("socket()"); + exit(1); + } + + /*//set reuse port to on to allow multiple binds per host + error = setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); + + if (error != 0) { + perror("setsockopt()"); + exit(1); + } + + //bind to the port + error = bind(sockFd, sockRes->ai_addr, sockRes->ai_addrlen); + + if (error != 0) { + perror("bind()"); + exit(1); + }*/ + + //A test send message + /*int numbytes; + char msg[10] = "ala"; + if ((numbytes = sendto(sockFd, msg, strlen(msg), 0, sockRes->ai_addr, sockRes->ai_addrlen)) == -1) { + perror("talker: sendto"); + exit(1); + }*/ + + freeaddrinfo(sockRes); + + printf("Client connected to %s on port %s\n", hostName, port); + + return sockFd; +} + +void disableNetwork(int sockFd) { + close(sockFd); +} + +/* TODO: Send and receive methods will be implemented when the socket structure + * will be ready */ +void sendMessage() { + +} + +void receiveMessage() { + +} Added: branches/tomasz-2012/tools/MgmtMsgClient/Network.h =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/Network.h (rev 0) +++ branches/tomasz-2012/tools/MgmtMsgClient/Network.h 2012-06-10 21:26:50 UTC (rev 202) @@ -0,0 +1,18 @@ +/** + * @file Network.h + * @author Tomasz Kleinschmidt + * + * @brief Network class definition. + */ + +#ifndef NETWORK_H +#define NETWORK_H + +int initNetwork(char* port, char* hostName); +void disableNetwork(int sockFd); + +void sendMessage(); +void receiveMessage(); + +#endif /* NETWORK_H */ + Added: branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.cpp =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.cpp (rev 0) +++ branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.cpp 2012-06-10 21:26:50 UTC (rev 202) @@ -0,0 +1,36 @@ +/** + * @file OptBuffer.cpp + * @author Tomasz Kleinschmidt + * + * @brief OptBuffer class implementation + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "OptBuffer.h" +#include "const.h" + +/** + * The constructor should allocate memory for all the options and assign default values + */ +OptBuffer::OptBuffer(char* appName) { + this->help_arg = appName; + + this->u_address = (char*)calloc(MAX_ADDR_STR_LEN, sizeof(char)); + sprintf(this->u_address, "%s", U_ADDRESS); + + this->u_port = (char*)calloc(MAX_PORT_STR_LEN, sizeof(char)); + sprintf(this->u_port, "%s", PTP_GENERAL_PORT); + + this->msg_print = false; + this->help_print = false; +} + +/** + * The deconstructor should free the memory + */ +OptBuffer::~OptBuffer() { + free(this->u_address); + free(this->u_port); +} Added: branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.h =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.h (rev 0) +++ branches/tomasz-2012/tools/MgmtMsgClient/OptBuffer.h 2012-06-10 21:26:50 UTC (rev 202) @@ -0,0 +1,28 @@ +/** + * @file OptBuffer.h + * @author Tomasz Kleinschmidt + * + * @brief OptBuffer class definition + */ + +#ifndef OPTBUFFER_H +#define OPTBUFFER_H + +class OptBuffer { +public: + OptBuffer(char* appName); + virtual ~OptBuffer(); + + char* help_arg; + char* u_address; + char* u_port; + + bool msg_print; + bool help_print; + +private: + +}; + +#endif /* OPTBUFFER_H */ + Modified: branches/tomasz-2012/tools/MgmtMsgClient/const.h =================================================================== --- branches/tomasz-2012/tools/MgmtMsgClient/const.h 2012-06-09 12:35:08 UTC (rev 201) +++ branches/tomasz-2012/tools/MgmtMsgClient/const.h 2012-06-10 21:26:50 UTC (rev 202) @@ -1,5 +1,7 @@ /** * @file const.h + * @author Tomasz Kleinschmidt + * * @brief Constants used throughout the code */ @@ -7,7 +9,12 @@ #define CONST_H #define U_ADDRESS "127.0.0.1" -#define U_PORT 10018 +#define PTP_EVENT_PORT "319" +#define PTP_GENERAL_PORT "320" + +#define MAX_ADDR_STR_LEN 50 +#define MAX_PORT_STR_LEN 50 + #endif /* CONST_H */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |