Skip to content

Commit

Permalink
project update
Browse files Browse the repository at this point in the history
  • Loading branch information
iikrllx committed Nov 8, 2023
1 parent 3d44a1f commit 9b07f15
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
a.out
typp
typp-server
cpm.*
wpm.*
*.o
.gdb_history
.deps/
Expand Down
4 changes: 2 additions & 2 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Run the 'locale' command


--- Spell
autoreconf -iv && ./configure 'CFLAGS=-O0 -g3' && sudo make install
$ autoreconf -iv && ./configure 'CFLAGS=-O0 -g3' && sudo make install


--- Launch and show your best result
typp
$ typp


--- File Structure
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@ From the available random texts: <strong>English</strong> / <strong>Russian</str

## Table of Contents
- [Cloning and check needed packages](#cloning-and-check-needed-packages)
- [TUI Launch](#tui-launch)
- [TUI Overview](#tui-overview)
- [Result more details](#result-more-details)
- [Share your result](#share-your-result)
- [Advice](#advice)

## Cloning and check needed packages
```
git clone https://github.com/iikrllx/typp
cd typp && cat INSTALL
$ git clone https://github.com/iikrllx/typp
$ cd typp && cat INSTALL
```
Install packages from list and then:
```
autoreconf -iv && ./configure && sudo make install
$ autoreconf -iv && ./configure && sudo make install
```
Disable terminal shortcuts<br/>
Terminal -> Edit -> Preferences -> Advanced<br/>
Check the boxes:
* Disable menu shortcut key (F10 by default)
* Disable help window shortcut key (F1 by default)

## TUI Launch
```
$ typp
```

## TUI Overview
![gif](./images/overview.gif)

Expand Down Expand Up @@ -68,8 +74,9 @@ characters_count / time_in_minute
The countdown time starts from the first entered character.

## Share your result
Not stable working.
Read 'src/server/README' -> requires improvement / work.<br/>
Enter your nickname into the form after entering text. Your result will be sent to the TCP server.<br/>
(source code - https://github.com/iikrllx/typp-server)<br/>
Through the main menu it will be possible to go to the pivot table of common results for viewing.
The top five results (users) are immediately visible.<br/>
![screenshot](./images/wpm_table.png)
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = gcc
typp_CFLAGS = -Wall ${ncursesw_CFLAGS} ${formw_CFLAGS} ${menuw_CFLAGS}
typp_LDADD = -lm ${ncursesw_LIBS} ${formw_LIBS} ${menuw_LIBS}
typp_SOURCES=main.c common.c remote_results.c server.c
typp_SOURCES=main.c common.c remote_results.c connection.c
bin_PROGRAMS=typp
2 changes: 1 addition & 1 deletion src/server.c → src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/*
* server.c - server connection.
* connection.c - server connection.
*
*/

Expand Down
3 changes: 3 additions & 0 deletions src/server/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TCP server. Accepts requests from TCP client (typp) and processes them.
Luanch server (socket listening). NOT TESTED (requires improvement).
$ bash launch.sh
20 changes: 20 additions & 0 deletions src/server/launch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# Launch server, environment
#

gcc -g3 -O0 -Wall server.c -o typp-server -pthread

while true; do
if test $(pidof typp-server); then
kill $(pidof typp-server)
else
./typp-server &
break
fi
done

usr_path=/usr/local/bin
if test ! $(diff sort-values.sh $usr_path/sort-values.sh &>/dev/null); then
sudo cp sort-values.sh $usr_path
fi
230 changes: 230 additions & 0 deletions src/server/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
typp-server - accepts requests from the client (typp) and processes them.
Copyright (C) 2021 Kirill Rekhov <mgrainmi@gmail.com>
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 3 of the License, 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, see <https://www.gnu.org/licenses/>.
*/

/*
* server.c - all code here, run 'launch.sh' script for launch server.
*
*/

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define CON_LIMIT 100

void xfprintf(char *str, int line)
{
fprintf(stderr, "typp-server: %s:%d: %s\n", __FILE__, line, str);
exit(EXIT_FAILURE);
}

void write_data_to_file(const char *file, const char *value)
{
int fd;

if ((fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0644)) == -1)
xfprintf(strerror(errno), __LINE__);

if (write(fd, value, strlen(value)) == -1)
xfprintf(strerror(errno), __LINE__);
}

int check_client_msg_format(char client_msg[])
{
int j = 0;

char *token = strtok(client_msg, " ");

while (token != NULL) {
j++;

if (j == 1) {
if (!isdigit(token[0]) || strlen(token) != 1)
return -1;
}

if (j == 2) {
for (int i = 0; token[i] != 0; i++)
if (!isalpha(token[i]))
return -1;
}

if (j == 3 || j == 4) {
for (int i = 0; token[i] != 0; i++)
if (!isdigit(token[i]))
return -1;
}

if (j == 5) {
if (strchr(token, ':') == NULL)
return -1;
}

token = strtok(NULL, " ");
}

return 0;
}

void *socket_thread(void *arg)
{
pid_t child_sort;

int nb, fd;
int client_sock = *((int *)arg);
int file_size = 0, modif_size = 0;

char *pmsg, *tmppt, *all_results;

/* files names for writing / reading results */
char final_res[] = "-pm.final";
char all_res[] = "-pm.all";

/* message from client */
char client_msg[64] = { 0 };

if (recv(client_sock, client_msg, sizeof(client_msg), 0) == -1)
xfprintf(strerror(errno), __LINE__);

tmppt = client_msg;

if (strlen(tmppt) == 3 && strcmp(++tmppt, "pm") == 0) {

final_res[0] = client_msg[0];
fd = open(final_res, O_RDONLY);

if (fd == -1)
xfprintf(strerror(errno), __LINE__);

/* get size of the file */
file_size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);

modif_size = file_size * sizeof(char) + sizeof(char);
all_results = malloc(modif_size);

if (all_results == NULL)
xfprintf(strerror(errno), __LINE__);

/* fill buffer with results */
if ((nb = read(fd, all_results, modif_size)) <= 0)
xfprintf(strerror(errno), __LINE__);

/* send all values to client */
if (send(client_sock, all_results, nb, 0) != nb)
xfprintf(strerror(errno), __LINE__);

free(all_results);
close(client_sock);
close(fd);

} else {
/* check language index, ru - 0 / en - 1
prepare file name for writing */
if (client_msg[0] != '1')
all_res[0] = 'c';
else
all_res[0] = 'w';

pmsg = strdup(client_msg);

/* check message client format: "<n> Username <nnn> <nn> <nn>:<nn>" */
if (check_client_msg_format(client_msg) != -1) {
/* message without first number (language index) */
write_data_to_file(all_res, pmsg + 2);

switch (child_sort = fork()) {
case -1:
xfprintf(strerror(errno), __LINE__);
case 0:
execlp("sort-values.sh", "sort-values.sh",
all_res, NULL);
xfprintf(strerror(errno), __LINE__);
default:
wait(NULL);
}
}

free(pmsg);
}

pthread_exit(NULL);
}

int main(void)
{
struct sockaddr_in servaddr;
struct sockaddr_storage servstorage;
int serv_sock, client_sock, i = 0, opt = 1;
pthread_t tid[CON_LIMIT];
socklen_t addr_size;

if ((serv_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
xfprintf(strerror(errno), __LINE__);
memset(&servaddr, 0, sizeof(servaddr));

/* This is completely optional, but it helps in reuse of address and port.
Prevents error such as: "address already in use". */
if (setsockopt
(serv_sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt)) == -1)
xfprintf(strerror(errno), __LINE__);

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(8012);

if ((bind(serv_sock, (struct sockaddr *)&servaddr, sizeof(servaddr))) !=
0)
xfprintf(strerror(errno), __LINE__);

if ((listen(serv_sock, CON_LIMIT)) != 0)
xfprintf(strerror(errno), __LINE__);

while (true) {
/* Accept call creates a new socket for the incoming connection */
addr_size = sizeof(servstorage);
client_sock =
accept(serv_sock, (struct sockaddr *)&servstorage,
&addr_size);

if (pthread_create
(&tid[i++], NULL, socket_thread, (void *)&client_sock) != 0)
xfprintf(strerror(errno), __LINE__);

if (i >= CON_LIMIT) {
i = 0;
while (i < CON_LIMIT)
pthread_join(tid[i++], NULL);
i = 0;
}
}

return 0;
}
8 changes: 8 additions & 0 deletions src/server/sort-values.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
#
# Sort and added ordinal numbers
#

if test ! -z $1; then
sort -k2 -n -r $HOME/typp-server/$1 | awk '{print NR " " $s}' > ${1/.*all}.final
fi

0 comments on commit 9b07f15

Please sign in to comment.