/// TKS_SharedBuffer.cpp
///
/// (c) 2005-2008 Bastian Spiegel <bs@tkscript.de> 
///     - distributed under terms of the GNU general public license (GPL).
///
///

#include <string.h>
#include "tks.h"
#include "PTN_Node.h"
#include "YAC_Buffer.h"
#include "TKS_SharedBuffer.h"

#ifdef YAC_WIN32
#include <windows.h> 
#endif

#include <time.h>


TKS_SharedBuffer::TKS_SharedBuffer(void) : YAC_Buffer() {
   class_ID = TKS_CLID_SHAREDBUFFER;
   shmOwner = 0;
   shmHandle.ptr = NULL;
}

TKS_SharedBuffer::~TKS_SharedBuffer() {
   TKS_SharedBuffer::free();
}

void TKS_SharedBuffer::free(void) {
   if(shmHandle.ptr)
   {
      freeSharedMemory();
      shmHandle.ptr = NULL;
      shmOwner      = 0;
      buffer        = NULL;
      io_offset     = 0;
      size          = 0;
      shmKey.empty();
   }
}

sSI TKS_SharedBuffer::allocSharedMemory(sSI _size) {
   TKS_SharedBuffer::free();
   
   genSharedMemoryKey();
   
   return allocSharedMemoryByKey(&shmKey, _size);
}

void TKS_SharedBuffer::genSharedMemoryKey(void) {
   time_t t; time(&t); 
   sUI rand_ms = (sUI)t;
   shmKey.alloc(18);
   shmKey.printf("%08x%08x", rand_ms, &rand_ms);
}


YAC_String *TKS_SharedBuffer::getSharedMemoryKey (void) {
   return &shmKey;
}

sSI TKS_SharedBuffer::setSize(sSI _size) {
   Dprintf("[---] warning: setSize(size=%i) called on SharedBuffer.\n", _size);
   return 0;
}

sSI TKS_SharedBuffer::resize(sSI _size) {
   Dprintf("[---] warning: resize(size=%i) called on SharedBuffer.\n", _size);
   return 0;
}

sUI YAC_VCALL TKS_SharedBuffer::yacArrayAlloc(sUI _sx, sUI _sy, sUI _type, sUI _elementByteSize) {
   Dprintf("[---] warning: yacArrayAlloc(sx=%i sy=%i type=%i elementByteSize=%i) called on SharedBuffer.\n", 
      _sx, _sy, _type, _elementByteSize);
   return 0;
}

sUI YAC_VCALL TKS_SharedBuffer::yacArrayRealloc(sUI _sx, sUI _sy, sUI _type, sUI _elementByteSize) {
   Dprintf("[---] warning: yacArrayRealloc(sx=%i sy=%i type=%i elementByteSize=%i) called on SharedBuffer.\n", 
      _sx, _sy, _type, _elementByteSize);
   return 0;
}
