Пример #1
0
void free_hash()
{
	for (unsigned int i = 0; i < MAX_HASH; ++i)
	{
		hashnode_t *temp = hashTable[i]->h_next;
		hashnode_t *todel;
		while (temp != NULL)
		{
			todel = temp;
			temp = temp->h_next;
			freeResource(todel);
		}
		pthread_rwlock_destroy(&hashTable[i]->m_rwlock);
		freeResource(hashTable[i]);
	}
}
Пример #2
0
WinDesktop::~WinDesktop()
{
  Log::info(_T("Deleting WinDesktop"));
  terminate();
  wait();
  freeResource();
  Log::info(_T("WinDesktop deleted"));
}
Пример #3
0
int handleremote(struct HTTPConnection * p)
{
  int rssum,rrsum,sendnum;
  struct ProxyConnection * proxy;
  struct DispatchConnection * dispatch;
  dispatch = (struct DispatchConnection *)p;
  int parseResult, dispatchclosed;
  if(dispatch == NULL) return -1;
  proxy = dispatch->proxy;
  if(dispatch->con.buf == NULL){
    dispatch->con.buf = initHTTPBuffer(4096*64);
    if(dispatch->con.buf == NULL){
      logerr("can't alloc memory", errno);
      return -1;
    }
  }
  if(dispatch->con.events & EPOLLOUT){
    if(dispatch->con.buf != NULL && dispatch->con.buf->wrBuf != NULL){
      sendnum = dispatch->con.buf->outBytes;
      if(dispatch->con.buf->wrReady == 1 && sendnum > 0){
	dispatchclosed = 0;
	rssum = sendtohost(dispatch->con.fd, dispatch->con.buf->wrBuf, sendnum, &dispatchclosed);
	if(rssum == sendnum){
	  printf("send %d bytes to remote.\n", rssum);
	  cleanProxyRequest(dispatch->proxy);
	}
	else{
	  dispatch->con.events &= ~EPOLLOUT;
	  printf("send error and bytes %d.\n", rssum);
	}
      }
    }
  }
  if((dispatch->con.events & EPOLLIN)){
    char * bufpos = dispatch->con.buf->rdBuf + dispatch->con.buf->bufferedBytes;
    dispatchclosed = 0;
    dispatch->con.events &= ~EPOLLIN;
    rrsum = recvfromhost(dispatch->con.fd, bufpos, &dispatchclosed);
    if(rrsum > 0){
      dispatch->con.buf->bufferedBytes += rrsum;
      printf("recv %d bytes from remote,total is %d.\n", rrsum, dispatch->con.buf->bufferedBytes);
      if(dispatch->header.parsePass != 1){
	parseResponse(dispatch->con.buf->rdBuf, dispatch->con.buf->bufferedBytes, &(dispatch->header));
      }
      else printf("http response length is %d.\n", dispatch->header.length);
      if(dispatch->header.parsePass == 1 && DispatchResulted(dispatch)){
	notifyDispatchResponse(dispatch);
      }
    }
    if(dispatchclosed == 1){
      /* remote server closed the conenction
	 In the http 1.0,the server would close the connection and indicate 
	 that the response is over.
      */

      if(dispatch->con.buf->rdEOF != 1) notifyDispatchResponse(dispatch);
      rmfromepoll(epfd, dispatch->con.fd);
      freeDispatchConnect(dispatch);
    }
  }
  if(dispatch->status == PROXY_FREE){
    /*
      when the dispatch socket is closed and proxy socket has sended the response to host,
      then free the dispatch resource.

    */

    freeResource(&dispatchlist, &freeDispatchlist, (ListItem *)dispatch);
  }
  return 0;
}
Пример #4
0
int handlehost(struct HTTPConnection * p)
{
  int dispatchfd,hssum,hrsum,sendnum;
  struct epoll_event reqev;
  struct ProxyConnection * proxy;
  struct DispatchConnection * dispatch;
  char * readBuf;
  proxy = (struct ProxyConnection *)p;
  int parseResult, proxyclosed;
  char requestbuf[1024];
  if(proxy == NULL){
    logwarn("invalid handle pointer.\n");
    return -1;
  }
  /*
    check the proxy buffer, allocate if buffer is NULL.

  */
  if(proxy->con.buf == NULL){
    proxy->con.buf = initHTTPBuffer(4096*4);
    if(proxy->con.buf == NULL){
      logerr("allocate for proxy buffer.\n");
      return -1;
    }
  }
  if(proxy->con.events & EPOLLIN){
    proxyclosed = 0;
    proxy->con.events &= ~EPOLLIN;
    readBuf = proxy->con.buf->rdBuf + proxy->con.buf->bufferedBytes;
    hrsum = recvfromhost(proxy->con.fd, readBuf, &proxyclosed);
    if(hrsum > 0){
      printf("recv %d bytes from host.\n", hrsum);
      proxy->con.buf->bufferedBytes += hrsum;
      if(proxy->header.parsePass != 1){
	parseResult = parseRequest(proxy->con.buf->rdBuf, &(proxy->header));
	printf("request's host is %s.\n", proxy->header.host);
	if(parseResult == 0){
	  printf("http header length is %d.\n", proxy->header.length);
	  printf("http uri is %s.\n", proxy->header.uri);
	}
      }
      if(proxy->dispatch == NULL){
	if(proxy->header.host[0] != '\0'){
	  if((dispatch = FindDispatchConnect(dispatchlist, proxy->header.host, PROXY_FINISHED)) != NULL){
	    /*
	      The dispatch connection to the specified host already existed.
	    */

	    printf("host %s connection already existed.\n", proxy->header.host);
	    attachProxyDispatch(proxy, dispatch);
	    dispatch->status = PROXY_USED;
	  }
	  else{
	    dispatchfd = NULLFD;
	    if(connecthost(proxy->header.host, proxy->header.port, &dispatchfd) == -1){
	      if(dispatchfd != NULLFD){
		close(dispatchfd);
		rmfromepoll(epfd, proxy->con.fd);
		freeProxyConnect(proxy);
	      }
	      return -1;
	    }
	    printf("connect success.\n");
	    setnonblock(dispatchfd);
	    if(addtoepoll(epfd, dispatchfd, ETINOUT) !=0 ){
	      logerr("add remote fd to epoll error", errno);
	      return -1;
	    }
	    List * pl;
	    struct DispatchConnection * dispatchcon;
	    pl = getDispatchResource(&freeDispatchlist, dispatchfd, handleremote);
	    dispatchcon = (struct DispatchConnection *)(pl->item);
	    AddConnectToManager(dispatchfd, dispatchcon);
	    addToList(&dispatchlist, pl);
	    strcpy(dispatchcon->host, proxy->header.host);
	    attachProxyDispatch(proxy, dispatchcon);
	  }
	}
      }
      if(proxy->header.parsePass == 1 && proxy->con.buf->bufferedBytes >= proxy->header.length){
	notifyProxyRequest(proxy);
      }
    }
    if(proxyclosed == 1){
      /*
	avoid web browser open a connection, but send no data and close after.

      */

      if(proxy->con.buf->bufferedBytes == 0) proxy->status = PROXY_FREE; 
      else if(proxy->status == PROXY_USED) proxy->status = PROXY_CLOSED;
      else if(proxy->status == PROXY_FINISHED) proxy->status = PROXY_FREE;
    }
  }
  if(proxy->con.events & EPOLLOUT){
    dispatch = proxy->dispatch;
    if(proxy->con.buf != NULL && proxy->con.buf->wrBuf != NULL){
      /*
	when the dispatch response is recived and the socket is ready for write,
	proxy sends the response to host.

      */

      sendnum = proxy->con.buf->outBytes;
      if(proxy->con.buf->wrReady == 1 && sendnum > 0){
	proxyclosed = 0;
	hssum = sendtohost(proxy->con.fd, proxy->con.buf->wrBuf, sendnum, &proxyclosed);
	if(hssum == sendnum){
	  printf("send %d bytes to host.\n", hssum);
	  sprintf(requestbuf, "%d %s %s %s.\n", dispatch->header.status, proxy->header.method, proxy->header.uri, proxy->header.version);
	  loginfo(requestbuf);
	  notifyProxyFinished(proxy);
	  cleanDispatchResponse(dispatch);
	}
	else{
	  if(proxyclosed == 1){
	    notifyProxyFinished(proxy);
	    detachProxyDispatch(proxy, dispatch);
	  }
	  else{
	    proxy->con.events &= ~EPOLLOUT;
	    printf("send error and bytes %d.\n", hssum);
	  }
	}
      }
    }
  }
  if(proxy->status == PROXY_FREE){
    /*
      when the host close the connection and proxy send the response to host,
      the proxy could also close the connection to free the socket.

    */
    
    rmfromepoll(epfd, proxy->con.fd);
    freeProxyConnect(proxy);
    freeResource(&proxylist, &freeProxylist, (ListItem *)proxy);
  }
  return 0;
}
Пример #5
0
WinDesktop::WinDesktop(ClipboardListener *extClipListener,
                       UpdateSendingListener *extUpdSendingListener,
                       AbnormDeskTermListener *extDeskTermListener)
: m_extClipListener(extClipListener),
  m_extUpdSendingListener(extUpdSendingListener),
  m_extDeskTermListener(extDeskTermListener),
  m_clToSrvChan(0),
  m_srvToClChan(0),
  m_clToSrvGate(0),
  m_srvToClGate(0),
  m_deskServWatcher(0),
  m_dispatcher(0),
  m_updateHandler(0),
  m_userInputClient(0),
  m_userInput(0),
  m_deskConf(0),
  m_gateKicker(0),
  m_wallPaper(0)
{
  Log::info(_T("Creating WinDesktop"));

  try {
    if (Configurator::getInstance()->getServiceFlag()) {
      m_deskServWatcher = new DesktopServerWatcher(this);

      m_clToSrvChan = new ReconnectingChannel(60000);
      m_srvToClChan = new ReconnectingChannel(60000);

      m_clToSrvGate = new BlockingGate(m_clToSrvChan);
      m_srvToClGate = new BlockingGate(m_srvToClChan);

      m_dispatcher = new DesktopSrvDispatcher(m_srvToClGate, this);

      m_updateHandler = new UpdateHandlerClient(m_clToSrvGate, m_dispatcher,
                                                this);

      UserInputClient *userInputClient =
        new UserInputClient(m_clToSrvGate, m_dispatcher, this);
      m_userInputClient = userInputClient;
      m_userInput = new SasUserInput(userInputClient);

      m_deskConf = new DesktopConfigClient(m_clToSrvGate);
      m_gateKicker = new GateKicker(m_clToSrvGate);
      m_dispatcher->resume();
      onConfigReload(0);
    } else {
      m_updateHandler = new LocalUpdateHandler(this);
      bool ctrlAltDelEnabled = false;
      m_userInput = new WindowsUserInput(this, ctrlAltDelEnabled);
      m_deskConf = new DesktopConfigLocal();
      applyNewConfiguration();
      m_wallPaper = new WallpaperUtil;
      m_wallPaper->updateWallpaper();
    }
    Configurator::getInstance()->addListener(this);
  } catch (Exception &ex) {
    Log::error(_T("exception during WinDesktop creaion: %s"), ex.getMessage());
    freeResource();
    throw;
  }
  resume();
}
Пример #6
0
void genericThreadLaunch(UINT level, UINT type, void* (*f)(void*))
{
	pthread_t* thread = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
	ThreadParam** thArray = (ThreadParam**)malloc(num_threads * sizeof(ThreadParam*));
	pthread_attr_t attr;
	UINT rc;
	void *status;

	/* Initialize and set thread detached attribute */
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

	UINT numCand = numCandidates;
	UINT tnumCandidates = numCand/num_threads;
	if (numCand % num_threads != 0) tnumCandidates++;
	UINT toffset = 0;
	UINT thread_count = 0;
	for(UINT t = 0; t < num_threads; t++) 
	{
		//printf("Main: creating thread %ld\n", t);
		if (numCand < tnumCandidates) tnumCandidates = numCand;
		if (tnumCandidates > 0)
		{
			thArray[t] = (ThreadParam*)malloc(sizeof(ThreadParam));
			ThreadParam* th = thArray[t];
			th->level = level;
			th->numCandidates = tnumCandidates;
			th->offset = toffset;
			allocateResource(tnumCandidates, level, th, type);
			rc = pthread_create(&thread[t], &attr, f, th);
			thread_count ++;
		}
		else
			break;

		if (rc) 
		{
			printf("ERROR; return code from pthread_create() is %d\n", rc);
			exit(-1);
		}
		toffset += tnumCandidates;
		numCand -= tnumCandidates;
	}

	/* Free attribute and wait for the other threads */
	pthread_attr_destroy(&attr);
	for(UINT t = 0; t < thread_count; t++) 
	{
		rc = pthread_join(thread[t], &status);
		if (rc) 
		{
			printf("ERROR; return code from pthread_join() is %d\n", rc);
			exit(-1);
		}
		//printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
	}

	for(UINT t = 0; t < thread_count; t++) 
	{
		freeResource(thArray[t]);
		free(thArray[t]);
		thArray[t] = NULL;
	}
	//printf("Main: program completed. Exiting.\n");
	free(thArray);
	free(thread);
}
Пример #7
0
int getepvariablesFMU( char*  const fileName, 
					   char*  const myOutputVarsName, 
					   char*  const myOutputVarsType, 
					   int*   const myNumOutputVars, 
					   char*  const myInputKeys, 
					   int*   const myNumInputKeys, 
				       char*  const myInputVars, 
					   int*   const myNumInputVars,
					   int*   const myInputVarsType,
					   int*   const myStrLen){

  FILE * fd;
  XML_Parser p;
  int i, j, count, ret;
  //ret = check_variable_cfg_Validate(fileName);
  //if(-1 == ret) 
    //return -1;

  fd = fopen(fileName, "r");
  if(!fd){
    fprintf(stderr, "Error: Could not open file '%s' when getting EnergyPlus variables.\n", fileName);
    return -1;
  }
  p = XML_ParserCreate(NULL);
  if(!p){
    fprintf(stderr, "Error: Could not allocate memory for parser in function 'getepvariables'.\n");
    return -1;
  }
  
  outputVarsName = myOutputVarsName;
  outputVarsType = myOutputVarsType;
  numOutputVars = myNumOutputVars;
  inputVars = myInputVars;
  inputVarsType = myInputVarsType;
  numInputVars = myNumInputVars;
  numInputKeys = *myNumInputKeys;
  strLen = myStrLen;
  i=0; j=0; count=0;
  inputKeys = NULL;
  while(1){
    if(myInputKeys[count] == '\0') {
      if(inputKeys[i][j] != '\0')
        inputKeys[i][j] = '\0';
      break;
    }
    if(myInputKeys[count] == ','){
      inputKeys[i][j]='\0';
      i++;
      j=0;
      count++;
    }
    else {
      if(j == 0) {
        inputKeys = (char**) realloc(inputKeys, sizeof(char*) * (i+1) );
        if(inputKeys == NULL) {
          fprintf(stderr, "Error: Memory allocation failed in 'utilXml.c'\n");
          return -1;
        }
        inputKeys[i] = NULL;
      }
          
      inputKeys[i] = (char*)realloc(inputKeys[i], sizeof(char) * (j+2) );
      if(inputKeys[i] == NULL) {
        fprintf(stderr, "Error: Memory allocation failed in 'utilXml.c'.\n");
        return -1;
      }
      inputKeys[i][j] = myInputKeys[count];
      j++; count++;
    }
  }
  if((i+1) != *myNumInputKeys ){
    fprintf(stderr, 
	    "Error: Number of input variables keys found does not match:\nFound %d, expected %d\n", 
	    i+1, * myNumInputKeys);
    freeResource(inputKeys, i+1);
    return -1;
  }
  *numOutputVars = 0;
  *numInputVars = 0;
  outputVarsName[0] = '\0';
  outputVarsType[0] = '\0';
  inputVars[0] = '\0';
  source = -1;
  ERROR_STATUS = 0;
  XML_SetElementHandler(p, EPstart, EPend);

  for (;;) {
    int done;
    int len;

    len = (int)fread(Buff, 1, BUFFSIZE, fd);
    if (ferror(fd)) {
      fprintf(stderr, "Error when reading xml variables in '%s'.\n", fileName);
      freeResource(inputKeys, numInputKeys);
      return -1;
    }
    done = feof(fd);

    if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR
	|| ERROR_STATUS == 1) {
      fprintf(stderr, "Error: Parser error in file '%s':\n%s\n",
	      fileName,
	      XML_ErrorString(XML_GetErrorCode(p)));
      freeResource(inputKeys, numInputKeys);
      return -1;
    }

    if (done)
      break;
  }
  XML_ParserFree(p);
  fclose(fd);
  freeResource(inputKeys, numInputKeys);
  return 0;
}