#ifndef PLATFORM_UNIX
#error "Compiling commlib-unix.c without PLATFORM_UNIX defined"
#endif

#include <pthread.h>
#include <unistd.h>

#include "commlib-parent.h"
#include "commlib-child.h"


void TERM_Write(char c) {
  printf("%c",c);
} /* TERM_Write */

unsigned char TERM_Read() {
  char c;
  scanf("%c",&c);
  return c;
} /* TERM_Read */

void *test_parent(void *arg) {
  int count;
  char buf[257];

  fprintf(stderr,"Entered test_parent...\n");
  sleep(1);

  /* test 1: write a block to child */
  printf("---\n");
  printf("\nTest 1: Send an initial block to child (simulating initial download)\n\n");
  fprintf(stderr,"test_parent: writing block to child...\n");
  parent_write_block_to_child("test1",5);
  
  sleep(5);
  printf("\nMP3 decoding takes forever on the HC11...\n");
  sleep(3);

  /* test 2: read a block from child */
  printf("---\n");
  printf("\n\nThe child now sends decoded data back to the parent...\n\n");
  printf("test_parent: reading block from child...\n");
  count = parent_read_block_from_child(buf);
  buf[count] = '\0';
  printf("test_parent: got from child: '%s'\n",buf);

  sleep(10);

  /* test 3: write a block to child */
  printf("---\n");
  printf("\n\nSending the next MP3 block to the child...\n\n");
  fprintf(stderr,"test_parent: writing block to child...\n");
  parent_write_block_to_child("test3",5);

  
  sleep(5);

  /* test 4: write a block to child */
  printf("---\n");
  printf("Sending another block to child (since a router-type child may request more\n");
  printf("than one block to decode concurrently).\n\n");
  fprintf(stderr,"\n\ntest_parent: writing block to child...\n");
  parent_write_block_to_child("test4",5);

  sleep(10);

  /* test 5: read a block from child */
  printf("---\n");
  printf("\n\nReading decoded block 1\n\n");
  printf("test_parent: reading block from child...\n");
  count = parent_read_block_from_child(buf);
  buf[count] = '\0';
  printf("test_parent: got from child: '%s'\n",buf);

  sleep(5);

  /* test 6: read second block from child */
  printf("---\n");
  printf("\n\nReading second decoded block\n\n");
  printf("test_parent: reading block from child...\n");
  count = parent_read_block_from_child(buf);
  buf[count] = '\0';
  printf("test_parent: got from child: '%s'\n",buf);


  return NULL;
} /* test_parent */

void *test_child(void *arg) {
  int count;
  char buf[257];

  fprintf(stderr,"Entered test_child...\n");
  sleep(1);

  /* test 1: read block from parent */
  fprintf(stderr,"test_child: requesting block from parent...\n");
  count = child_req_block_from_parent(buf);
  buf[count] = '\0';
  printf("test_child: got from parent: '%s'\n",buf);

  /* test 2: send corresponding block back to parent */
  printf("test_child: sending block to parent...\n");
  child_write_block_to_parent("test2",5);

  /* test 3: read block from parent */
  fprintf(stderr,"test_child: requesting block from parent...\n");
  count = child_req_block_from_parent(buf);
  buf[count] = '\0';
  printf("test_child: got from parent: '%s'\n",buf);

  /* test 4: read block from parent */
  fprintf(stderr,"test_child: requesting block from parent...\n");
  count = child_req_block_from_parent(buf);
  buf[count] = '\0';
  printf("test_child: got from parent: '%s'\n",buf);

  /* test 5: send corresponding block back to parent */
  printf("test_child: sending block to parent...\n");
  child_write_block_to_parent("test5",5);

  /* test 6: send corresponding block back to parent */
  printf("test_child: sending block to parent...\n");
  child_write_block_to_parent("test6",5);

  return NULL;
} /* test_child */




int main(int argc, char **argv) {
  pthread_t ptchild, ptparent;

  commlib_init();

  pthread_create(&ptchild,NULL,test_child,NULL);
  pthread_create(&ptparent,NULL,test_parent,NULL);
  
  pthread_join(ptchild,NULL);
  pthread_join(ptparent,NULL);

  return 0;
} /* main */


