#include <iostream.h>
#include "client.h"
#include "daemon.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <errno.h>

//#define call_dosemu()

main() {
  char str[] = "Hello 1 2 3 testing";
  int  len = strlen (str) + 1;
  if (server_init() < 0) {
    exit(-1);
  }

    
  strcpy (server->daemon_comm.buf, str);
  
    
  DaemonMemInfo * mem = (DaemonMemInfo *)server->data.buf;
  mem->s.seg = server->daemon_comm.seg;
  mem->s.off = server->daemon_comm.off + offsetof (daemon_comms, buf);
  mem->len = 1024; // len of daemon_comm.buf
  mem->shm_key = 1000001;
  int flags = IPC_CREAT | 0666;
  int id = shmget (mem->shm_key, mem->len, flags);
  if (id == -1) {
    cerr << "shmget failed: " << strerror (errno) << endl;
    server_exit();
    exit (-1);
  }
  
  char * buf = (char *)shmat (id, 0, 0);
  if ((int)buf == -1) {
    cerr << "shmat failed: " << strerror (errno) << endl;
    server_exit();
      exit (-1);
  }
  for (int index = 0; index < 5000;index ++) {
    /* do this to copy the data in daemon_comm.buf into the dos machine */
    DOS_CMD = DAEMON_DOCMD;
    DOS_ARG = DAEMON_REPLY;
    call_dosemu();
#if 1
    /* now the real call */
    DOS_CMD = DAEMON_GETMEM;
    call_dosemu();
#endif
  }
  cout << "buf = " << buf << endl;
  shmctl (id, IPC_RMID, NULL);
  shmdt (buf);

  server_exit();
}
