#include <WScom.h>
#include <WSCfunctionList.h>
#include <WSCbase.h>
//----------------------------------------------------------
//Function for the event procedure
//----------------------------------------------------------
#include <WSDappDev.h>
#include <WSCconductor.h>
#include <newwin000.h>

void initop(WSCbase* object){
  char buf1[256];
  strcpy(buf1,"127.0.0.1");

  //do something...
  if (WSGIappDev()->getArgc() > 1){
    char** ptr = WSGIappDev()->getArgv();
    WSGIconductor()->setAddr(ptr[1]);
    newvudp_000->setBindAddr(ptr[1]);
    strncpy(buf1,ptr[1],256);
    buf1[255] = 0;
  }
  if (WSGIappDev()->getArgc() > 2){
    char** ptr = WSGIappDev()->getArgv();
    newvudp_000->setProperty(WSNip,ptr[2]);
  }
  WSGIconductor()->setPort(WS_DEFAULT_AGENT_PORT);
  WSGIconductor()->startDispatch();
  newvudp_000->setProperty(WSNport,WS_DEFAULT_AGENT_UDP_PORT);
  newvudp_000->setProperty(WSNrunning,True);
//printf("initop...\n");

  WSCuchar buffer[256];
  WSCushort port = WS_DEFAULT_AGENT_UDP_PORT;
  buffer[0] = 'w';
  buffer[1] = 's';
  buffer[2] = 'a';
  buffer[3] = 'U'; //UPDATE: requests new instance data.
  buffer[4] = port & 0xff;
  buffer[5] = port >> 8;
  strncpy((char*)&buffer[6],buf1,26);
  buffer[31] = 0;
  strncpy((char*)&buffer[32],"",48);
  buffer[79] = 0;
  strncpy((char*)&buffer[80],"",64);
  buffer[143] = 0;
  strncpy((char*)&buffer[144],"",64);
  buffer[207] = 0;
  long ret = newvudp_000->write(buffer,256);
//printf("ret=%d\n",ret);
  if (ret != 256){
    char buffer[256];
    newvudp_000->getErrorMsg(buffer,256);
fprintf(stderr,"wsagent: error in initializing. udp_send:%d %s\n",ret,buffer);
  }

}
static WSCfunctionRegister  op("initop",(void*)initop);
