Code Search for Developers
 
 
  

tcpxfer_data.c from EmStar at Krugle


Show tcpxfer_data.c syntax highlighted



char tcpxfer_data_c_cvsid[] = "$Id: tcpxfer_data.c,v 1.8 2007/01/08 01:06:26 mlukac Exp $";


#include "tcpxfer_i.h"

void tcpxfer_data_print_state(tcpxfer_data_t *tdt, buf_t *buf)
{
  switch (tdt->status) {
  case XFERFILE_INIT:
    bufprintf(buf, "INIT");
    break;
  case XFERFILE_SEND_INIT:
    bufprintf(buf, "SEND_INIT");
    break;
  case XFERFILE_SEND_SEND:
    bufprintf(buf, "SEND_SEND");
    break;
  case XFERFILE_SEND_RETRY:
    bufprintf(buf, "SEND_RETRY");
    break;
  case XFERFILE_SEND_CLOSE:
    bufprintf(buf, "SEND_CLOSE");
    break;
  case XFERFILE_SEND_DONE:
    bufprintf(buf, "SEND_DONE");
    break;
  case XFERFILE_RECV_INIT:
    bufprintf(buf, "RECV_INIT");
    break;
  case XFERFILE_RECV_RECV:
    bufprintf(buf, "RECV_RECV");
    break;
  case XFERFILE_RECV_RETRY:
    bufprintf(buf, "RECV_RETRY");
    break;
  case XFERFILE_RECV_CLOSE:
    bufprintf(buf, "RECV_CLOSE");
    break;
  case XFERFILE_RECV_DONE:
    bufprintf(buf, "RECV_DONE");
    break;
  default:
    bufprintf(buf, "???");
  }
}

void tcpxfer_data_print_proto_code(uint32_t icode)
{
  char code[1024];

  switch(icode) {
  case TCPXFER_DONEXFER:
    sprintf(code, "xfer done");
    break;
  case TCPXFER_ERRORINTERNAL:
    sprintf(code, "receiver internal error");
    break;
  case TCPXFER_FILEEXISTS:
    sprintf(code, "file exists, done sending");
    break;
  case TCPXFER_ERRORPROTO:
    sprintf(code, "protocol error");
    break;
  case TCPXFER_ERRORWRITE:
    sprintf(code, "receiver write error");
    break;
  case TCPXFER_ERRORMD5:
    sprintf(code, "receiver md5 error");
    break;
  case TCPXFER_ERRORALREADY:
    sprintf(code, "receiver is currently receiving file");
    break;
  case TCPXFER_TIMEDOUT:
    sprintf(code, "recevier said we timed out!");
    break;
    /*
  case TCPXFER_:
    sprintf(code, "");
    break;
    */
  default:
    sprintf(code, "Unknown code!");
    break;
  }
  elog(LOG_WARNING, "Protocode: %i:%s", icode, code);

}


int tcpxfer_data_clean_up(void *data, int interval, g_event_t *event)
{
  int i = 0, j = 0;
  tcpxfer_t *txt = (tcpxfer_t *) data;
  for (i = 0; i < TCPXFER_MAX_XFERS; ++i) {

    if (txt->allxfers[i] == NULL)
      continue;

    if ((txt->allxfers[i]->status == XFERFILE_SEND_DONE ||
	 txt->allxfers[i]->status == XFERFILE_RECV_DONE) &&
	msec_since(&txt->allxfers[i]->retrysince) > TCPXFER_LIST_TIMEOUT) {
      tcpxfer_data_t *tdt = txt->allxfers[i];
      elog(LOG_WARNING, "Removing data for done file %i:%s:%s", tdt->xfer_id, tdt->file, tdt->addr);
      for (j = 0; j < TCPXFER_SEND_MAX_CHANNELS; ++j) {
	if (tcpxfer_channels[j].transfer != NULL) {
	  tcpxfer_data_t *ttdt = tcpxfer_channels[j].transfer;
	  if (strcmp(ttdt->file, tdt->file) == 0 &&
	      strcmp(ttdt->addr, tdt->addr) == 0) {
	    tcpxfer_channels[j].status = 0;
	    tcpxfer_channels[j].transfer = NULL;
	  }
	}
      }
      //close(tdt->fd);
      tcpxfer_file_remove_all_meta(tdt);
      buf_free(tdt->tempbuf);
      g_event_destroy(tdt->send_timer); tdt->send_timer = NULL;
      g_event_destroy(tdt->transfer_timeout); tdt->transfer_timeout = NULL;
      free(tdt);
      txt->allxfers[i] = NULL;
      continue;
    } 
    if ((txt->allxfers[i]->status == XFERFILE_SEND_RETRY ||
	 txt->allxfers[i]->status == XFERFILE_RECV_RETRY) &&
	msec_since(&txt->allxfers[i]->retrysince) > TCPXFER_LIST_TIMEOUT) {
      tcpxfer_data_t *tdt = txt->allxfers[i];
      elog(LOG_WARNING, "Removing retried file %i:%s:%s", tdt->xfer_id, tdt->file, tdt->addr);
      for (j = 0; j < TCPXFER_SEND_MAX_CHANNELS; ++j) {
	if (tcpxfer_channels[j].transfer != NULL) {
	  tcpxfer_data_t *ttdt = tcpxfer_channels[j].transfer;
	  if (strcmp(ttdt->file, tdt->file) == 0 &&
	      strcmp(ttdt->addr, tdt->addr) == 0) {
	    tcpxfer_channels[j].status = 0;
	    tcpxfer_channels[j].transfer = NULL;
	  }
	}
      }
      buf_free(tdt->tempbuf);
      //close(tdt->fd);
      g_event_destroy(tdt->send_timer); tdt->send_timer = NULL;
      g_event_destroy(tdt->transfer_timeout); tdt->transfer_timeout = NULL;
      free(tdt);
      txt->allxfers[i] = NULL;
    }
  }

  return EVENT_RENEW;
}



void tcpxfer_data_set_retry(tcpxfer_data_t *tdt, int sending)
{
  tdt->totaltime += msec_since(&(tdt->retrysince));
  gettimeofday(&tdt->retrysince, NULL);
  if (sending) {
#if 0 /* REMOVE QUERY */  // now done in response to send command
    if (!tdt->query_from_dts)
      qdev_set_curr_client_data(tdt->state->query, NULL);
    else
      qdev_set_curr_client_data(tdt->state->query_dts, NULL);
#endif
    close(tdt->fd);
    tdt->fd = -1;
    tdt->status = XFERFILE_SEND_RETRY;
    g_event_destroy(tdt->send_timer);
    tdt->send_timer = NULL;
  } else {
    tdt->status = XFERFILE_RECV_RETRY;
  }
  g_event_destroy(tdt->transfer_timeout);
  g_event_destroy(tdt->finish_conn_timer);
  tdt->finish_conn_timer = NULL;
  tdt->transfer_timeout = NULL;
}

void tcpxfer_data_set_done(tcpxfer_data_t *tdt, int sending)
{
  gettimeofday(&tdt->retrysince, NULL);
  if (sending) {
#if 0 /* REMOVE QUERY */ // now done in response to send command
    if (!tdt->query_from_dts)
      qdev_set_curr_client_data(tdt->state->query, NULL);
    else
      qdev_set_curr_client_data(tdt->state->query_dts, NULL);
#endif
    close(tdt->fd);
    tdt->fd = -1;
    tdt->status = XFERFILE_SEND_DONE;
    g_event_destroy(tdt->send_timer);
    tdt->send_timer = NULL;
  } else {
    tdt->status = XFERFILE_RECV_DONE;
  }
  g_event_destroy(tdt->transfer_timeout);
  g_event_destroy(tdt->finish_conn_timer);
  tdt->finish_conn_timer = NULL;
  tdt->transfer_timeout = NULL;
}


int tcpxfer_data_addxfer(tcpxfer_t *txt, tcpxfer_data_t *tdt)
{
  int i = 0;
  for (i = 0; i < TCPXFER_MAX_XFERS; ++i) {
    if (txt->allxfers[i] == NULL) {
      txt->allxfers[i] = tdt;
      return i;
    }
  }
  return -1;
}


tcpxfer_data_t * tcpxfer_data_find_send_by_file_addr(tcpxfer_t *txt,
						     char *file,
						     char *addr)
{
  int i = 0;
  for (i = 0; i < TCPXFER_MAX_XFERS; ++i) {
    if (txt->allxfers[i] != NULL) {
      if (strcmp(txt->allxfers[i]->file, file) == 0 &&
	  strcmp(txt->allxfers[i]->addr, addr) == 0 &&
	  (txt->allxfers[i]->status == XFERFILE_SEND_INIT ||
	   txt->allxfers[i]->status == XFERFILE_SEND_SEND ||
	   txt->allxfers[i]->status == XFERFILE_SEND_RETRY ||
	   txt->allxfers[i]->status == XFERFILE_SEND_CLOSE ||
	   txt->allxfers[i]->status == XFERFILE_SEND_DONE)
	  ) {
	if (txt->allxfers[i]->status != XFERFILE_SEND_RETRY) {
	  elog(LOG_WARNING, "Reusing found filedata... but status invalid");
	}
	return txt->allxfers[i];
      }
    }
  }
  return NULL;
}

tcpxfer_data_t *tcpxfer_data_find_recv_by_file_md5(tcpxfer_t *txt,
						   char *file,
						   unsigned char *md5)
{
  int i = 0;
  for (i = 0; i < TCPXFER_MAX_XFERS; ++i) {
    if (txt->allxfers[i] != NULL) {
      if (strcmp(txt->allxfers[i]->file, file) == 0 &&
	  memcmp(txt->allxfers[i]->md5sum, md5, sizeof(unsigned char) * 16) == 0 &&
	  (txt->allxfers[i]->status == XFERFILE_RECV_INIT ||
	   txt->allxfers[i]->status == XFERFILE_RECV_RECV ||
	   txt->allxfers[i]->status == XFERFILE_RECV_RETRY ||
	   txt->allxfers[i]->status == XFERFILE_RECV_CLOSE ||
	   txt->allxfers[i]->status == XFERFILE_RECV_DONE)
	  ) {
	return txt->allxfers[i];
      }
    }
  }
  return NULL;
}





See more files for this project here

EmStar

EmStar is a software system for developing and deploying wireless sensor networks involving Linux-based platforms. As the wireless sensor network community has attempted to deploy more complex designs---large-scale, long-lived systems that need self-organization and adaptivity---a number of difficult software design issues have arisen. Advances in software design have not kept pace with the capabilities of hardware. This is because designing for an adaptive, efficient, and useful sensor network has turned out to be surprisingly complex and difficult. EmStar is a Linux-based software framework, whose goal is to dramatically reduce this complexity, enabling work to be shared and reused, and simplifying and speeding the design of new sensor network applications.

Project homepage: http://cvs.cens.ucla.edu/emstar/
Programming language(s): C,Shell Script
License: other

  TODO
  md5.c
  md5.h
  tcpxfer_client.c
  tcpxfer_data.c
  tcpxfer_file.c
  tcpxfer_i.h
  tcpxfer_main.c
  tcpxfer_md5.c
  tcpxfer_packet.c
  tcpxfer_query.c
  tcpxfer_send.c
  tcpxfer_server.c
  tcpxfer_status.c