commit af41084368d06382c48c132a40eb7992e06d8c14 Author: kts Date: Thu Aug 1 13:54:20 2013 -0700 Initial commit of bcast, has terminal interface and x11 interface stub diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..04d1c56 --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ +OUT = bcast +PREFIX=./ +OBJS = main.o net.o common.o +CON_OBJS = con/console.o +CC = gcc +DEBUG = -g +CFLAGS = -Wall -c $(DEBUG) +LFLAGS = -Wall $(DEBUG) +X11_OBJS = x11/x11.o +X11_LFLAGS = -L/usr/X11R6/lib -lX11 + +$(OUT): $(CON_OBJS) $(OBJS) + $(CC) $(OBJS) $(CON_OBJS) $(LFLAGS) -o $(OUT) + +ccast: $(OBJS) $(CON_OBJS) + $(CC) $(OBJS) $(CON_OBJS) $(LFLAGS) -o ccast + +xcast: $(OBJS) $(X11_OBJS) + $(CC) $(OBJS) $(X11_OBJS) $(LFLAGS) $(X11_LFLAGS) -o xcast + +all: bcast ccast xcast + +clean: + rm $(OBJS) $(CON_OBJS) $(X11_OBJS) ccast xcast $(OUT) + +install: + cp $(OUT) $(PREFIX)/$(OUT) + +net.o: net.c net.h macros.h + $(CC) $(CFLAGS) -c net.c + +main.o: main.c net.h common.h con/console.h macros.h ui_common.h + $(CC) $(CFLAGS) -c main.c + +common.o: common.c common.h macros.h + $(CC) $(CFLAGS) -c common.c + +console.o: con/console.c con/console.h ui_common.h + $(CC) $(CFLAGS) -c con/console.c + +x11.o: x11/x11.c x11/x11.h ui_common.h x11/x11_assets.h + $(CC) $(CFLAGS) -c x11/x11.c diff --git a/common.c b/common.c new file mode 100644 index 0000000..5e3fb4a --- /dev/null +++ b/common.c @@ -0,0 +1,145 @@ +#include "common.h" + +#if __APPLE__ +char * strndup (const char *s, size_t n) { + char *result; + size_t len = strlen (s); + + if (n < len) + len = n; + + result = (char *) malloc (len + 1); + if (!result) + return 0; + + result[len] = '\0'; + return (char *) memcpy (result, s, len); +} +#endif + +void buildCommands() { + //char** commands = NULL; +} + +//void addCommand(const char *command_string, void(*function)()) { +void addCommand(const char *command_string, void(*function)) { + int hash = generateHash(command_string, COMMAND_HASH_SIZE); + printf("command '%s' added with hash %d\n", command_string, hash); + commands_table[hash] = function; +} + +char **getCommand(const char* string) { + char** command_array = NULL; + + int i = 0; + int j = 1; + char last_pos = 0; + + char current_char = string[i]; + short done = 0; + + while (!done) { + if (current_char == ' ') { + command_array = realloc(command_array, sizeof(char*)*j+1); + if (command_array == NULL) { + printf("ERROR, couldn't allocate memory for command_array\n"); + } + command_array[j] = strndup(string+last_pos+1, i-last_pos-1); + last_pos = i; + j++; + } else if (current_char == '\n') { + command_array = realloc(command_array, sizeof(char*)*j+1); + if (command_array == NULL) { + printf("ERROR, couldn't allocate memory for command_array\n"); + } + command_array[j] = strndup(string+last_pos+1, i-last_pos-1); + last_pos = i; + done = 1; + j++; + } + i++; + current_char = string[i]; + } + command_array[0] = (void*)(intptr_t)(j-1); + return command_array; +} + +void freeCommand(char **command_array) { + //printf("res[0] = %d\n", command_array[0]); + int j; + for (j = 1; j < (intptr_t)command_array[0]+1; j++) { + //printf("res[%d] = %s\n", j, command_array[j]); + free(command_array[j]); + } + free(command_array); +} + +int generateHash(const char* string, int table_size) { + int i, sum; + size_t string_length = strlen(string); + for (sum=0, i=0; i < string_length; i++) { + sum += string[i]; + } + return sum % table_size; +} + +void setConfig(const char* variable, const char* value) { + if (variable && value) { + int hash = generateHash(variable, CONFIG_HASH_SIZE); + //printf("\n%s's hash is %d\n", variable, hash); + config[hash] = realloc(config[hash], strlen(value)); + strcpy(config[hash], value); + + config_name[hash] = realloc(config_name[hash], strlen(variable)); + strcpy(config_name[hash], variable); + + printf("%s(%d) set to %s\n", config_name[hash], hash, config[hash]); + //printf("%s's value: %s\n", variable, config[hash]); + } else { + printf("usage: %s", SET_SYNTAX); + } +} + +void freeConfig() { + int i = 0; + for (i = 0;i < CONFIG_HASH_SIZE;i++) { + if (config[i]) { + free(config[i]); + free(config_name[i]); + } + } +} + +void addHelp(const char* variable, const char* value) { + int hash = generateHash(variable, COMMAND_HASH_SIZE); + help[hash] = realloc(help[hash], strlen(value)); + strcpy(help[hash], value); +} + +void freeHelp() { + int i; + for (i = 0;i < COMMAND_HASH_SIZE;i++) { + if (help[i]) { + free(help[i]); + } + } +} + +void addSyntax(const char* variable, const char* value) { + int hash = generateHash(variable, COMMAND_HASH_SIZE); + syntax[hash] = realloc(syntax[hash], strlen(value)); + strcpy(syntax[hash], value); +} + +void freeSyntax() { + int i; + for (i = 0;i < COMMAND_HASH_SIZE;i++) { + if (syntax[i]) { + free(syntax[i]); + } + } +} + +void quitProgram() { + is_running = 0; +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..ed98cef --- /dev/null +++ b/common.h @@ -0,0 +1,65 @@ +/* +=============================================================================== + +Common + +=============================================================================== +*/ +#ifndef COMMON_H +#define COMMON_H + +#include "macros.h" + +#include +#include +#include + +#include // for intptr_t http://stackoverflow.com/questions/5701450/getting-the-warning-cast-to-pointer-from-integer-of-different-size-from-the-fo#5703367 + + +#if __APPLE__ +char * strndup (const char *s, size_t n); +#endif +/* etc */ + +char *start_message[3]; + +char* command_array; + +char** getCommand(const char* string); +void freeCommand(char **command_array); + +int generateHash(const char* string, int table_size); +//void addCommand(const char *command_string, void(*function)(const void*, const void*, const void*)); +//void addCommand(const char *command_string, void(*function)()); +void addCommand(const char *command_string, void(*function)); +//void (*commands_table[64]) (const void*, const void*); +void (*commands_table[COMMAND_HASH_SIZE]) (); +//void(*function)(const void*, const void*, const void*) **commands[64]; // array of command strings, e.g., "quit", "save", etc. +//int *commands_table; // array of command hashes (integers). +int commands_length; // total of commands available +int hash_size; // err, change this. + +char *help[COMMAND_HASH_SIZE]; +char *syntax[COMMAND_HASH_SIZE]; + +char *config_name[CONFIG_HASH_SIZE]; +char (*config[CONFIG_HASH_SIZE]); +void setConfig(const char* variable, const char* value); + +int is_running; +void quitProgram(); + + +void addHelp(const char* variable, const char* value); +void addSyntax(const char* variable, const char* value); +void freeHelp(); +void freeSyntax(); +void freeConfig(); + +typedef struct { + int length; + char *arg; +} t_command; + +#endif diff --git a/con/console.c b/con/console.c new file mode 100644 index 0000000..1b30b6d --- /dev/null +++ b/con/console.c @@ -0,0 +1,213 @@ +#include "console.h" + +int initInterface() { + setAttribute(GREEN); + setAttribute(DIM); + printf("%s", START_MESSAGE); + clearAttributes(); + + showInterfaces(); + + showHelp(NULL); + setAttribute(MAGENTA); + setAttribute(UNDERSCORE); + printf("\n--ready--"); + clearAttributes(); + + interface_fd = STDIN; + FD_SET(interface_fd, &master_fds); + max_fd = interface_fd; + + showPrompt(); + return 0; +} + +int closeInterface() { + + return 0; +} + +int handleInterface() { + // TODO: change to while loop :S + if (read(interface_fd, data_buffer, 2048) < 0) { + perror("couldn't read() STDIN"); + } else { + if (data_buffer[0] == '/') { // denotes command + char **command = getCommand(data_buffer); + int command_hash = generateHash(command[1], COMMAND_HASH_SIZE); + if (!commands_table[command_hash]) { + clearLine(); + setAttribute(YELLOW); + printf("%s '%s'", UNKNOWN_MESSAGE, command[1]); + clearAttributes(); + } else { + if ((intptr_t)command[0] == 1) { // if only /command + (*commands_table[command_hash])(NULL); + } else if ((intptr_t)command[0] == 2) { // if 1 args + (*commands_table[command_hash])(command[2]); + } else if ((intptr_t)command[0] == 3) { // if 2 args + (*commands_table[command_hash])(command[2], command[3]); + } + } + //printf("\nhash of command %s: %d\n", command[1], generateHash(command[1], 64)); + freeCommand(command); + showPrompt(); + } else if (data_buffer[0] == '\n') { // return + fputs("\033[A\033[2K",stdout); + rewind(stdout); + ftruncate(1,0); + //fputs("\033[A",stdout); + fputs("\033[32m",stdout); + printf("> "); + fputs("\033[0m",stdout); + fflush(stdout); + } else { + if (is_listening) { + int i = 0; + int done = 0; + while (!done) { + if (data_buffer[i++] == '\n') + done = 1; + } + char message[i-1]; + strncpy(message, data_buffer, i-1); // cut the \n + message[i-1] = '\0'; + + clearLine(); + sendOpen(message); + showPrompt(); +// sendMulticast(send_socket, &group_socket, data_buffer, strlen(data_buffer)); +/* fputs("\033[A\033[2K",stdout); + rewind(stdout); + ftruncate(1,0); + fputs("\033[32m",stdout); + printf("> "); + fputs("\033[0m",stdout); + fflush(stdout);*/ + } else { + setAttribute(RED); + printf("not listening"); + clearAttributes(); + showPrompt(); + } + } + } + return 0; +} + +int handleInput(const char *data_buffer) { + fputs("\033[1A", stdout); + printf("\n"); + fputs("\033[2K",stdout); // move up and clear + rewind(stdout); + ftruncate(1,0); + receiveOpen(data_buffer, DATAGRAM_SIZE); +// fputs("\033[1B",stdout); // move cursor back down +// fputs("\033[u", stdout); + fflush(stdout); + showPrompt(); + /*fputs("\033[2K",stdout); + rewind(stdout); + ftruncate(1,0); + printf("data_in: %s", data_buffer); + showPrompt();*/ + return 0; +} + +void showHelp(const char *value) { + setAttribute(MAGENTA); + setAttribute(UNDERSCORE); + printf("--help--\n"); + clearAttributes(); + if (value) { + int hash = generateHash(value, COMMAND_HASH_SIZE); + if (help[hash]) { + setAttribute(CYAN); + printf("%s", help[hash]); + if (syntax[hash]) { + printf("\nusage: %s", syntax[hash]); + } + clearAttributes(); + } + } else { + setAttribute(CYAN); + printf("%s", HELP_MESSAGE); + clearAttributes(); + } +} + +void showVariables() { + setAttribute(MAGENTA); + setAttribute(UNDERSCORE); + printf("--variables--\n"); + clearAttributes(); + int i; + for (i = 0;i < CONFIG_HASH_SIZE;i++) { + if (config[i]) { + setAttribute(GREEN); + printf("%s(%d): ", config_name[i], i); + clearAttributes(); + printf("%s\n", config[i]); + } + } + clearAttributes(); +} + +void showInterfaces() { + int i; + fputs("\033[4m\033[35m", stdout); + printf("--%d interfaces detected--\n", interfaces); + fputs("\033[0m",stdout); + fputs("\033[32m", stdout); + for (i = 0; i < interfaces; i++) { + if (ip_list[i]) { + printf("%s: %s\n", dev_list[i], ip_list[i]); + } + } + fputs("\033[0m",stdout); + clearAttributes(); +} + +void setAttribute(const int attribute) { + switch(attribute) { + case RED: + fputs("\033[31m",stdout); + break; + case YELLOW: + fputs("\033[33m",stdout); + break; + case GREEN: + fputs("\033[32m", stdout); + break; + case CYAN: + fputs("\033[36m", stdout); + break; + case MAGENTA: + fputs("\033[35m", stdout); + break; + case UNDERSCORE: + fputs("\033[4m", stdout); + break; + case DIM: + fputs("\033[2m", stdout); + break; + } +} + +void clearAttributes() { + fputs("\033[0m", stdout); +} + +void showPrompt() { + fputs("\033[32m",stdout); + printf("\n> "); + fputs("\033[0m",stdout); + fflush(stdout); +} + +void clearLine() { + fputs("\033[A\033[2K",stdout); + rewind(stdout); + ftruncate(1,0); +} + diff --git a/con/console.h b/con/console.h new file mode 100644 index 0000000..61cd630 --- /dev/null +++ b/con/console.h @@ -0,0 +1,35 @@ +#ifndef CONSOLE_H +#define CONSOLE_H + +#include +#include +#include + +#include // ftruncate + +#include "../ui_common.h" +#include "../common.h" +#include "../macros.h" +#include "../net.h" + +#define RED 31 +#define GREEN 32 +#define YELLOW 33 +#define CYAN 34 +#define MAGENTA 35 + +#define UNDERSCORE 4 +#define DIM 2 + +/* ui */ +void showHelp(const char *value); + +void showInterfaces(); +void showVariables(); +void showPrompt(); +void clearLine(); + +void setAttribute(const int); +void clearAttributes(); + +#endif diff --git a/macros.h b/macros.h new file mode 100644 index 0000000..17d369e --- /dev/null +++ b/macros.h @@ -0,0 +1,51 @@ +#ifndef MACROS_H +#define MACROS_H + +#define START_MESSAGE " ______ _______ _______ _______ _______\n |_____] | |_____| |______ | \n |_____] |_____ | | ______| |\n\n" + +/* strings, foo' */ +#define QUIT_COMMAND "quit" +#define QUIT_MESSAGE "さよなら!" +#define QUIT_HELP "shuts down running sockets and quits the program" + +#define SET_COMMAND "set" +#define SET_MESSAGE "variable: " +#define SET_HELP "sets a variable to a value" +#define SET_SYNTAX "/set variable value" + +#define LIST_COMMAND "list" +#define LIST_HELP "lists all variables and their values\nuse /set to change a variable" + +#define START_COMMAND "start" +#define START_HELP "starts either listen or send if specified, otherwise starts both" +#define START_SYNTAX "/start listen|send" +#define START_LISTEN_COMMAND "listen" +#define START_SEND_COMMAND "send" + +#define STOP_COMMAND "stop" +#define STOP_HELP "stops either listen or send if specified, otherwise stops both" +#define STOP_SYNTAX "/stop {listen|send}" +#define STOP_LISTEN_COMMAND "listen" +#define STOP_SEND_COMMAND "send" + +#define RESTART_COMMAND "restart" +#define RESTART_HELP "restarts listen and/or send if either are running" +#define RESTART_SYNTAX "/restart {listen|send}" +#define RESTART_LISTEN_COMMAND "listen" +#define RESTART_SEND_COMMAND "send" + +#define HELP_COMMAND "help" +#define HELP_MESSAGE "type a message and hit return to send\npreface commands with '/'\navailable commands: help, start, stop, restart, set, list, quit" +#define HELP_HELP "the help command, obv." +#define HELP_SYNTAX "/help {command}" + +#define UNKNOWN_MESSAGE "unknown command" + +/* ints, foo*2 */ + +#define STDIN 0 +#define DATAGRAM_SIZE 512 +#define COMMAND_HASH_SIZE 64 +#define CONFIG_HASH_SIZE 64 + +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..c191572 --- /dev/null +++ b/main.c @@ -0,0 +1,116 @@ +#include "macros.h" +#include "net.h" +#include "common.h" +#include "con/console.h" +#include "ui_common.h" + +#include + +#define STDIN 0 +#define DATAGRAM_SIZE 512 + +#define QUIT_STRING "quit" +#define QUIT_MESSAGE "さよなら!" + +#define NICK_STRING "nick" +#define NICK_MESSAGE "nick set to" +#define NICK_SYNTAX "/nick new_nickname" + +#define SET_STRING "set" + +#define SET_IP_STRING "ip" +#define SET_IP_MESSAGE "(re)start send/listen sockets to use new IP" +#define SET_PORT_STRING "port" + +#define START_STRING "start" +#define START_SYNTAX "/start listen|send" +#define START_LISTEN "listen" +#define START_LISTEN_MESSAGE "--starting listen--" +#define START_SEND "send" +#define START_SEND_MESSAGE "--starting send--" + +#define STOP_STRING "stop" +#define STOP_LISTEN "listen" +#define STOP_LISTEN_MESSAGE "--stopping listen--" +#define STOP_SEND "send" +#define STOP_SEND_MESSAGE "--stopping send--" + +#define RESTART_STRING "restart" +#define RESTART_MESSAGE "--restarting sockets--" + +#define HELP_STRING "help" + +#define UNKNOWN_CMD_MESSAGE "unknown command" + +int main() { + char data_buffer[2047]; + + FD_ZERO(&master_fds); + FD_ZERO(&read_fds); + + getInterfaces(); + initNetwork(); + + addCommand(QUIT_COMMAND, quitProgram); + addCommand(HELP_COMMAND, showHelp); + addCommand(SET_COMMAND, setConfig); + addCommand(LIST_COMMAND, showVariables); + addCommand(RESTART_COMMAND, restartSockets); + addCommand(START_COMMAND, startSockets); + addCommand(STOP_COMMAND, stopSockets); + + + addCommand("open", sendOpen); + + addHelp(SET_COMMAND, SET_HELP); + addSyntax(SET_COMMAND, SET_SYNTAX); + addHelp(LIST_COMMAND, LIST_HELP); + addHelp(START_COMMAND, START_HELP); + addSyntax(START_COMMAND, START_SYNTAX); + addHelp(STOP_COMMAND, STOP_HELP); + addSyntax(STOP_COMMAND, STOP_SYNTAX); + addHelp(RESTART_COMMAND, RESTART_HELP); + addSyntax(RESTART_COMMAND, RESTART_SYNTAX); + addHelp(QUIT_COMMAND, QUIT_HELP); + addHelp(HELP_COMMAND, HELP_HELP); + addSyntax(HELP_COMMAND, HELP_SYNTAX); + + if (initInterface() == 0) { + is_running = 1; + } else { + printf("error, couldn't start interface...\n"); + } + + while(is_running) { + //for(;;) { + read_fds = master_fds; + if (select(max_fd+1, &read_fds, NULL, NULL, NULL) == -1) { + perror("select"); + return -1; + } + for (i = 0; i <= max_fd; i++) { + if (FD_ISSET(i, &read_fds)) { + if (i == interface_fd) { + handleInterface(); + } else if (i == listen_socket) { + if (recv(listen_socket, data_buffer, DATAGRAM_SIZE, 0) < 0) { + perror("couldn't read() listen_socket"); + } else { + handleInput(data_buffer); + } + } + } + } + } + closeInterface(); + //freeCommands(); + stopSockets(NULL); + freeInterfaces(); + freeHelp(); + freeSyntax(); + freeConfig(); + setAttribute(GREEN); + printf("%s\n", QUIT_MESSAGE); + clearAttributes(); + return 0; +} diff --git a/mockup.xcf b/mockup.xcf new file mode 100644 index 0000000..804885d Binary files /dev/null and b/mockup.xcf differ diff --git a/net.c b/net.c new file mode 100644 index 0000000..64c2779 --- /dev/null +++ b/net.c @@ -0,0 +1,437 @@ +#include "net.h" + +int initNetwork() { + #if defined(WIN32) + WORD wVersionRequested; + WSADATA wsaData; + int err; + /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ + wVersionRequested = MAKEWORD(2, 2); + err = WSAStartup(wVersionRequested, &wsaData); + if (err != 0) { + /* Tell the user that we could not find a usable */ + /* Winsock DLL. */ + printf("WSAStartup failed with error: %d\n", err); + } + #endif + + setConfig("port", "7332"); + + char *hostname[15]; + //char hostname; + gethostname(&hostname, 16); + + char *username = getenv("USER"); + + size_t nick_length = strlen(hostname)+strlen(username)+1; + char nick[nick_length]; + int i = 0; + int j = 0; + while(jsin_addr, ip, sizeof(ip))) + return; + + ip_list[i] = malloc(strlen(ip)); + setString(ip_list[i], ip); + printf("%s - %s\n", ifreq[i].ifr_name, ip); + dev_list[i] = malloc(strlen(ifreq[i].ifr_name)); + setString(dev_list[i], ifreq[i].ifr_name); + } + close(test_socket); + setConfig("ip", ip_list[interfaces-1]); +} + +void freeInterfaces() { + int i; + for (i = 0;i < interfaces;i++) { + if (ip_list[i]) { + free(ip_list[i]); + } + } +} diff --git a/net.h b/net.h new file mode 100644 index 0000000..2e7b802 --- /dev/null +++ b/net.h @@ -0,0 +1,101 @@ +/* +=============================================================================== + +Net + +=============================================================================== +*/ +#ifndef NET_H +#define NET_H + +#include + +#if defined (WIN32) + #include + #include + #include +#else + #include + #include + #include +#endif + +#include +#include +#include +#include +#include + +#include // +#include // for ifreq + +#include "common.h" // for config +#include "con/console.h" + +#include +#include + +#define MESSAGE_OPEN 0 + +struct in_addr local_interface; +struct sockaddr_in group_socket; +struct ip_mreq group; +int listen_socket; +int send_socket; + +/* "bools" */ +int is_listening; +int is_sending; +int is_group_socket_set; + +/* ifreq */ +struct ifconf ifconf; +struct ifreq ifreq[31]; +int interfaces; +char *ip_list[16]; +char *dev_list[16]; + +/* listen */ +struct timeval listen_tv; +fd_set master_fds; +fd_set read_fds; +int max_fd; +int i; + +char data_buffer[512]; +int data_buffer_length; + +char ip_address[15]; +char port[15]; + +int setIp(const char string[]); + +int initNetwork(); + +void getInterfaces(); // same v +void freeInterfaces(); // change to int + +void startSockets(const char *value); +void stopSockets(const char *value); +void restartSockets(const char *value); + +int startSendSocket(); +int stopSendSocket(); +int openSendSocket(); +int closeSendSocket(); +int startListenSocket(); +int stopListenSocket(); +int openListenSocket(); +int closeListenSocket(); +int receiveSocket(int send_socket); +int checkSocket(); +int sendMulticast(int send_socket, struct sockaddr_in *group_socket, char data_buffer[], int data_length); +void sendOpen(); + +void receiveOpen(const char packet[], size_t packet_size); +int handleOpenMessage(const char packet[]); + +int setString(char *destination, const char source[]); +char *stripString(const char string[]); + +#endif diff --git a/old/Display.cpp b/old/Display.cpp new file mode 100644 index 0000000..cd0b131 --- /dev/null +++ b/old/Display.cpp @@ -0,0 +1,9 @@ +#include "Display.h" + +Display::Display() { + std::cout << "Display::Display()" << std::endl; +}; + +Display::~Display() { + std::cout << "Display::~Display()" << std::endl; +}; diff --git a/old/Display.h b/old/Display.h new file mode 100644 index 0000000..bb802a3 --- /dev/null +++ b/old/Display.h @@ -0,0 +1,21 @@ +/* +=============================================================================== + +Display + +=============================================================================== +*/ +#ifndef DISPLAY_H +#define DISPLAY_H + +#include + +class Display { +// private: + public: + Display(); + ~Display(); +// void Begin(); +// void End(); +}; +#endif diff --git a/old/Display.o b/old/Display.o new file mode 100644 index 0000000..b0cdfa6 Binary files /dev/null and b/old/Display.o differ diff --git a/old/Listener.cpp b/old/Listener.cpp new file mode 100644 index 0000000..b67e89c --- /dev/null +++ b/old/Listener.cpp @@ -0,0 +1,15 @@ +#include "Listener.h" + +Listener::Listener(short port_) { + this->port = port; + std::cout << "Listener::Listener()" << std::endl; +}; + +Listener::~Listener() { + std::cout << "Listener::~Listener()" << std::endl; +}; + +// main loop, so to speak. +void Listener::Run() { + std::cout << "Listener::Run()" << std::endl; +}; diff --git a/old/Listener.h b/old/Listener.h new file mode 100644 index 0000000..f5c8d86 --- /dev/null +++ b/old/Listener.h @@ -0,0 +1,23 @@ +/* +=============================================================================== + +Listener + +=============================================================================== +*/ +#ifndef LISTENER_H +#define LISTENER_H + +#include + +class Listener { + private: + short port; + void Run(); + public: + Listener(short port); + ~Listener(); +// void Begin(); +// void End(); +}; +#endif diff --git a/old/Listener.o b/old/Listener.o new file mode 100644 index 0000000..358a965 Binary files /dev/null and b/old/Listener.o differ diff --git a/old/Makefile b/old/Makefile new file mode 100644 index 0000000..597d5ef --- /dev/null +++ b/old/Makefile @@ -0,0 +1,25 @@ +OUT = bcast +PREFIX=./ +OBJS = main.o Listener.o Display.o +CXX = g++ +DEBUG = -g +CXXFLAGS = -std=gnu++11 -Wall -c $(DEBUG) +LFLAGS = -Wall $(DEBUG) + +$(OUT): $(OBJS) + $(CXX) $(OBJS) $(LFLAGS) -o $(OUT) + +clean: + rm *.o $(OUT) + +install: + cp $(OUT) $(PREFIX)/$(OUT) + +Listener.o: Listener.cpp Listener.h + $(CXX) $(CXXFLAGS) -c Listener.cpp + +Display.o: Display.cpp Display.h + $(CXX) $(CXXFLAGS) -c Display.cpp + +main.o: main.cpp Listener.h Display.h + $(CXX) $(CXXFLAGS) -c main.cpp diff --git a/old/bcast b/old/bcast new file mode 100755 index 0000000..1e147bd Binary files /dev/null and b/old/bcast differ diff --git a/old/main.cpp b/old/main.cpp new file mode 100644 index 0000000..104bfae --- /dev/null +++ b/old/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "Listener.h" +#include "Display.h" + +using namespace std; + +int main() { + Listener *listener = new Listener(7331); + Display *display = new Display(); +// dia_val_ai->Begin(); + delete listener; + delete display; +} diff --git a/old/main.o b/old/main.o new file mode 100644 index 0000000..0b09df2 Binary files /dev/null and b/old/main.o differ diff --git a/old/mcast b/old/mcast new file mode 100755 index 0000000..99cc815 Binary files /dev/null and b/old/mcast differ diff --git a/old/mcast.c b/old/mcast.c new file mode 100644 index 0000000..ec5ac12 --- /dev/null +++ b/old/mcast.c @@ -0,0 +1,160 @@ + /* Send Multicast Datagram code example. */ + + #include + + #include + + #include + + #include + + #include + + #include + + struct in_addr localInterface; + + struct sockaddr_in groupSock; + + int sd; + + char databuf[1024] = "Multicast test message lol!"; + + int datalen = sizeof(databuf); + + + + int main (int argc, char *argv[ ]) + + { + + /* Create a datagram socket on which to send. */ + + sd = socket(PF_INET, SOCK_DGRAM, 0); + + if(sd < 0) + + { + + perror("Opening datagram socket error"); + + exit(1); + + } + + else + + printf("Opening the datagram socket...OK.\n"); + + + + /* Initialize the group sockaddr structure with a */ + + /* group address of 225.1.1.1 and port 5555. */ + + memset((char *) &groupSock, 0, sizeof(groupSock)); + + groupSock.sin_family = PF_INET; + + groupSock.sin_addr.s_addr = inet_addr("226.1.1.1"); + + groupSock.sin_port = htons(7332); + + + + /* Disable loopback so you do not receive your own datagrams. + + { + + char loopch = 0; + + if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch, sizeof(loopch)) < 0) + + { + + perror("Setting IP_MULTICAST_LOOP error"); + + close(sd); + + exit(1); + + } + + else + + printf("Disabling the loopback...OK.\n"); + + } + + */ + + + + /* Set local interface for outbound multicast datagrams. */ + + /* The IP address specified must be associated with a local, */ + + /* multicast capable interface. */ + + localInterface.s_addr = inet_addr("192.168.182.51"); + + if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0) + + { + + perror("Setting local interface error"); + + exit(1); + + } + + else + + printf("Setting the local interface...OK\n"); + + /* Send a message to the multicast group specified by the*/ + + /* groupSock sockaddr structure. */ + + /*int datalen = 1024;*/ + + if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0) + + {perror("Sending datagram message error");} + + else + + printf("Sending datagram message...OK\n"); + + + + /* Try the re-read from the socket if the loopback is not disable + + if(read(sd, databuf, datalen) < 0) + + { + + perror("Reading datagram message error\n"); + + close(sd); + + exit(1); + + } + + else + + { + + printf("Reading datagram message from client...OK\n"); + + printf("The message is: %s\n", databuf); + + } + + */ + + return 0; + + } + diff --git a/old/mcast_client b/old/mcast_client new file mode 100755 index 0000000..01da409 Binary files /dev/null and b/old/mcast_client differ diff --git a/old/mcast_client.c b/old/mcast_client.c new file mode 100644 index 0000000..dd90cc9 --- /dev/null +++ b/old/mcast_client.c @@ -0,0 +1,172 @@ + + + /* Receiver/client multicast Datagram example. */ + + #include + + #include + + #include + + #include + + #include + + #include + + + + struct sockaddr_in localSock; + + struct ip_mreq group; + + int sd; + + int datalen; + + char databuf[1024]; + + + + int main(int argc, char *argv[]) + + { + + /* Create a datagram socket on which to receive. */ + + sd = socket(AF_INET, SOCK_DGRAM, 0); + + if(sd < 0) + + { + + perror("Opening datagram socket error"); + + exit(1); + + } + + else + + printf("Opening datagram socket....OK.\n"); + + + + /* Enable SO_REUSEADDR to allow multiple instances of this */ + + /* application to receive copies of the multicast datagrams. */ + + { + + int reuse = 1; + + if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) + + { + + perror("Setting SO_REUSEADDR error"); + + close(sd); + + exit(1); + + } + + else + + printf("Setting SO_REUSEADDR...OK.\n"); + + } + + + + /* Bind to the proper port number with the IP address */ + + /* specified as INADDR_ANY. */ + + memset((char *) &localSock, 0, sizeof(localSock)); + + localSock.sin_family = AF_INET; + + localSock.sin_port = htons(7332); + + localSock.sin_addr.s_addr = INADDR_ANY; + + if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock))) + + { + + perror("Binding datagram socket error"); + + close(sd); + + exit(1); + + } + + else + + printf("Binding datagram socket...OK.\n"); + + + + /* Join the multicast group 226.1.1.1 on the local 203.106.93.94 */ + + /* interface. Note that this IP_ADD_MEMBERSHIP option must be */ + + /* called for each local interface over which the multicast */ + + /* datagrams are to be received. */ + + group.imr_multiaddr.s_addr = inet_addr("226.1.1.1"); + + group.imr_interface.s_addr = inet_addr("192.168.182.51"); + + if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) + + { + + perror("Adding multicast group error"); + + close(sd); + + exit(1); + + } + + else + + printf("Adding multicast group...OK.\n"); + + + + /* Read from the socket. */ + + datalen = sizeof(databuf); + + if(read(sd, databuf, datalen) < 0) + + { + + perror("Reading datagram message error"); + + close(sd); + + exit(1); + + } + + else + + { + + printf("Reading datagram message...OK.\n"); + + printf("The message from multicast server is: \"%s\"\n", databuf); + + } + + return 0; + + } + diff --git a/ui_common.h b/ui_common.h new file mode 100644 index 0000000..ddd576c --- /dev/null +++ b/ui_common.h @@ -0,0 +1,24 @@ +#ifndef UI_COMMON_H +#define UI_COMMON_H + +int initInterface(); +int closeInterface(); + +int interface_fd; + +int handleInterface(); +int handleInput(const char *data_buffer); + +/* remove */ +void showHelp(const char *value); +void showVariables(); + +void showPrompt(); + +void setAttribute(const int); +void clearAttributes(); + +//void clearLine(); + + +#endif diff --git a/x11/x11.c b/x11/x11.c new file mode 100644 index 0000000..6b92d19 --- /dev/null +++ b/x11/x11.c @@ -0,0 +1,413 @@ +#include "x11.h" + +void testFunc(){} + +void drawLogo() { +// char tmp[] = "#008800"; +// XParseColor(display, colormap, tmp, &tmp_color); +// XAllocColor(display, colormap, &tmp_color); +// XSetForeground(display, gc, tmp_color.pixel); + //XSetForeground(display, gc, 12812818); + srand(time(NULL)); + XSetForeground(display, gc, rand()); + + XDrawArc(display, window, gc, -26, -24, 72, 48, (260*64), (90*64)); + XDrawArc(display, window, gc, -28, -26, 76, 52, (260*64), (90*64)); + XDrawArc(display, window, gc, -32, -30, 80, 56, (260*64), (90*64)); + XDrawString(display, window, gc, 4, 13, "bcast", 5); + XDrawString(display, window, gc, 4, 13, "bcast", 5); +} + +int initInterface() { + x_logo.chars = START_MESSAGE; + x_logo.nchars = 60; + x_logo.delta = 10; + + display = XOpenDisplay(NULL); + screen = DefaultScreen(display); + int black = BlackPixel(display, screen); + int white = WhitePixel(display, screen); + + window = XCreateSimpleWindow(display, RootWindow(display, 0), 1, 1, + 240, 320, 0, white, black); + //window = XCreateSimpleWindow(display, RootWindow(display, 0), 1, 1, 256, 256, 0, BlackPixel (display, 0), BlackPixel(display, 0)); + XSetStandardProperties(display, window, "bcast", "bcast", None, NULL, 0, NULL); + XMapWindow(display, window); + + gc = XCreateGC(display, window, 0, 0); + XSetForeground(display, gc, WhitePixel(display, screen)); + XSetBackground(display, gc, BlackPixel(display, screen)); + + XSelectInput(display, window, + ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask + | ButtonReleaseMask | StructureNotifyMask + ); + + /* set our colormap */ +// int i; +// XColor tmp[255]; +// for (i=0;i<255;i++) { +// tmp[i].pixel = i; +// tmp[i].flags = DoRed|DoGreen|DoBlue; +// tmp[i].red = i*256; +// tmp[i].green = i*256; +// tmp[i].blue = i*256; +// } + +// colormap = XCreateColormap(display, RootWindow(display, screen), +// DefaultVisual(display, screen), AllocAll); +// XStoreColors(display, colormap, tmp, 255); +// XSetWindowColormap(display, window, colormap); + +// XSetForeground(display, gc, tmp[16].pixel); + + colormap = DefaultColormap(display, 0); + green_gc = XCreateGC(display, window, 0, 0); + char green[] = "#008800"; + XParseColor(display, colormap, green, &green_color); + XAllocColor(display, colormap, &green_color); + XSetForeground(display, green_gc, green_color.pixel); + + string_gc = XCreateGC(display, window, 0, 0); + XSetForeground(display, string_gc, BlackPixel(display, screen)); + XSetBackground(display, gc, WhitePixel(display, screen)); + + int offset_left = 240; + + quit_button = newButton(offset_left, 6, QUIT_COMMAND, strlen(QUIT_COMMAND), quitProgram); + offset_left = quit_button.x = 240-(quit_button.width+6); + addInput(&quit_button); + + stop_button = newButton(offset_left, 6, STOP_COMMAND, strlen(STOP_COMMAND), stopSockets); + offset_left = stop_button.x = offset_left-(stop_button.width+6); + addInput(&stop_button); + + start_button = newButton(offset_left, 6, START_COMMAND, strlen(START_COMMAND), startSockets); + offset_left = start_button.x = offset_left-(start_button.width+6); + addInput(&start_button); + + restart_button = newButton(offset_left, 6, RESTART_COMMAND, strlen(RESTART_COMMAND), startSockets); + offset_left = restart_button.x = offset_left-(restart_button.width+6); + addInput(&restart_button); + + offset_left = 240; + send_button = newButton(offset_left, 300, "send", 4, sendMessage); + offset_left = send_button.x = offset_left-(send_button.width+6); + addInput(&send_button); + + text_input = newTextInput(offset_left, 300, 27, sendMessage); + offset_left = text_input.x = offset_left-(text_input.width+6); + addInput(&text_input); + + offset_left = 8; + + list_button = newButton(offset_left, 275, LIST_COMMAND, strlen(LIST_COMMAND), showVariables); + offset_left = offset_left+(list_button.width+6); + addInput(&list_button); + + redrawInterface(); + + XFlush(display); + + interface_fd = ConnectionNumber(display); + FD_SET(interface_fd, &master_fds); + max_fd = interface_fd; + return 0; +} + +Input newButton(int x, int y, const void* data, size_t size, void(*callback)) { + Input new_input; + new_input.type = INPUT_BUTTON; + new_input.position = 0; + new_input.x = x; + new_input.y = y; + new_input.state = STATE_INACTIVE; + new_input.data = malloc(size-1); + //strcpy(new_input.data, data); + memcpy(new_input.data, data, size); + new_input.size = size; + new_input.width = new_input.size*7; + new_input.height = 13; + new_input.callback = callback; + new_input.previous = NULL; + new_input.next = NULL; + return(new_input); +} + +Input newTextInput(int x, int y, size_t size, void(*callback)) { + Input new_input; + new_input.type = INPUT_TEXT; + new_input.position = 0; + new_input.x = x; + new_input.y = y; + new_input.state = STATE_INACTIVE; + char string[size]; + int i; + for (i=0;inext = input; + input->previous = last_input; + last_input = input; + } + inputs[input_count] = input; + input_count++; + return 0; +} + +int drawInput(const Input *input) { + if (input->type == INPUT_BUTTON) { + if (input->state == STATE_INACTIVE) { + XFillRectangle(display, window, string_gc, input->x-1, input->y-1, input->size*7, 13); + XDrawRectangle(display, window, gc, input->x-2, input->y-2, input->size*7, 13); + XFillRectangle(display, window, gc, input->x, input->y, input->size*7, 13); + XDrawRectangle(display, window, gc, input->x, input->y, input->size*7, 13); + XDrawString(display, window, string_gc, input->x+(input->size/2)+1, input->y+(11), (char *)input->data, input->size); + } else if (input->state == STATE_ACTIVE) { + //XDrawRectangle(display, window, gc, input->x-2, input->y-2, input->size*7, 13); + XFillRectangle(display, window, string_gc, input->x, input->y, input->size*7, 13); // optional + XFillRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13); + XDrawRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13); + XDrawString(display, window, string_gc, input->x-1+(input->size/2)+1, input->y-1+(11), (char *)input->data, input->size); + } + } else if (input->type == INPUT_TEXT) { + drawInputText(input); + } + return(0); +} + +int drawInputText(const Input *input) { + if (input->state == STATE_INACTIVE) { + XFillRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13); + XDrawRectangle(display, window, string_gc, input->x-2, input->y-2, input->size*7, 13); + XFillRectangle(display, window, string_gc, input->x, input->y, input->size*7, 13); + XDrawRectangle(display, window, gc, input->x, input->y, input->size*7, 13); + XDrawString(display, window, gc, input->x+(input->size/2)+1, input->y+(11), (char *)input->data, input->size); + } else if (input->state == STATE_ACTIVE) { + //XDrawRectangle(display, window, gc, input->x-2, input->y-2, input->size*7, 13); + XFillRectangle(display, window, string_gc, input->x, input->y, input->size*7, 13); // optional + XFillRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13); + XDrawRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13); + XDrawString(display, window, string_gc, input->x-1+(input->size/2)+1, input->y-1+(11), (char *)input->data, input->size); + } + return 0; +} + +int closeInterface() { + freeInput(quit_button); + FD_CLR(interface_fd, &master_fds); + XFreeGC(display, gc); + XFreeGC(display, string_gc); + XDestroyWindow(display, window); + XCloseDisplay(display); + return 0; +} + +int redrawInterface() { + //XClearWindow(display, window); + XDrawRectangle(display, window, gc, 1, 1, 238, 318); + + Input *current_input; + int i; + for (i=0;i < input_count; i++) { + current_input = inputs[i]; + if (current_input) { + drawInput(current_input); + } else { + break; + } + } + +// @@ TODO: implement iterator +/* while (iterateInput(current_input)) { + drawInput(current_input); + }*/ + drawLogo(); + return 0; +} + +int iterateInput(Input *input) { + if (input->previous == NULL) { + printf("lol"); + return 0; + } else { + printf("real lol\n"); + input = (Input*)input->previous; + return 1; + } +} + +int clearInterface() { + XClearWindow(display, window); + return 0; +} + +void sendMessage(char *str, size_t string_length) { + printf("send: %s -- %d\n", (char *)text_input.data, text_input.size); + sendMulticast(send_socket, &group_socket, text_input.data, text_input.size); +} + +int handleInterface() { + while (XPending(display)) { + XNextEvent(display, &event); + if (event.type == Expose && event.xexpose.count == 0) { + redrawInterface(); + } + if (event.type == KeyPress && XLookupString(&event.xkey, key_text, 255, &key, 0) == 1) { +/* if (key_text[0] == 'q') { + quitProgram(); + printf("%s\n", QUIT_MESSAGE); + }*/ + if (active_input != NULL) { + textInput(active_input, key_text[0]); + } + redrawInterface(); // lol + } + if (event.type == ButtonPress) { + Input *current_input; + int i; + if (active_input != NULL) { + active_input->state = STATE_INACTIVE; + active_input = NULL; + } + for (i=0;i < input_count; i++) { + current_input = inputs[i]; + if (current_input) { + if (event.xbutton.x < current_input->x+current_input->width && event.xbutton.x > current_input->x + && event.xbutton.y < current_input->y+current_input->height && event.xbutton.y > current_input->y) { + if (current_input->type == INPUT_TEXT) { + active_input = current_input; + } + current_input->state = STATE_ACTIVE; + active_list[active_count] = current_input; + active_count++; + } + } else { + break; + } + } + redrawInterface(); + } else if (event.type == ButtonRelease) { + Input *current_input; + int i; + for (i=0;i < input_count; i++) { + current_input = inputs[i]; + if (current_input) { + if (current_input->state == STATE_ACTIVE) { + if (current_input->type == INPUT_TEXT) { + //(*current_input->callback)(NULL); + } else { + (*current_input->callback)(NULL); + current_input->state = STATE_INACTIVE; + } + } + } else { + break; + } + } +/* + for (i=0;icallback)(NULL); + } else { + (*active_list[i]->callback)(NULL); + active_list[i]->state = STATE_INACTIVE; + } + } else { + break; + } + } + active_count = 0;*/ + } + redrawInterface(); // lol + } + return 0; +} + +void textInput(Input *input, const char character) { + printf("%d hmm\n", character); + char string[input->size]; + memcpy(string, input->data, input->size); + + if ((int)character == 8) {// backspace + input->position--; + string[input->position] = ' '; + //string[input->position] = ' '; + } else if ((int)character == 13) { // return + (*input->callback)(&string, input->size); + } else { + string[input->position] = character; + input->position++; + } + memcpy(input->data, string, input->size); + + //input->data[1] = character; + printf("%c augh\n", character); +} + +int handleInput(const char *data_buffer) { + + return 0; +} + +void showHelp(const char *value) { + +} + +void showVariables() { + +} + +void setAttribute(const int attribute) { + +} + +void clearAttributes() { + +} + +// remove ? +void showPrompt() { + +} diff --git a/x11/x11.h b/x11/x11.h new file mode 100644 index 0000000..2470a17 --- /dev/null +++ b/x11/x11.h @@ -0,0 +1,59 @@ +#ifndef X11_H +#define X11_H +#include +#include +#include "x11_assets.h" + +#include "../ui_common.h" +#include "../common.h" +#include "../net.h" +#include "../macros.h" + +Display *display; +Window window; +int screen; +GC gc; +GC tmp_gc; +GC string_gc; +XColor tmp_color; + +XEvent event; +KeySym key; +char key_text[255]; + +XTextItem x_logo; + +// colors +Colormap colormap; +GC green_gc; +XColor green_color; + +int redrawInterface(); + +/* buttons */ +Input quit_button; +Input start_button; +Input stop_button; +Input restart_button; +Input list_button; +Input test_input; +Input send_button; + +Input text_input; + +Input newButton(int x, int y, const void* data, size_t size, void(*callback)); +Input newInput(int x, int y, const void* data, size_t size, void(*callback)); +Input newTextInput(int x, int y, size_t size, void(*callback)); + +void textInput(Input *input, const char character); + +int iterateInput(Input *input); +int addInput(Input *input); +void freeInput(Input input); +int drawInput(const Input *input); + +int drawInputText(const Input *input); + +void sendMessage(char *str, size_t string_length); + +#endif diff --git a/x11/x11_assets.h b/x11/x11_assets.h new file mode 100644 index 0000000..91334bf --- /dev/null +++ b/x11/x11_assets.h @@ -0,0 +1,52 @@ +#ifndef X11_ASSETS_H +#define X11_ASSETS_H + +#define INPUT_BUTTON 1 +#define INPUT_TEXT 2 + +#define STATE_INACTIVE 0 +#define STATE_ACTIVE 1 + +int button_count; + +typedef struct { + int x; + int y; + int width; + int height; + int state; + int length; + char *string; + struct Button *next; + struct Button *previous; + void(*callback)(); +} Button; + +typedef struct { + int type; + int x; + int y; + int width; + int height; + int state; + int size; + int position; + void *data; + struct Input *next; + struct Input *previous; + void(*callback)(); +} Input; + +int input_count; +Input *last_input; +Input *inputs[128]; + +Input *active_input; + +Button *last_button; +Button *buttons[128]; + +int active_count; +Input *active_list[15]; + +#endif