Пример #1
0
int main(int argc, char *argv[]){
  int port;
  int serversock;
  int i = 0;
  int res = 0;
  int acceptsock = -1;
  int num_requests = 0;
  int* x = NULL;
  queue_item_t* tmp = NULL;
  struct sigaction sa;
  pthread_t threads[MAX_THREADS];
  priqueue_t queue, jobs;
  tdata_t data;
  pthread_attr_t attr;
  sem_t semaphore;

  /* Signal handling */
  sa.sa_handler = sigINT_handler;
  sa.sa_flags   = 0;
  sigemptyset(&sa.sa_mask);
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGPIPE, &sa, NULL);

  /* Basic error-checking */
  if(argc < 2) {
    fprintf(stderr, "Error: No port specified.\n\tUsage: ./proxy <port>\n");
    return 1;
  }

  port = atoi(argv[1]);

  if(port < 1000 || port > 30000) {
    fprintf(stderr, "Error: Port number %d out of range. Please use a port between 1000 and 30000\n", port);
    return 1;
  }

  /* Server setup */
  serversock = newServerSocket(argv[1]);

  if(serversock == -1) {
    fprintf(stderr, "Error: Could not bind/listen to port %d\n", port);
    return 1;
  }

  /* Attributes */
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  /* Setup synchronization mutex */
  pthread_mutex_init(&mutex, NULL);
  pthread_mutex_init(&hostcheck, NULL);
  sem_init(&semaphore, 0, 0);

  /* Implement queue for caching & jobs */
  priqueue_init(&queue, MAX_CACHE, comparer, match);
  priqueue_init(&jobs, MAX_PENDING, compare_jobs, match_jobs); /* Up to 500 waiting jobs */

  /* init threads */
  data.queue = &queue;
  data.jobs  = &jobs;
  data.sem   = &semaphore;

  for(i = 0 ; i < MAX_THREADS ; ++i)
    pthread_create(&threads[i], &attr, connection_handler, (void*)&data);

  /* The server loop */
  while(!done) {
    x = (int*)malloc(sizeof(int));
    
    if((acceptsock = acceptSocket(serversock)) == -1) { /* If an error occurs or interrupted */
      if(!done)
        fprintf(stderr, "Error: Failed to connect client.\n");
      free(x);
      continue;
    }
    
    *x = acceptsock;

    pthread_mutex_lock(&job_mutex);
    res = priqueue_insert(&jobs, (void*)x);
    pthread_mutex_unlock(&job_mutex);
    if(res == -1) {
      closeSocket(acceptsock);
      free(x); /* Lost (rejected) request */
    } else {
      sem_post(&semaphore);
    }

    acceptsock = -1;
    num_requests++;
  }
  closeSocket(serversock);

  for(i = 0 ; i < MAX_THREADS ; ++i) /* Make sure everything gets through */
    sem_post(&semaphore);

  for(i = 0 ; i < MAX_THREADS ; ++i)
    pthread_join(threads[i], NULL);

  printf("%10d total HTTP requests served.\n", num_requests);

  /* Cleanup queue elements */
  for(i = 0 ; i < MAX_CACHE ; ++i) {
    tmp = (queue_item_t*)priqueue_delete_min(&queue);
    if(tmp == NULL)
      break;
    destroyQueueItem(&tmp);
  }

  for(i = 0 ; i < MAX_PENDING ; ++i) {
    x = (int*)priqueue_delete_min(&jobs);
    if(x == NULL)
      break;
    free(x);
  }

  sem_destroy(&semaphore);
  pthread_mutex_destroy(&mutex);
  pthread_mutex_destroy(&job_mutex);
  pthread_mutex_destroy(&hostcheck);
  pthread_attr_destroy(&attr);
  priqueue_destroy(&queue);
  priqueue_destroy(&jobs);
	return 0;
}
Пример #2
0
int main(int argc, char *argv[]){
  unsigned short port;
  char *description = NULL;
  int connections = 0, listen_socket, *sockp;
  pthread_t commandThread;
  if (argc != 3) {
    fprintf(stderr, "Usage: %s <port> <self>\n", argv[0]);
    exit(EXIT_FAILURE);
  }

  port = atoi(argv[1]);
  self = argv[2];

  if(iop_install_handler(SIGCHLD, 0, iop_netrequest_sigchild_handler) != 0){
    netlog("iop_netrequest could not install signal handler");
    exit(EXIT_FAILURE);
  }

  if(pthread_create(&commandThread, NULL, netrequest_cmd_thread, NULL)){
      fprintf(stderr, "Could not spawn netrequest_cmd_thread thread\n");
      return -1;
    }


  if(allocateListeningSocket(port, &listen_socket) != 1){
    fprintf(stderr, "Couldn't listen on port %d\n", port);
    exit(EXIT_FAILURE);
  }
  
  netlog("Netrequest listening on port %d\n", port);
  
  while(1){
    echofds net2Sys;
    pthread_t thrNet2Sys;

    description = NULL;
    netlog("Blocking on acceptSocket (connections = %d)\n", connections);
    sockp = acceptSocket(listen_socket, &description);
    
    connections++;

    netlog("Woken from acceptSocket: %s)\n", description);

    if (*sockp == INVALID_SOCKET) {
      netlog("%s", description);
      free(description);
      continue;
    }

    
    net2Sys.from = *sockp;
    net2Sys.to = STDOUT_FILENO;

    if(pthread_create(&thrNet2Sys, NULL, handleRequest, &net2Sys) != 0){
      netlog("Couldn't create thrR2A thread\n");
    }
    netlog("%s", description);
    free(sockp);
    free(description);
  }
}
Пример #3
0
static void
ws_accept(XtPointer xp, int *source, XtInputId *id)
{ acceptSocket((Socket) xp);
}
Пример #4
0
main(int argc, char **argv)
{
    if(argc != REQ_ARG_NUM) {
        printf("usage: %s [listen port]\n", argv[0]);
        exit(0);
    }

    int port = atoi(argv[L_PORT_ARG]);
    int listenSocket = makeListenSocket(port);
    printf("\nListen Port %d -> %s (%s)\n",
           port, argv[D_HOST_ARG], argv[D_PORT_ARG]);
    fflush(stdout);
    
    fd_set rfds;
    FD_ZERO(&rfds);
    FD_SET(listenSocket, &rfds);

    RcvList *rcvList = NULL;
    char rcvBuf[RCV_BUF_SIZE];
    for(;;) {
        fd_set rflg = rfds;
        int ret = select(FD_SETSIZE, &rflg, NULL, NULL, NULL);
        if(ret < 0) {
            perror("main:select");
            exit(1);
        }
        else if(ret > 0) {

            RcvList **pp = &rcvList;
            while(*pp != NULL) {
                int rcvRes = 0;
                if(FD_ISSET((*pp)->clientSocket, &rflg)) {
                    rcvRes = rcvSocket((*pp)->clientSocket,
                                           rcvBuf, sizeof(rcvBuf));
                    printf("%s->:%d[byte]\n", (*pp)->clientHost, rcvRes);
		    printf("HEX:");
		    for(int i=0; i < rcvRes; i++) {
		      printf("%02X,",rcvBuf[i]);
		    }
		    printf("\nSTR:");
		    for(int i=0; i < rcvRes; i++) {
		      printf("%c",rcvBuf[i]);
		    }
		    printf("\n");
                }
                if(rcvRes < 0) {
                    deleteRcvNode(pp, &rfds);
                    continue;
                }
                pp = &((*pp)->next);
            }

            if(FD_ISSET(listenSocket, &rflg)) {
                int clientSocket = acceptSocket(listenSocket);
                if(clientSocket>=0) {
                    addRcvNode(&rcvList, &rfds, clientSocket);
                }
            }
        }
    }
}
Пример #5
0
void processIncoming() {
	struct timespec ts = { 1, 0 };
	struct kevent cl = { sfd, EVFILT_READ, EV_ADD|EV_ONESHOT, 0, 0, 0 };
	struct kevent el = { 0, 0, 0, 0, 0, 0 };
	if (kevent(kq,&cl,1,&el,1,&ts)>0) spawn(acceptSocket(sfd));
}
Пример #6
0
int main(int argc, char** argv) {

    printf("************* Welcome to UCLA FPGA agent! **********\n");
#if SOCKET
    int listenfd = setupSocket( 5000 );
#endif
    struct cl_package clPackage = initFPGA("logistic_lpp.xclbin", "logistic");

    float* weights = (float*)malloc(LABEL_SIZE*FEATURE_SIZE*sizeof(float));
    float* data = (float*)malloc(PARTITION_SIZE*(FEATURE_SIZE+LABEL_SIZE)*sizeof(float));
    float* gradient = (float*)malloc(LABEL_SIZE*FEATURE_SIZE*sizeof(float));

#if SOCKET
    while (1) 
#endif
    {
#if SOCKET
        int connfd = acceptSocket(listenfd);
#endif
        printf("************* Got a new task! *************\n");
        //fread(A, n*n, sizeof(float), fin);
        //fread(B, n*n, sizeof(float), fin);
        size_t nbyte;
        int L;
        int D;
        int n;
        int cached;
#if SOCKET
        timespec timer1 = tic( );
        nbyte = recv(connfd, &L, sizeof(L), MSG_WAITALL);
        nbyte = recv(connfd, &D, sizeof(D), MSG_WAITALL);
        nbyte = recv(connfd, &n, sizeof(n), MSG_WAITALL);
        nbyte = recv(connfd, &cached, sizeof(cached), MSG_WAITALL);
        printf("parameter recieved --- ");
        toc( timer1 );
#else
        L = LABEL_SIZE;
        D = FEATURE_SIZE;
		if (argc > 1)
			n = atoi(argv[1]);
		else
			n = 100;
#endif

        printf("# of labels: %d\n", L);
        printf("# of features: %d\n", D);
        printf("# of data points: %d\n", n);
        
        if(L>1024 || D>1024 || n > 65536)
        {
            printf("ERROR: too large data size!\n");
            return 1;
        }
        memset((void*)gradient, 0, L*D*sizeof(float));

        int i;
#if SOCKET
        nbyte = recv_large_array(connfd, (char*)weights, L*D*sizeof(float));
        printf("received weights for %d bytes --- ", nbyte);
        toc( timer1 );
#if SHOW_DATA
        printf("the first 10 elements are:\n");
        for( i = 0; i < 10; i++ ) printf("%f\n", weights[i]);
#endif
        if( cached == 0 )
        {
            //nbyte = recv(connfd, data, n*(D+L)*sizeof(float), MSG_WAITALL);
            nbyte = recv_large_array( connfd, (char*)data, n*(D+L)*sizeof(float) );
            printf("received training data for %d bytes --- ", nbyte);
            toc( timer1 );
        }
#if SHOW_DATA
        printf("the first 10 elements are:\n");
        for( i = 0; i < 10; i++ ) printf("%f\n", data[i]);
#endif
#else
        for( i = 0; i < D; i++ ) weights[i]=0.;
        FILE* pFile = fopen("train_data.txt","r");
        for( i = 0; i < n*(D+L); i++ ) fscanf(pFile, "%f", data+i);
        fclose(pFile);
#endif
        
        printf("FPGA computation...\n");
        //computeGradient(weights,data,gradient,L,D,n);
        computeGradientByFPGA(weights,data,gradient,L,D,n,clPackage);
        printf("FPGA computation finished! --- ");
        toc( timer1 );

#if SOCKET
        //for( int i = 0; i < 10; i++ ) gradient[i]=i;
        nbyte = send(connfd, gradient, L*D*sizeof(float), 0);
        //int temp = tr.tv_usec;
        //nbyte = send(connfd, &temp, sizeof(int), 0);
        printf("sent gradient for %d bytes --- ", nbyte);
        toc( timer1 );
        printf("************* Task finished! *************\n");
#else
        for( i = 0; i < 10; i++ ) printf("%f\n",gradient[i]);
#endif


#if SOCKET
        close(connfd);
#endif
    }

    free(weights);
    free(data);
    free(gradient);
	clReleaseMemObject(clPackage.d_weights);
	clReleaseMemObject(clPackage.d_data);
	clReleaseMemObject(clPackage.d_gradient);

	return 0;
}