Пример #1
0
int main(int argc, char **argv) {
  pthread_t tListen;
  pthread_t tTap;
  pthread_t tPoll;

  if(argc != 2) {
    exit(EXIT_FAILURE);
  }

  /* Parse config file */
  struct peerlist* peers;

  config = malloc(sizeof(struct ConfigInfo));
  getConfigInfo(argv[1], config, peers);

  printf("Listenport is %u\n",config->listenport);
  /* Run TCP Server Thread */
  pthread_create(&tListen, NULL, listenerHandler, (void *)tcp_fd);

	if(config == NULL) printf("NULL CONFIG ABOUT\n");
  /* Run TAP Thread if device has TAP */
  if(config->tap != NULL){
  printf("HEY TP\n");
    TAP_NAME = config->tap;
	printf("TAP NAME IS %s\n",localTapName);
    pthread_create(&tTap, NULL, tapHandler, (void *)tap_fd);
  }

  /* Run Polling Thread */
  pthread_create(&tPoll, NULL, refreshMembersHandle, (void *)peers);

  /* Connect to peers */
  connectToPeers(peers);

  /* Wait for TCP and TAP and Polling thread */
  pthread_join(tListen, NULL);
  if(config->tap !=NULL){
    pthread_join(tTap, NULL);
  }
  pthread_join(tPoll,NULL);

  return 0;
}
int main(int argc, char **argv) {
    parseParameters(argc, argv);

    if (pthread_mutex_init(&lock, NULL) != 0) {
        printf("ERROR: cannot initialize mutex! \n");
        exit(1);
    }

    connectToDFServer();

    connectToPeers();

    unsigned long t1, t2;
    struct timespec clock;
    unsigned long sleep_time;

    char buffer[MSG_SIZE];
    int num_received_bytes;
    int num_sent_bytes;

    /* Start by resetting the inputs and outputs to their default values */
    pthread_mutex_lock(&lock);
    clear_ua_inputs();
    clear_ua_outputs();
    initializeCustomLogic();
    pthread_mutex_unlock(&lock);

    while (1) {
        clock_gettime(CLOCK_REALTIME, &clock);
        t1 = clock.tv_sec * 1000000L;

        pthread_mutex_lock(&lock);

        /* Receive all input signals from DF server */
        num_received_bytes = recv(DF_server_socket, buffer, MSG_SIZE, MSG_DONTWAIT);
        if (num_received_bytes > 0)
            ua_receive((buffer_el *) buffer, num_received_bytes, &ua_inputs, NULL);

        /* Call the User Application generated by SCADE */
		executeOperator();

        /* Clear inputs after each cycle update */
        clear_ua_inputs();

        pthread_mutex_unlock(&lock);

        /* Send all output signals to DF server */
        num_sent_bytes = ua_send((buffer_el *) buffer, &ua_outputs, NULL);
        send(DF_server_socket, buffer, num_sent_bytes, MSG_DONTWAIT);

        /* Send messages to each peer */
        sendMessagesToPeers();

        executeCustomLogic();

        clock_gettime(CLOCK_REALTIME, &clock);
        t2 = clock.tv_sec * 1000000L;

        sleep_time = 100000L;
        if (t2 - t1 > sleep_time)
            printf("Timing: cycle delayed by %d milliseconds! \n", (int) (((t2 - t1) - sleep_time)/1000L));
        else
            usleep(sleep_time - (t2 - t1));
    }

    pthread_join(thread_receiver, NULL);

    close(DF_server_socket);
    close(broadcast_socket);
    close(listening_socket);

    pthread_mutex_destroy(&lock);

    return 0;
}