Skip to content

Commit

Permalink
Presented Version added
Browse files Browse the repository at this point in the history
  • Loading branch information
0x161e-swei committed Aug 1, 2014
1 parent 605027d commit 3b3858f
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 48 deletions.
169 changes: 169 additions & 0 deletions Communication_Bridge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@


#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#include "Communication_Bridge.h"
#include "busOperation.h"



int Send_2_Client(int fds, unsigned char boardmap[BOARDSIZE][BOARDSIZE], const char game_msg[], bool dummyline, const char text_msg[])
{
int Sent_Bytenum;
unsigned int i, j;
//Server_Message *msg;
char msg[BOARDSIZE * BOARDSIZE + 10 + 20 + 1];

memcpy(msg, game_msg, strlen(game_msg));
for (i = strlen(game_msg); i < 10; i++)
msg[i] = ' ';
memcpy(&msg[10], boardmap, BOARDSIZE * BOARDSIZE);
i = 10 + BOARDSIZE * BOARDSIZE;
memcpy(&msg[i], text_msg, strlen(text_msg));
j = strlen(text_msg);
i += j;
for ( ; j < 20; j++, i++)
msg[i] = ' ';
msg[i] = '\0';
/*for ( ; i < BOARDSIZE; i++)
for (j = 0; j < BOARDSIZE; j++)
msg[i * BOARDSIZE + j] = boardmap[i][j];
for ( ; i < 10; i++)
msg[i] = ' ';
for ( ; i < strlen(text_msg); i++)
msg[i] = text_msg[i];
msg[i] = '\0';*/
/*
msg = (Server_Message *)malloc(sizeof(Server_Message));
if ( boardmap )
memcpy(msg->Map, boardmap, BOARDSIZE * BOARDSIZE);
if ( game_msg ){
//msg->game_msg = (unsigned char *)malloc(strlen(game_msg) * sizeof(unsigned char));
strcpy(msg->game_msg, game_msg, strlen(game_msg));
}
printf("Send_2_Client game_message: %s\n", msg->game_msg);
msg->dummyline = dummyline;
if ( text_msg ){
//msg->game_msg = (unsigned char *)malloc(strlen(text_msg) * sizeof(unsigned char));
strcpy(msg->text_msg, text_msg, strlen(text_msg));
}
*/
if ( (Sent_Bytenum = send(fds, msg, sizeof(msg), 0)) < 0 ){
perror("Server: SendError"); exit(1);
}

//if ( msg->game_msg ) free(msg->game_msg);
//if ( msg->text_msg ) free(msg->text_msg);
//free(msg);
return Sent_Bytenum;

}

int Send_2_Server(int fds, char Operation[])
{
int Sent_Bytenum;
//unsigned int i;
//char msg[11];
/*Client_Message *msg;
printf("Send_2_Server: %s\n", Operation);
msg = (Client_Message *)malloc(sizeof(Client_Message));
if ( Operation ){
//msg->operation = (unsigned char *)malloc(strlen(Operation) * sizeof(unsigned char));
memcpy(msg->operation, Operation, strlen(Operation));
}
*/
/*
for (i = 0; i < strlen(Operation); i++)
msg[i] = Operation[i];
for ( ; i < 10; i++)
msg[i] = ' ';
msg[i] = '\0';
printf("Send_2_Server: %s\n", msg);
*/
Sent_Bytenum = send(fds, Operation, sizeof(Operation), 0);
//if ( msg->operation ) free(msg->operation);
//free(msg);
return Sent_Bytenum;

}

int Reqs_4_Client(int fds, char buf[])
{
int Recv_Bytenum;//, i;
//unsigned char buf[255];//*buf;
//buf = (unsigned char *)malloc(255);
if ( (Recv_Bytenum = recv(fds, buf, sizeof(buf), 0)) > 0 ){
buf[Recv_Bytenum] = '\0';
}
else{
perror("Server: RecvError"); exit(1);
}
/*
for (i = 0; i < Recv_Bytenum; i++)
if( buf[i] == ' '){
buf[i] = '\0';
break;
}
*/
printf("Reqs_4_Client: %s.for %dBytes\n", buf, Recv_Bytenum);
return Recv_Bytenum;
}


int Reqs_4_Server(int fds, char game_msg[])
{
int Recv_Bytenum;
unsigned int i, j;
unsigned char Map[BOARDSIZE][BOARDSIZE];
unsigned int Map_ram[38];
char Test_msg[20];
memset(Map, 0, BOARDSIZE * BOARDSIZE);
memset(Map_ram, 0, 38);
//Server_Message *sev_msg;
//sev_msg = (Server_Message *)malloc(sizeof(Server_Message));
unsigned char buf[450];//*buf;
//buf = (unsigned char *)malloc(255);
if ( (Recv_Bytenum = recv(fds, buf, sizeof(buf), 0)) > 0 ){
printf("Reqs_4_Server_Recv_Bytenum: %d\n", Recv_Bytenum);
//memcpy(sev_msg, buf, sizeof(Server_Message));
//strcpy(game_msg, sev_msg->game_msg);
for (i = 0; buf[i] != ' '; i++)
game_msg[i] = buf[i];
game_msg[i] = '\0';
Recv_Bytenum = i;
printf("Reqs_4_Server: %s.for %dBytes.\n", game_msg, Recv_Bytenum);
memcpy(Map, &buf[10], BOARDSIZE * BOARDSIZE);
printf("After Sending from server to client.\n");
busMap(Map, Map_ram);
busSend(Map_ram);
j = 0;
for (i = 371; j < 16; i++)
Test_msg[j++] = buf[i];
Test_msg[j] = '\0';
if ( Test_msg[0] != ' ' )
busText(Test_msg);

}

return Recv_Bytenum;

}
24 changes: 24 additions & 0 deletions Communication_Bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef COMMUNICATION_BRIDGE_H_
#define COMMUNICATION_BRIDGE_H_

#define BOARDSIZE 19

struct Server_Message{
char game_msg[10];
unsigned char Map[BOARDSIZE][BOARDSIZE];
bool dummyline;
char text_msg[20];
};

struct Client_Message{
char operation[10];

};


extern int Send_2_Client(int fds, unsigned char boardmap[BOARDSIZE][BOARDSIZE], const char game_msg[], bool dummyline, const char text_msg[]);
extern int Send_2_Server(int fds, char Operation[]);
extern int Reqs_4_Client(int fds, char buf[]);
extern int Reqs_4_Server(int fds, char game_msg[]);

#endif /* COMMUNICATION_BRIDGE_H_ */
1 change: 1 addition & 0 deletions GTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using namespace std;
const char GTPClient::PositionXTable[] = "ABCDEFGHJKLMNOPQRST";
const char * GTPClient::ColorString[] = {NULL, "black", "white"};


void GTPClient::GTPFork()
{
int pipefd0[2],pipefd1[2];
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DEBUG := -g
CC = g++
CXX = g++
EXECUTABLE = server client client_demo
EXECUTABLE = server client client_demo
LIB =
CFLAG = -c -Wall $(DEBUG)
CPPFLAG = $(CFLAG)
Expand All @@ -11,13 +11,13 @@ LDFLAG = $(LIB)
all: $(EXECUTABLE)
chmod +x gnugo.gtp

server: mmap.o GTPClient.o busOperation.o
server: mmap.o GTPClient.o busOperation.o Communication_Bridge.o
$(CXX) -o $@ $^

client: client.o
client: client.o Communication_Bridge.o busOperation.o
$(CXX) -o $@ $^

client_demo: client_demo.o
client_demo: client_demo.o Communication_Bridge.o busOperation.o
$(CXX) -o $@ $^

.c.o:
Expand Down
4 changes: 2 additions & 2 deletions busOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ int busSend(unsigned int busRam[38]) {
mapped = (volatile unsigned int *)map_addr;

for (int i = 0; i < 19; i++) {
mapped[i + 19] = busRam[i + 19];
mapped[i ] = busRam[i ];
mapped[i + 19] = busRam[37 - i];
mapped[i ] = busRam[18 - i];
}

munmap(map_addr, size_board);
Expand Down
49 changes: 41 additions & 8 deletions client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

#include <arpa/inet.h>

#include "Communication_Bridge.h"
#include "busOperation.h"

#define PORT "3490" // the port client will be connecting to

#define MAXDATASIZE 100 // max number of bytes we can get at once
Expand Down Expand Up @@ -50,7 +53,7 @@ void Usage_info(char *cmd, int d)

int main(int argc, char *argv[])
{
int sockfd;
int sockfd, j;
char server_buf[MAXDATASIZE], *cmd;
struct addrinfo hints, *servinfo, *p;
bool Game_over = false;
Expand Down Expand Up @@ -106,14 +109,17 @@ int main(int argc, char *argv[])
* instead of operation message
*
*/
send(sockfd, argv[2], strlen(argv[2]) + 1, 0); // set username
for (j = 0; j < 32; j++)
busText(" ");
Send_2_Server(sockfd, argv[2]); // set username
cmd = (char *)malloc(15 * sizeof(char));
Game_over = false;
while( !Game_over ){
/* Wait for server to wake me up */
printf("Waiting for wake\n");
if ( (Recv_Bytenum = recv(sockfd, server_buf, sizeof(server_buf), 0)) > 0 ){
server_buf[Recv_Bytenum] = '\0';
//if ( (Recv_Bytenum = recv(sockfd, server_buf, sizeof(server_buf), 0)) > 0 ){
if ( (Recv_Bytenum = Reqs_4_Server(sockfd, server_buf))){
//server_buf[Recv_Bytenum] = '\0';
printf("%s\n", server_buf);
if ( strcmp(server_buf, "GAMEOVER") == 0 ){
Game_over = true;
Expand All @@ -132,7 +138,8 @@ int main(int argc, char *argv[])
toUpper_s(cmd, strlen(cmd) + 1);
while ( strcmp(cmd, "QUIT") != 0 && !Game_over ){
if ( strcmp(cmd, "GEN") == 0 ){ // Radomly setpeice
if ( (Send_Bytenum = send(sockfd, cmd, strlen(cmd), 0)) < 0 ){
//if ( (Send_Bytenum = send(sockfd, cmd, strlen(cmd), 0)) < 0 ){
if ( (Send_Bytenum = Send_2_Server(sockfd, cmd)) < 0 ){
perror("Client: Send Error"); exit(1);
}
else{
Expand All @@ -141,7 +148,8 @@ int main(int argc, char *argv[])
}
}
else if ( strcmp(cmd, "PASS") == 0 ){
if ( (Send_Bytenum = send(sockfd, cmd, strlen(cmd), 0)) < 0 ){
//if ( (Send_Bytenum = send(sockfd, cmd, strlen(cmd), 0)) < 0 ){
if ( (Send_Bytenum = Send_2_Server(sockfd, cmd)) < 0 ){
perror("Client: Send Error"); exit(1);
}
else break;
Expand All @@ -159,6 +167,7 @@ int main(int argc, char *argv[])
cmd[--i] = '0' + num % 10;
num /= 10;
}
/*
if ( (Send_Bytenum = send(sockfd, cmd, strlen(cmd), 0)) < 0 ){
perror("Client: Send Error"); exit(1);
}
Expand All @@ -173,16 +182,40 @@ int main(int argc, char *argv[])
}
else perror("Client: Wait after move error!"); exit(1);
}
*/
if ( Send_2_Server(sockfd, cmd) < 0 ){
perror("Client: Send Error"); exit(1);
}
else{
usleep(50000);
if ( (Recv_Bytenum = Reqs_4_Server(sockfd, server_buf)) > 0){
//server_buf[Recv_Bytenum] = '\0';
if ( strcmp(server_buf, "SUCCEED") == 0 ){
printf("Succeed received!\n");
break;
}
else Usage_info(cmd, 2);
printf("After_move buffer: %s", server_buf);
}
else{
perror("Client: Wait after move error!"); exit(1);
}
}

}
else Usage_info(cmd, 1);
}
else Usage_info(cmd, 0);
}
else Usage_info(cmd, 0);
}

if ( strcmp(cmd, "QUIT") == 0 ){
if ( (Send_Bytenum = send(sockfd, cmd, strlen(cmd), 0)) < 0 ){
perror("Client: Send Error"); exit(1);
//if ( (Send_Bytenum = send(sockfd, cmd, strlen(cmd), 0)) < 0 ){
// perror("Client: Send Error"); exit(1);
//}
if ( Send_2_Server(sockfd, cmd) < 0){
perror("Client: Send Error"); exit(1);
}
}
}
Expand Down
Loading

0 comments on commit 3b3858f

Please sign in to comment.