#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 putmem";
  int  len = strlen (str) + 1;
  if (server_init() < 0)  {
    exit(-1);
  }
  char buf2[1024];
    
//  strcpy (server->daemon_comm.buf, str);
  daemon_comms & dcom = server->daemon_comm;
  bzero (dcom.buf, 1024);
#if 0
  DaemonMemInfo * mem = (DaemonMemInfo *)server->data.buf;
#else
  DaemonMemInfo * mem = new DaemonMemInfo;
#endif
  mem->s.seg = dcom.seg;
  mem->s.off = dcom.off + offsetof (daemon_comms, buf);
  mem->len = 1024; // len of daemon_comm.buf
  mem->shm_key = 1000001;
#if 1
  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);
  }
#endif
  for (int index = 0; index < 1;index ++) {

#if 0
    DOS_CMD = DAEMON_PUTMEM;
    strcpy (buf, str);
    call_dosemu();
#else
//    Daemon_putmem (mem, str);
    Daemon_putmem (dcom.seg, dcom.off + offsetof (daemon_comms, buf), str, strlen(str));
#endif
    // have to use getmem, to retrieve the data as any other op will overwrite
    // dcom.buf
    Daemon_getmem (buf2, dcom.seg, dcom.off + offsetof (daemon_comms, buf), 100);
    
  }
  cout << "dcom.buf = " << dcom.buf << endl;
  cout << "buf = " << buf << endl;
  cout << "buf2 = " << buf2 << endl;
#if 1
  shmctl (id, IPC_RMID, NULL);
  shmdt (buf);
#endif
  server_exit();
}
