Beispiel #1
0
int main(int argc, char** argv)
{
    int score, round;
    d = DictCreate();

    /*
    DictInsert(d, "foo", "hello world");
    puts(DictSearch(d, "foo"));
    DictInsert(d, "foo", "hello world2");
    puts(DictSearch(d, "foo"));
    DictDelete(d, "foo");
    puts(DictSearch(d, "foo"));
    DictDelete(d, "foo");
    assert(DictSearch(d, "foo") == 0);
    DictDelete(d, "foo");

    for(i = 0; i < 10000; i++) {
      sprintf(buf, "%d", i);
      DictInsert(d, buf, buf);
    }
    */

    round = atoi(argv[1]);
    score = atoi(argv[2]);

    printf("%f\n", scoreProb(round, score));

    DictDestroy(d);

    return 0;
}
Beispiel #2
0
void init()
{
  num_msg     = 0;
  client_id   = 0;
  messages    = DictCreateS();
  clients     = DictCreate();
  msg_history = DictCreateS();
}//end init
Beispiel #3
0
int main (int argc, char *argv[]){
          /*int c;
          FILE *pp;
          extern FILE *popen();
          if ( !(pp=popen("ls -l", "r")) ) 
            return 1;
 
          while ( (c=fgetc(pp)) != EOF ) {
          putc(c, stdout); 
          fflush(pp);
          }   
          pclose(pp);*/

  //пользовательскике данные
  struct UserParams userParams;
  Dict login = DictCreate();
  Dict fLogin = DictCreate();
  Dict location = DictCreate();;
  userParams.login = login;
  userParams.fLogin = fLogin;
  userParams.location = location;

  //параметры адреса
  printf("starting server\n");
  struct addrinfo hints; 
  memset(&hints, 0, sizeof hints);
  hints.ai_flags =  AI_PASSIVE;
  hints.ai_family = AF_UNSPEC; 
  hints.ai_socktype = SOCK_STREAM; 
  hints.ai_protocol = IPPROTO_TCP; 
  
  // получаение списка доступных адресов
  char* port = "8080";
  struct addrinfo* addresses = getAvilableAddresses(&hints, port); 
  if(!addresses)
    return EXIT_FAILURE;
  
  //коннект сокета
  int socketfd = assignSocket(addresses);
  if(!socketfd)
    return EXIT_FAILURE;


  if (setNonBlock(socketfd) == -1){
    perror("non_block");
    return EXIT_FAILURE;
  }


  if (listen (socketfd, SOMAXCONN) == -1){
    perror ("listen");
    return EXIT_FAILURE;
  }
  printf("listening port %s\n", port);
  
  //создание epoll
  int epollfd = epoll_create1(0);
  if (epollfd == -1){
    perror ("epoll_create");
    return EXIT_FAILURE;
  }


  if (addToEpoll(epollfd, socketfd) == -1)
    return EXIT_FAILURE;
  
  //регистрация обработчика событий
  signal(SIGINT, handleSignal);
    
  int maxEventNum = 8;
  struct epoll_event events[maxEventNum * sizeof(struct epoll_event)];
  //заполнение параметров для потоков
  struct Params params;
  pthread_mutex_init(&params.mutex, NULL);
  pthread_cond_init(&params.condvar, NULL); 
  params.end = 0;
  params.currrent = 0;
  params.epollfd = epollfd;
  params.events = events;
  params.socketfd = socketfd;
  params.userParams = userParams;
  pthread_t working[WORKING_THREADS_COUNT]; 
  for(int i = 0; i<WORKING_THREADS_COUNT; i++){   
    pthread_create(&working[i], NULL, workThread, &params);
  } 

  //обработка событий
  int timeout = -1 ;
  while(!done){
    if(params.currrent >= params.end){
      printf("waiting new events\n");
      int eventsNumber = epoll_wait(epollfd, events, maxEventNum, timeout);
      params.currrent = 0;
      params.end = eventsNumber;
      printf("Send\n");
      pthread_cond_signal(&params.condvar); 
    }
  }
  
  printf("server is going down\n");
  printf("closing connections\n");
  close(socketfd);
  close(epollfd);
  printf("done\n");
  return EXIT_SUCCESS;
}
Beispiel #4
0
int search(struct position source, struct position target, int (*blocked)(struct position))
{
    struct position current;
    current = source;

    Dict d;
    d = DictCreate();

    PQ* q;
    q = initQueue();

    heapNode hn;
    hn.distFromSource = 0;
    hn.value = abs(current.x - target.x) + abs(current.y - target.y);
    hn.pos = current;
    enqueue(hn,q);
    DictInsert(d,current.x,current.y,hn.distFromSource);

    while(q->size)	//stops when priority queue is empty
    {
        int i;
        hn = dequeue(q);
        current = hn.pos;
        if(foundTarget(current,target))	//hooray! target found!!
        {
            DictDestroy(d);
            destroyQueue(q);
            return hn.distFromSource;
        }
        for(i = 0; i < 4; i++) //for loop explores all the four neighbors defined as 0...3
        {
            struct position neighbor = createNeighbor(current,i);
            int dictSearchDist = DictSearch(d,neighbor.x,neighbor.y);
            if(!blocked(neighbor) && dictSearchDist < 0)	//add the neighbor to PQ
            {
                DictInsert(d,neighbor.x,neighbor.y,hn.distFromSource + 1);
                heapNode node;
                int distToTarget = abs(neighbor.x - target.x) + abs(neighbor.y - target.y);	//manhattan distance
                node.value = (hn.distFromSource + 1) + distToTarget;
                node.pos = neighbor;
                node.distFromSource = hn.distFromSource + 1;
                enqueue(node,q);
            }
            else if(dictSearchDist >= 0)
            {
                if(dictSearchDist > hn.distFromSource + 1)
                {
                    DictInsert(d,neighbor.x,neighbor.y,hn.distFromSource + 1);
                    heapNode node;
                    int distToTarget = abs(neighbor.x - target.x) + abs(neighbor.y - target.y);	//manhattan distance
                    node.value = (hn.distFromSource + 1) + distToTarget;
                    node.pos = neighbor;
                    node.distFromSource = hn.distFromSource + 1;
                    enqueue(node,q);
                }
            }
        }
    }
    DictDestroy(d);
    destroyQueue(q);
    return NO_PATH;
}