41 #include "PracticalSockets.h" 
   42 #include "../include/config.h" 
   48 #define MAX_PACKETS_FOR_TB 10 
   51 __file_size(
const char *fname)
 
   56     rc = lstat(fname, &s);
 
   58         fprintf(stderr, 
"err: Cannot find %s!\n", fname);
 
   65 __file_read(
const char *fname, 
char *buff, 
size_t len)
 
   70     if ((fname == NULL) || (buff == NULL) || (len == 0))
 
   73     fp = fopen(fname, 
"r");
 
   75         fprintf(stderr, 
"err: Cannot open file %s: %s\n",
 
   76             fname, strerror(errno));
 
   79     rc = fread(buff, len, 1, fp);
 
   81         fprintf(stderr, 
"err: Cannot read from %s: %s\n",
 
   82             fname, strerror(errno));
 
   92     unsigned int len = strlen(
str);
 
   94     for(
unsigned int i=0; i<=len; i++) {
 
   96       printf(
"DEBUG: null character position: %d\n",i+1);
 
  101     printf(
"DEBUG: null character not found\n");
 
  111 int main(
int argc, 
char * argv[]) {
 
  113     if ((argc < 2) || (argc > 4)) { 
 
  114         cerr << 
"Usage: " << argv[0] << 
" <Server Port> <optional simulation mode> <optional >" << endl;
 
  115         cerr << 
"<optional simulation mode> : 0 - fcsim, 2 - csim, 3 - csynth + cosim, 4 - memchecksim, 5 - kcachegrind" << endl;
 
  116         cerr << 
"<optional loop> : 0 - executed once, 1 - executed continuously" << endl;
 
  120     unsigned short servPort = atoi(argv[1]); 
 
  121     unsigned int num_batch = 0;
 
  122     string clean_cmd, synth_cmd;;
 
  123     unsigned int run_loop = 0;
 
  126         run_loop = atoi(argv[3]);
 
  128              cerr << 
"ERROR: Not valid loop option provided: " << argv[3] << 
" . Choose either 0 or 1. Aborting..." << endl;
 
  135         UDPSocket sock(servPort);
 
  137         TCPServerSocket servSock(servPort);     
 
  138         TCPSocket *servsock = servSock.accept();     
 
  141         unsigned int recvMsgSize; 
 
  142         string sourceAddress; 
 
  143         unsigned short sourcePort; 
 
  147         cout << 
"Handling client ";
 
  149             cout << servsock->getForeignAddress() << 
":";
 
  150         } 
catch (SocketException e) {
 
  151             cerr << 
"Unable to get foreign address" << endl;
 
  154             cout << servsock->getForeignPort();
 
  155         } 
catch (SocketException e) {
 
  156             cerr << 
"Unable to get foreign port" << endl;
 
  164             clock_t last_cycle_rx = 
clock();
 
  168             int input_string_total_len = 0;
 
  171             int bytes_in_last_pack;
 
  172             bool msg_received = 
false;
 
  173             cout << 
" ___________________________________________________________________ " << endl;
 
  174             cout << 
"/                                                                   \\" << endl;
 
  175             cout << 
"INFO: Proxy tb batch # " << ++num_batch << endl;       
 
  180             for (
int i = 0; msg_received != 
true; i++, total_pack++) {
 
  182                 recvMsgSize = sock.recvFrom(buffer, 
BUF_LEN, sourceAddress, sourcePort);
 
  184                 recvMsgSize = servsock->recv(buffer, receiving_now);
 
  186                 input_string_total_len += recvMsgSize;
 
  187                 bytes_in_last_pack = recvMsgSize;
 
  190                 memcpy( & longbuf[i * 
PACK_SIZE], buffer, recvMsgSize);
 
  191                 if (nullcharfound != 
true) {
 
  192                     cout << 
"INFO: The string is not entirely fit in packet " <<  total_pack << endl;
 
  199             cout << 
"INFO: Received packet from " << sourceAddress << 
":" << sourcePort << endl;
 
  201             string input_string = longbuf;
 
  202             if (input_string.length() == 0) {
 
  203                 cerr << 
"ERROR: received an empty string! Aborting..." << endl;
 
  209             string exec_cmd = 
"make fcsim -j 4";
 
  210             string ouf_file = 
"../../../../../../ROLE/custom/hls/uppercase/uppercase_prj/solution1/fcsim/build/hls_out.txt";
 
  212                 if (atoi(argv[2]) == 2) {
 
  213                     exec_cmd = 
"make csim";
 
  214                     ouf_file = 
"../../../../../../ROLE/custom/hls/uppercase/uppercase_prj/solution1/csim/build/hls_out.txt";
 
  216                 else if (atoi(argv[2]) == 3) {
 
  217                     synth_cmd = 
"make csynth && ";
 
  218                     exec_cmd = 
"make cosim";
 
  219                     ouf_file = 
"../../../../../../ROLE/custom/hls/uppercase/uppercase_prj/solution1/sim/wrapc_pc/build/hls_out.txt";
 
  221                 else if (atoi(argv[2]) == 4) {
 
  222                     exec_cmd = 
"make memchecksim";
 
  223                     ouf_file = 
"../../../../../../ROLE/custom/hls/uppercase/uppercase_prj/solution1/fcsim/build/hls_out.txt";
 
  225                 else if (atoi(argv[2]) == 5) {
 
  226                     exec_cmd = 
"make kcachegrind";
 
  227                     ouf_file = 
"../../../../../../ROLE/custom/hls/uppercase/uppercase_prj/solution1/fcsim/build/hls_out.txt";
 
  233             if (num_batch == 1) {
 
  234                 clean_cmd = 
"make clean && ";
 
  237             string str_command = 
"cd ../../../../../../ROLE/custom/hls/uppercase/ && " + clean_cmd + synth_cmd + 
"\ 
  238                         INPUT_STRING=" + input_string + 
" " + exec_cmd + 
" && \ 
  239                         cd ../../../../HOST/custom/uppercase/languages/cplusplus/build/ "; 
 
  240             const char *command = str_command.c_str(); 
 
  241             cout << 
"Calling TB with command:" << command << endl; 
 
  244             ssize_t 
size = __file_size(ouf_file.c_str());
 
  245             int rc = __file_read(ouf_file.c_str(), longbuf, 
size);
 
  247                 cerr << 
"ERROR: Cannot read file " << ouf_file << 
" . Aborting..."<< endl;
 
  251             clock_t next_cycle_rx = 
clock();
 
  252             double duration_rx = (next_cycle_rx - last_cycle_rx) / (
double) CLOCKS_PER_SEC;
 
  253             cout << 
"INFO: Effective FPS RX:" << (1 / duration_rx) << 
" \tkbps:" << (
PACK_SIZE * 
 
  254                     total_pack / duration_rx / 1024 * 8) << endl;
 
  255             last_cycle_rx = next_cycle_rx;
 
  259             string out_string = longbuf;
 
  260             if (out_string.length() == 0) {
 
  261                 cerr << 
"ERROR: Received empty string!" << endl; 
 
  265                 cout << 
"INFO: Succesfully received string from TB : " << out_string << endl; 
 
  266                 cout << 
"INFO: Will forward it back to host app ... total_pack=" << endl; 
 
  272             clock_t last_cycle_tx = 
clock();
 
  273             for (
int i = 0; i < total_pack; i++) {
 
  274                 if ( i == total_pack - 1 ) {
 
  275                     sending_now = bytes_in_last_pack;
 
  278                 sock.sendTo( & longbuf[i * 
PACK_SIZE], sending_now, sourceAddress, sourcePort);
 
  280                 servsock->send( & longbuf[i * 
PACK_SIZE], sending_now);
 
  284             clock_t next_cycle_tx = 
clock();
 
  285             double duration_tx = (next_cycle_tx - last_cycle_tx) / (
double) CLOCKS_PER_SEC;
 
  286             cout << 
"INFO: Effective FPS TX:" << (1 / duration_tx) << 
" \tkbps:" << (
PACK_SIZE * 
 
  287                total_pack / duration_tx / 1024 * 8) << endl;
 
  288             last_cycle_tx = next_cycle_tx; 
 
  290             cout << 
"\\___________________________________________________________________/" << endl;
 
  292         } 
while (run_loop == 1);
 
  297     } 
catch (SocketException & e) {
 
  298         cerr << e.what() << endl;
 
int main(int argc, char *argv[])
#define MAX_PACKETS_FOR_TB
bool findCharNullPos(char *str)