]> git.cworth.org Git - ttt/commitdiff
2005-12-04 Bryan Worth <bryan@theworths.org>
authorBryan Worth <bryan@theworths.org>
Mon, 5 Dec 2005 12:34:13 +0000 (12:34 +0000)
committerBryan Worth <bryan@theworths.org>
Mon, 5 Dec 2005 12:34:13 +0000 (12:34 +0000)
        * src/ttt-socket.c: (ttt_socket_create_client) fixed socket to be
        non-blocking. (ttt_socket_read) added return value for number
        of bytes read
        * Makefile.am: added necessary code for new curses-client program.
        * src/ttt-curses-client: new ncurses based client

ChangeLog
src/Makefile.am
src/ttt-curses-client.c [new file with mode: 0644]
src/ttt-socket.c
src/ttt-socket.h

index ebf09f7b5032d2692aa6a46cc72bee2c374f93b8..7cb6add5b59c2c4e7aec7658f9f77c018e1fcdf3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2005-12-04  Bryan Worth <bryan@theworths.org>
+        * src/ttt-socket.c: (ttt_socket_create_client) fixed socket to be
+        non-blocking. (ttt_socket_read) added return value for number
+        of bytes read
+        * Makefile.am: added necessary code for new curses-client program.
+        * src/ttt-curses-client: new ncurses based client
+
 2005-12-03  Richard D. Worth  <richard@theworths.org>
 
        * PROTOCOL: Add RETRACT, DECLINE. Add game identifier for all game
index 1d5de3f7669f20af0daa85e2f09edf2e73266f81..9ca2e53198bd8ae6755e37227b84e94fbeea35cc 100644 (file)
@@ -1,4 +1,4 @@
-bin_PROGRAMS = ttt-server ttt-client
+bin_PROGRAMS = ttt-server ttt-client ttt-curses-client
 noinst_PROGRAMS = test-board
 
 ttt_common_sources =           \
@@ -27,9 +27,14 @@ test_board_SOURCES =         \
        $(ttt_common_sources)   \
        test-board.c
 
+ttt_curses_client_SOURCES =     \
+       $(ttt_common_sources)   \
+       curses.h                \
+        ttt-curses-client.c
+
 AM_CFLAGS = $(WARN_CFLAGS) $(TTT_CFLAGS)
 AM_LFLAGS = --header-file=ttt-lex.h -Cr
 ttt_client_LDFLAGS = $(TTT_LIBS)
 ttt_server_LDFLAGS = $(TTT_LIBS) -lpthread
 test_board_LDFLAGS = $(TTT_LIBS)
-
+ttt_curses_client_LDFLAGS = $(TTT_LIBS) -lncurses
diff --git a/src/ttt-curses-client.c b/src/ttt-curses-client.c
new file mode 100644 (file)
index 0000000..b6e5981
--- /dev/null
@@ -0,0 +1,221 @@
+/* ttt-client.c - client handling code for tic-tac-toe game server
+ *
+ * Copyright © 2005 Carl Worth
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Author: Bryan Worth <bryan@theworths.org> 
+ */
+
+
+/* TODO: see /usr/share/doc/ncurses-devel-5.3/test/view.c for example code
+ *      to handle window resizing
+ *      
+ *      add code to read username from config file or prompt user if config
+ *      file does not exist. (username is currently hardcoded and needs
+ *      changed before compiling)
+ *
+ */     
+
+
+#include "ttt-socket.h"
+#include <curses.h>
+#include <signal.h>
+
+static void finish(int sig);
+
+void mvprintstr(int, int, char *, chtype);
+void mvwprintstr(WINDOW *, int, int, char *, chtype);
+void wprint(WINDOW *, char *);
+
+void mvprintstr(int y, int x, char *string, chtype attrs)
+{
+    int xx = 0;
+    int l;
+    chtype cline[1000];
+    l = strlen(string);
+    while (xx < l) {
+       cline[xx] = string[xx] | attrs;
+       xx++;
+    }
+    cline[l] = '\0';
+    mvaddchstr(y, x, cline);
+}
+
+
+void mvwprintstr(WINDOW * window, int y, int x, char *string, chtype attrs)
+{
+    int xx = 0;
+    int l;
+    chtype cline[1000];
+    l = strlen(string);
+    while (xx < l) {
+       cline[xx] = string[xx] | attrs;
+       xx++;
+    }
+    cline[l] = '\0';
+    mvwaddchstr(window, y, x, cline);
+}
+
+
+void wprint(WINDOW * window, char *string)
+{
+    int xx = 0;
+    int l;
+    chtype outc;
+    l = strlen(string);
+
+    while (xx < l) {
+       outc = string[xx];
+       if (string[xx] != '\r')
+           waddch(window, outc);
+       xx++;
+    }
+}
+
+
+char *message2;
+int _socket;
+size_t maxread;
+
+
+int main(int argc, char **argv)
+{
+    char *message;
+    char buffer[1000];
+    char *host;
+    char *port;
+    chtype c;
+    ssize_t numread;
+
+    static WINDOW *mainwnd;
+    static WINDOW *dispwin;
+    static WINDOW *inpwin;
+
+    mainwnd = initscr();
+    int dlines = LINES - 2, cols = COLS;
+
+    (void) nonl();
+    noecho();
+    cbreak();
+    nodelay(mainwnd, TRUE);
+    curs_set(0);
+    refresh();
+    dispwin = newwin(dlines - 1, cols - 2, 0, 0);
+    inpwin = newwin(1, cols - 2, dlines, 0);
+    keypad(mainwnd, TRUE);     // enable keyboard mapping 
+    keypad(inpwin, TRUE);      // enable keyboard mapping 
+    nodelay(inpwin, TRUE);
+    wrefresh(mainwnd);
+    wrefresh(dispwin);
+    wrefresh(inpwin);
+    (void) scrollok(mainwnd, TRUE);
+    (void) scrollok(dispwin, TRUE);
+    (void) scrollok(inpwin, TRUE);
+    (void) idlok(mainwnd, TRUE);
+    (void) idlok(dispwin, TRUE);
+    (void) idlok(inpwin, TRUE);
+    wsetscrreg(dispwin, 0, dlines - 2);
+    
+    (void) signal(SIGINT, finish);     /* arrange interrupts to terminate */
+    if (has_colors()) {
+       start_color();
+
+       init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
+       init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
+       init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
+       init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
+       init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
+       init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
+       init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
+       init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
+    }
+    wbkgd(dispwin, COLOR_PAIR(COLOR_WHITE));
+    wbkgd(inpwin, COLOR_PAIR(COLOR_CYAN));
+    wrefresh(dispwin);
+    wrefresh(inpwin);
+
+    xasprintf(&host, "localhost");
+    xasprintf(&port, "5334");
+    xasprintf(&message, "HELO bryan\r\n");
+
+    ttt_socket_create_client(host, port, &_socket);
+    ttt_socket_write(_socket, message, strlen(message));
+
+    maxread = 100;
+    numread = read(_socket, buffer, maxread);
+    if (numread > 0) {
+       buffer[numread] = '\0';
+       xasprintf(&message, "%s", buffer);
+       wprint(dispwin, message);
+       wrefresh(dispwin);
+    }
+    xasprintf(&message2, "MESSAGE \"");
+    while (1) {
+       curs_set(1);
+       c = wgetch(inpwin);
+       if (c != ERR) {
+           xasprintf(&message2, "%s%c", message2, (int) c);
+           waddch(inpwin, c);
+           wrefresh(inpwin);
+       }
+
+       numread = 0;
+       maxread = 100;
+       numread = read(_socket, buffer, maxread);
+       if (numread > 0) {
+           curs_set(0);
+           buffer[numread] = '\0';
+           wprint(dispwin, buffer);
+           wrefresh(dispwin);
+           wrefresh(inpwin);
+       }
+       if ((int) c == 13) {
+           curs_set(0);
+           xasprintf(&message2, "%s\"\r\n", message2);
+           ttt_socket_write(_socket, message2, strlen(message2));
+           xasprintf(&message2, "MESSAGE \"");
+           maxread = 100;
+           numread = read(_socket, buffer, maxread);
+           if (numread > 0) {
+               buffer[numread] = '\0';
+               wprint(dispwin, buffer);
+           }
+           wrefresh(dispwin);
+           werase(inpwin);
+           wrefresh(inpwin);
+       }
+    }
+
+
+
+}
+
+
+static void finish(int sig)
+{
+    char buffer[1024];
+    ssize_t numread;
+    endwin();
+
+    /* do your non-curses wrapup here */
+    xasprintf(&message2, "QUIT\r\n");
+    ttt_socket_write(_socket, message2, strlen(message2));
+    maxread = 100;
+    numread = read(_socket, buffer, maxread);
+    buffer[numread] = '\0';
+    xasprintf(&message2, "%s", buffer);
+    exit(0);
+}
index 20b96e263056103b87752fe6da432b5c8a89b63b..5bf36712a6f3b92ffb102e7eeb0328403a8af912 100644 (file)
@@ -166,6 +166,7 @@ ttt_socket_create_client (const char        *host,
     ttt_status_t status;
     struct sockaddr_in addr;
     int socket;
+    int flags;
 
     *socket_ret = -1;
 
@@ -178,13 +179,15 @@ ttt_socket_create_client (const char      *host,
        shutdown (socket, SHUT_RDWR);
        return status;
     }
-
+   flags = fcntl(socket, F_GETFL);
+   flags |= O_NONBLOCK;
+   fcntl(socket, F_SETFL, flags);
     *socket_ret = socket;
     return TTT_STATUS_SUCCESS;
 }
 
 /* Exported: see ttt-socket.h for documentation. */
-void
+ssize_t
 ttt_socket_read (int    socket,
                 void   *buf,
                 size_t  count)
@@ -192,11 +195,8 @@ ttt_socket_read (int        socket,
     int bytes_read;
     char *cbuf = buf;
 
-    while (count) {
        bytes_read = xread (socket, cbuf, count);
-       cbuf += bytes_read;
-       count -= bytes_read;
-    }
+       return(bytes_read);
 }
 
 /* Exported: see ttt-socket.h for documentation. */
index c3ca0bdd604263e65e49af91f1a106ad36b79b03..e3cd9bca8a515bf328121f03aeee5f213044e177 100644 (file)
@@ -78,7 +78,7 @@ ttt_socket_create_client (const char  *host,
  *
  * Errors: If any errors occur, this function does not return.
  */
-void
+ssize_t
 ttt_socket_read (int    socket,
                 void   *buf,
                 size_t  count);