31 #define SHUT_RDWR SD_BOTH
34 #include <sys/socket.h>
36 #include <netinet/tcp.h>
44#define GGS_BOARD_SIZE 256
47#define MOVELIST_SIZE 256
169static char admin_list[][16] = {
"delorme",
"dan",
"mic",
"romano",
"HCyrano",
"romelica",
""};
198 text->
line = (
const char**) realloc(text->
line, text->
n_lines * sizeof (
const char*));
216 for (i = 0; i < text->
n_lines; ++i) {
217 fprintf(f,
"GGS> %s\n", text->
line[i]);
233 for (i = 0; i < text->
n_lines; ++i) {
234 free((
void*)text->
line[i]);
236 free((
void*)text->
line);
255 for (n = 0; buffer[n] && buffer[n] !=
'\n' && buffer[n] !=
'\r'; ++n) ;
257 *line = (
char*) malloc(n + 1);
258 if (*line == NULL)
fatal_error(
"Allocation error\n");
259 if (n > 0) memcpy(*line, buffer, n);
262 while (buffer[n] ==
'\n' || buffer[n] ==
'\r') ++n;
266 return (
char*) (buffer + n);
283 while (buffer[0] ==
'|' || text->
n_lines == 0) {
289 return (
char*) buffer;
426 if (name == NULL || *name ==
'\0')
return false;
432 if (*rating ==
'(') ++rating;
456 for (c = word; *c; c++) {
457 if (*c ==
'k') type->
is_komi = 1;
462 if (*c ==
'a') type->
is_anti = 1;
507 return (strcmp(me, player[0].name) == 0 || strcmp(me, player[1].name) == 0);
521 const char *line = text->
line[0];
525 if (strcmp(
"/os:", word) == 0) {
527 if (strcmp(
"+", word) == 0) {
550 const char *line = text->
line[0];
554 if (strcmp(
"/os:", word) != 0)
return false;
557 if (strcmp(
"+", word) != 0)
return false;
560 if (strcmp(
"match", word) != 0)
return false;
578 match->match_type->is_rated = (strcmp(word,
"R") == 0) ? 1 : 0;
594 const char *line = text->
line[0];
598 if (strcmp(
"/os:", word) != 0)
return false;
601 if (strcmp(
"-", word) != 0)
return false;
604 if (strcmp(
"match", word) != 0)
return false;
630 const char *line = text->
line[0];
634 if (text->
n_lines < 17)
return false;
636 if (*line ==
'\0')
return false;
639 if (strcmp(
"/os:", word) != 0)
return false;
641 if (*line ==
'\0')
return false;
643 board->
is_join = (strcmp(
"join", word) == 0);
644 board->
is_update = (strcmp(
"update", word) == 0);
650 if (*line ==
'\0')
return false;
654 if (*line ==
'\0')
return false;
660 if (word[1] ==
'?') {
676 for (i = 0; i < 8; i++) {
678 for (j = 0; j < 8; j++)
705 board->
color[0] = word[0];
714 board->
color[1] = word[0];
719 for (i = 0; i < 8; i++) {
721 for (j = 0; j < 8; j++)
722 board->
board[i * 8 + j] = text->
line[ii][4 + j * 2];
747 if (strcmp(text->
line[0],
": + /os 1") == 0)
return true;
762 if (strcmp(text->
line[0],
": - /os 1") == 0)
return true;
779 const char *s = strstr(text->
line[0],
"Sorry, i will accept new games in");
781 if (s && strlen(s) > 35) {
803 const char *line = text->
line[0];
829 const char *line = text->
line[0];
830 return strcmp(line,
"READY") == 0;
843 const char *line = text->
line[0];
844 return strcmp(line,
"ALERT") == 0;
857 const char *line = text->
line[0];
858 return strcmp(line,
": Enter login (yours, or one you'd like to use).") == 0;
871 const char *line = text->
line[0];
872 return strcmp(line,
": Enter your password.") == 0;
886 enum {BUCKET_SIZE = 16384};
887 char buffer[BUCKET_SIZE + 1] =
"";
890 while (event->loop) {
891 r = recv(event->socket, buffer, BUCKET_SIZE, 0);
894 l = strlen(event->buffer);
895 event->buffer = (
char*) realloc(event->buffer, r + l + 1);
896 memcpy(event->buffer + l, buffer, r);
897 event->buffer[l + r] =
'\0';
917 struct addrinfo hints;
918 struct addrinfo *result, *rp = NULL;
922 event->buffer = (
char*) calloc(1, 1);
928 int value = WSAStartup(MAKEWORD(2,2), &wsaData);
929 if (value != NO_ERROR) {
936 memset(&hints, 0,
sizeof (
struct addrinfo));
937 hints.ai_family = AF_INET;
938 hints.ai_socktype = SOCK_STREAM;
939 hints.ai_flags = AI_PASSIVE;
940 hints.ai_protocol = getprotobyname(
"tcp")->p_proto;
941 hints.ai_canonname = NULL;
942 hints.ai_addr = NULL;
943 hints.ai_next = NULL;
946 for (rp = result; rp != NULL; rp = rp->ai_next) {
947 event->socket = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
948 if (event->
socket != -1) {
949 if (connect(event->
socket, rp->ai_addr, rp->ai_addrlen) != -1)
break;
951 closesocket(event->
socket);
961 freeaddrinfo(result);
977 shutdown(event->
socket, SHUT_RDWR);
979 closesocket(event->
socket);
1008 memmove(event->
buffer, s, strlen(s) + 1);
1009 ok = (*
event->buffer !=
'|');
1049 if (fmt == NULL) return ;
1052 buffer = (
char*) malloc(size + 1);
1053 if (buffer == NULL)
return;
1055 message_length = vsnprintf(buffer, size, fmt, ap);
1058 if (message_length > 0 && message_length < size)
break;
1060 if (message_length > 0) size = message_length + 1;
1066 printf(
"GGS< %s", buffer);
1067 send(client->
event.
socket, buffer, message_length, 0);
1121 printf(
"Connecting to GGS...\n");
1125 client->
loop->
i = 0;
1149 play = ui->
play + turn;
1169 char move[4], line[32];
1170 const char *(search_state_array[6]) = {
"running",
"interrupted",
"stop pondering",
"out of time",
"stopped on user demand",
"completed"};
1171 char search_state[32];
1176 printf(
"<use a single %d tasks search while a single game is played>\n",
options.
n_task);
1183 play = ui->
play + turn;
1203 if (remaining_time > 60000) remaining_time -= 10000;
1204 else if (remaining_time > 10000) remaining_time -= 2000;
1205 if (remaining_time < 1000) remaining_time = 1000;
1208 printf(
"<ggs: go thinking>\n");
1218 printf(
"[%s plays %s in game %s ; score = %d from book]\n", ui->
ggs->
me, move, ui->
ggs->
board->
id, result->
score);
1220 "\\%s plays %s in game %s"
1221 "\\score == %d from book\n",
1228 char s_nodes[16], s_speed[16];
1234 info(
"<%s plays %s in game %s ; score = %d at %d@%d%% ; %lld nodes in %.1fs (%.0f nodes/s.)>\n",
1237 result->
n_nodes, 0.001 * real_time, (result->
n_nodes / (0.001 * real_time + 0.001))
1242 sprintf(search_state,
"%s", search_state_array[play->
search->
stop]);
1245 "\\%s plays %s in game %s using %d thread%s"
1246 "\\score %s %+02d at %d@%d%% ; PV: %s ;"
1247 "\\nodes: %s ; time: search = %.1fs, move = %.1fs; speed: %s."
1251 bound, result->
score,
1275 printf(
"[received GGS_BOARD_JOIN]\n");
1282 play = ui->
play + 1;
1285 warn(
"Edax is not concerned by this game\n");
1309 if (play->
player == edax_turn) {
1310 printf(
"<My turn>\n");
1315 printf(
"[Waiting opponent move]\n");
1328 char buffer[256], s_move[4];
1330 int edax_turn, turn;
1333 printf(
"[received GGS_BOARD_UPDATE]\n");
1340 play = ui->
play + 1;
1358 info(
"<Resynchronize boards: diverging games>\n");
1362 if (turn != play->
player) {
1363 printf(
"[WARNING: updating player's turn]\n");
1371 if (ui->
is_same_play) printf(
"<Playing same game...>\n");
1374 if (play->
player == edax_turn) {
1375 printf(
"<My turn>\n");
1378 printf(
"<Opponent turn>\n");
1394 if (ui->
ggs == NULL)
fatal_error(
"ui_init_ggs: cannot allocate the GGS client\n");
1419 char *cmd = NULL, *param = NULL;
1432 if (strcmp(cmd,
"stop") == 0) {
1437 }
else if (strcmp(cmd,
"loop") == 0) {
1441 if (errno) client->
loop->
i = 100;
1442 if (client->
loop->
i > 0) {
1449 }
else if (strcmp(cmd,
"quit") == 0 || strcmp(cmd,
"q") == 0) {
1452 free(cmd); free(param);
1489 printf(
"[received GGS_OS_ON]\n");
1499 printf(
"[received GGS_OS_OFF]\n");
1504 printf(
"[received GGS_MATCH_ON]\n");
1508 printf(
"[received GGS_WATCH_ON]\n");
1514 printf(
"[received GGS_MATCH_OFF]\n");
1518 printf(
"[store match #1]\n");
1520 printf(
"[store match #2]\n");
1523 printf(
"[store match]\n");
1534 if (client->
loop->
i > 0) {
1540 printf(
"[received GGS_WATCH_OFF]\n");
1549 printf(
"[received GGS_WATCH_BOARD]\n");
1554 printf(
"[received GGS_REQUEST]\n");
1558 printf(
"[received GGS_ADMIN_CMD]\n");
1564 printf(
"[received GGS_SAIO_DELAY]\n");
1566 if (cmd != NULL && param != NULL) {
1567 if (strcmp(cmd,
"loop") == 0) {
1570 client->
once->
cmd = (
char*) malloc(strlen(cmd) + strlen(param) + 3);
1571 sprintf(client->
once->
cmd,
"%s %s\n", cmd, param);
1573 printf(
"[received GGS_SAIO_DELAY, retry request in %.1f s]\n", 0.001 * (client->
once->
delay -
real_clock()));
1583 printf(
"[received ALERT]\n");
void board_print(const Board *board, const int player, FILE *f)
Print out the board.
Definition board.c:1230
bool board_equal(const Board *b1, const Board *b2)
Compare two board for equality.
Definition board.c:335
int board_set(Board *board, const char *string)
Set a board from a string description.
Definition board.c:154
void book_free(Book *book)
Free resources used by the opening book.
Definition book.c:1422
void book_save(Book *book, const char *file)
Save an opening book.
Definition book.c:1622
void book_load(Book *book, const char *file)
Load the opening book.
Definition book.c:1471
@ NOMOVE
Definition const.h:37
@ WHITE
Definition const.h:43
@ BLACK
Definition const.h:42
@ STOP_TIMEOUT
Definition const.h:74
@ IS_THINKING
Definition const.h:103
static void ggs_client_free(GGSClient *client)
ggs_client_free
Definition ggs.c:1024
static void text_init(Text *text)
text_init
Definition ggs.c:181
static bool ggs_match_on(GGSMatchOn *match, Text *text)
ggs_match_on
Definition ggs.c:548
void ui_free_ggs(UI *ui)
ui_free_ggs
Definition ggs.c:1599
static char * ggs_parse_line(const char *buffer, char **line)
ggs_parse_line
Definition ggs.c:249
static Log ggs_log[1]
Definition ggs.c:167
void ui_loop_ggs(UI *ui)
ui_loop_ggs
Definition ggs.c:1418
static bool ggs_player_set(GGSPlayer *player, const char *name, const char *rating)
ggs_player_set
Definition ggs.c:423
static bool ggs_alert(Text *text)
ggs_alert
Definition ggs.c:841
static void ggs_client_refresh(GGSClient *client)
ggs_client_refresh
Definition ggs.c:1080
static bool ggs_password(Text *text)
ggs_password
Definition ggs.c:869
static void text_print(Text *text, FILE *f)
text_print
Definition ggs.c:212
static const GGSMatchType GGS_MATCH_TYPE_INI
Definition ggs.c:172
static bool ggs_has_player(GGSPlayer *player, const char *me)
ggs_match_check_destination
Definition ggs.c:506
static void ui_login(UI *ui)
ui_login
Definition ggs.c:1110
static void ui_ggs_play(UI *ui, int turn)
ui_ggs_play
Definition ggs.c:1163
static bool ggs_login(Text *text)
ggs_login
Definition ggs.c:855
struct GGSMatchOn GGSMatchOn
static bool ggs_board(GGSBoard *board, Text *text)
ggs_board
Definition ggs.c:628
static bool ggs_parse_time(int *time, const char *word)
ggs_parse_move
Definition ggs.c:347
static bool ggs_os_on(Text *text)
ggs_os_on
Definition ggs.c:745
static bool ggs_parse_clock(GGSClock *ggsclock, const char *line)
ggs_parse_clock
Definition ggs.c:362
static void text_free(Text *text)
text_free
Definition ggs.c:228
static void ggs_player_free(GGSPlayer *player)
ggs_player_free
Definition ggs.c:475
static void ggs_event_init(GGSEvent *event)
ggs_event_init
Definition ggs.c:915
static void ggs_event_free(GGSEvent *event)
ggs_event_free
Definition ggs.c:974
#define GGS_BOARD_SIZE
Definition ggs.c:44
static char admin_list[][16]
Definition ggs.c:169
static bool ggs_saio_delay(Text *text, long long *delay)
ggs_saio_delay
Definition ggs.c:777
static bool ggs_match_off(GGSMatchOff *match, Text *text)
ggs_match_off
Definition ggs.c:592
static bool ggs_os_off(Text *text)
ggs_os_off
Definition ggs.c:760
#define WORD_SIZE
Definition ggs.c:41
static bool ggs_parse_move(int *move, const char *word)
ggs_parse_move
Definition ggs.c:333
static void ggs_client_send(GGSClient *client, const char *fmt,...)
ggs_client_send
Definition ggs.c:1043
static const GGSClock GGS_CLOCK_INI
Definition ggs.c:171
struct GGSRequest GGSRequest
struct GGSClient GGSClient
static bool ggs_request(GGSRequest *request, Text *text)
ggs_request
Definition ggs.c:519
struct GGSMatchOff GGSMatchOff
static bool ggs_parse_int(int *value, const char *word)
ggs_parse_int
Definition ggs.c:302
static bool ggs_ready(Text *text)
ggs_ready
Definition ggs.c:827
struct GGSPlayer GGSPlayer
static bool ggs_parse_double(double *value, const char *word)
ggs_parse_double
Definition ggs.c:318
void ui_init_ggs(UI *ui)
ui_init_ggs
Definition ggs.c:1390
#define MOVELIST_SIZE
Definition ggs.c:47
static void ui_ggs_ponder(UI *ui, int turn)
ui_ggs_ponder
Definition ggs.c:1143
static void * ggs_event_loop(void *v)
ggs_event_loop
Definition ggs.c:883
static int ggs_admin(GGSAdmin *admin, Text *text)
ggs_admin
Definition ggs.c:801
struct GGSMatchType GGSMatchType
static void ui_ggs_update(UI *ui)
ui_ggs_update
Definition ggs.c:1327
static char * ggs_parse_text(const char *buffer, Text *text)
ggs_parse_text
Definition ggs.c:278
static void text_add_line(Text *text, const char *line)
text_add_line
Definition ggs.c:195
static void ggs_MBR_free(void *v)
ggs_MBR_free
Definition ggs.c:488
static void ggs_match_type_set(GGSMatchType *type, const char *word)
ggs_match_type_set
Definition ggs.c:444
static bool ggs_event_peek(GGSEvent *event, Text *text)
ggs_event_peek
Definition ggs.c:999
static void ui_ggs_join(UI *ui)
ui_ggs_join
Definition ggs.c:1269
char * line_to_string(const Line *line, int n, const char *separator, char *string)
Line to string.
Definition move.c:635
char * move_to_string(const int x, const int player, char *s)
Print out a move.
Definition move.c:76
static bool match(const char *s1, const char *s2)
Definition opening.c:1274
Options options
Definition options.c:22
void play_store(Play *play)
store the game into the opening book
Definition play.c:1175
void play_set_board(Play *play, const char *board)
Set a new board.
Definition play.c:810
void play_ponder(Play *play)
Ponder.
Definition play.c:729
void play_stop_pondering(Play *play)
Stop pondering.
Definition play.c:749
bool play_move(Play *play, int x)
Play a move.
Definition play.c:889
void play_init(Play *play, Book *book)
Initialization.
Definition play.c:27
void play_adjust_time(Play *play, const int left, const int extra)
adjust time.
Definition play.c:1211
bool play_is_game_over(Play *play)
Check if game is over.
Definition play.c:190
void play_go(Play *play, const bool update)
Start thinking.
Definition play.c:214
void play_stop(Play *play)
Stop thinking.
Definition play.c:769
void play_free(Play *play)
Free resources.
Definition play.c:52
const Selectivity selectivity_table[]
Definition search.c:97
void search_set_task_number(Search *search, const int n)
Change the number of task.
Definition search.c:847
int search_count_tasks(const Search *search)
Count the number of tasks used in parallel search.
Definition search.c:1324
void search_share(const Search *src, Search *dest)
Share search information.
Definition search.c:1312
bool need_saving
Definition book.h:47
Search * search
Definition book.h:49
int lower
Definition search.h:36
int upper
Definition search.h:37
char * command
Definition ggs.c:132
char name[16]
Definition ggs.c:133
int move_list[MOVELIST_SIZE]
Definition ggs.c:125
int move_list_n
Definition ggs.c:126
int move
Definition ggs.c:118
double komi
Definition ggs.c:115
char board_init[GGS_BOARD_SIZE]
Definition ggs.c:123
int move_no
Definition ggs.c:119
int is_join
Definition ggs.c:116
GGSPlayer player[2]
Definition ggs.c:112
char turn
Definition ggs.c:122
char board[GGS_BOARD_SIZE]
Definition ggs.c:121
int is_update
Definition ggs.c:117
char * id
Definition ggs.c:111
char color[2]
Definition ggs.c:120
GGSClock clock[2]
Definition ggs.c:114
GGSMatchType match_type[1]
Definition ggs.c:113
char turn_init
Definition ggs.c:124
long long delay
Definition ggs.c:159
struct GGSClient::@17 loop[1]
GGSRequest request[1]
Definition ggs.c:148
GGSEvent event
Definition ggs.c:152
const char * me
Definition ggs.c:153
char * cmd
Definition ggs.c:157
GGSAdmin admin[1]
Definition ggs.c:151
GGSBoard board[1]
Definition ggs.c:147
struct GGSClient::@18 once[1]
bool is_playing
Definition ggs.c:154
GGSMatchOn match_on[1]
Definition ggs.c:149
GGSMatchOff match_off[1]
Definition ggs.c:150
int i
Definition ggs.c:158
long long last_refresh
Definition ggs.c:155
int inc_flag
Definition ggs.c:58
int inc_time
Definition ggs.c:66
int ext_flag
Definition ggs.c:59
int ini_flag
Definition ggs.c:57
int ini_time
Definition ggs.c:65
int ext_move
Definition ggs.c:63
int ext_time
Definition ggs.c:67
int inc_move
Definition ggs.c:62
int ini_move
Definition ggs.c:61
bool loop
Definition ggs.c:139
int socket
Definition ggs.c:138
char * buffer
Definition ggs.c:140
Thread thread
Definition ggs.c:141
Lock lock
Definition ggs.c:142
char * id
Definition ggs.c:90
GGSPlayer player[2]
Definition ggs.c:91
GGSPlayer player[2]
Definition ggs.c:97
char * id
Definition ggs.c:96
GGSMatchType match_type[1]
Definition ggs.c:98
int is_rand
Definition ggs.c:82
int is_synchro
Definition ggs.c:80
int is_rated
Definition ggs.c:79
int is_komi
Definition ggs.c:81
int is_anti
Definition ggs.c:83
int is_saved
Definition ggs.c:78
int size
Definition ggs.c:85
int discs
Definition ggs.c:84
double rating
Definition ggs.c:73
char * name
Definition ggs.c:72
char * id
Definition ggs.c:103
GGSPlayer player[2]
Definition ggs.c:104
GGSClock clock[2]
Definition ggs.c:106
GGSMatchType match_type[1]
Definition ggs.c:105
LogFile.
Definition util.h:423
int n_task
Definition options.h:29
char * ggs_host
Definition options.h:63
char * ggs_password
Definition options.h:65
char * ggs_login
Definition options.h:64
bool ggs_open
Definition options.h:67
char * ggs_log_file
Definition options.h:79
char * ggs_port
Definition options.h:66
char * book_file
Definition options.h:59
int player
Definition play.h:32
Board board[1]
Definition play.h:26
Result result[1]
Definition play.h:29
Search search[1]
Definition play.h:28
volatile PlayState state
Definition play.h:37
bool book_move
Definition search.h:50
int move
Definition search.h:44
unsigned long long n_nodes
Definition search.h:49
int score
Definition search.h:45
long long time
Definition search.h:48
int selectivity
Definition search.h:43
Line pv[1]
Definition search.h:47
int depth
Definition search.h:42
Bound bound[BOARD_SIZE+2]
Definition search.h:46
int n_empties
Definition search.h:99
int id
Definition search.h:101
int depth
Definition search.h:117
volatile Stop stop
Definition search.h:122
int selectivity
Definition search.h:118
int percent
Definition search.h:28
const char ** line
Definition ggs.c:51
int n_lines
Definition ggs.c:52
struct GGSClient * ggs
Definition ui.h:34
Play play[2]
Definition ui.h:32
Book book[1]
Definition ui.h:33
bool is_same_play
Definition ui.h:35
int mode
Definition ui.h:37
bool ui_event_peek(UI *ui, char **cmd, char **param)
Wait input.
Definition ui.c:160
char * string_duplicate(const char *s)
Duplicate a string.
Definition util.c:299
char * parse_int(const char *string, int *result)
Parse an integer.
Definition util.c:761
void thread_create2(Thread *thread, void *(*function)(void *), void *data)
Create a thread.
Definition util.c:922
char * parse_skip_word(const char *string)
Skip word.
Definition util.c:528
void thread_join(Thread thread)
Join a thread.
Definition util.c:940
long long(* time_clock)(void)
Time clock.
Definition util.c:122
int string_to_coordinate(const char *s)
Convert the two first chars of a string into a coordinate.
Definition util.c:384
char * parse_field(const char *string, char *word, unsigned int n, char separator)
Parse a field.
Definition util.c:582
char * parse_real(const char *string, double *result)
Parse a real number (as a double floating point).
Definition util.c:796
int string_to_int(const char *s, const int default_value)
Convert a string into an integer.
Definition util.c:457
char * parse_skip_spaces(const char *string)
Skip spaces.
Definition util.c:514
char * parse_word(const char *string, char *word, unsigned int n)
Parse a word.
Definition util.c:562
char * format_scientific(double v, const char *unit, char *f)
Format a value with a unit.
Definition util.c:222
void relax(int t)
sleep for t ms.
Definition util.c:203
long long string_to_time(const char *string)
Read time as "D:HH:MM:SS.C".
Definition util.c:320
Miscellaneous utilities header.
#define log_receive(l, title,...)
log a reception
Definition util.h:451
#define log_close(l)
Close an opened log file.
Definition util.h:435
#define error(...)
Display an error message as "ERROR : filename : funcname : line number : ...".
Definition util.h:361
long long real_clock(void)
#define fatal_error(...)
Display an error message as "FATAL_ERROR : file name : function name : line number : ....
Definition util.h:349
#define info(...)
Display a message.
Definition util.h:382
#define warn(...)
Display a warning message as "WARNING : ... ".
Definition util.h:373
#define log_open(l, file)
open a log file if allowed.
Definition util.h:429
#define log_send(l, title,...)
log a sending
Definition util.h:460