Exemplo n.º 1
0
int main()
{
#ifdef DEBUG
	fp = fopen("test.txt","r");
#endif
	
	int n , m , k;
	
	int i , j;

	int t1 , t2;
	list *tmp_list;

	int l;

	tree_node* levels[11];
	tree_node* tmp_node;
	tree_node* node_prev_level;
	tree_node* tmp_parent;

	char tmp_char;
	char c1 , c2;

	int tmp_type;

	int ac1 , ac2;

	int valid;

	int cases;

	cases = 0;

	while( 1 )
	{
		
		SCAN("%d %d %d" , &n , &m , &k );
		if( n == 0 )
			break;

		cases++;
		if( cases != 1 )
			printf("\n");
		printf("NTA%d:\n" , cases );

		for( i = 0 ; i < n ; i++ )
			for( j = 0 ; j < k ; j++ )
			{
				SCAN("%d %d" , &t1 , &t2 );
				signal_table[i][j].l = t1;
				signal_table[i][j].r = t2;
				tmp_list = &signal_table[i][j];

				while( GETCHAR() != '\n' )
				{
					SCAN("%d %d" , &t1 , &t2 );
					tmp_list->next = (list*)malloc( sizeof(list) );
					tmp_list = tmp_list->next;
					tmp_list->l = t1;
					tmp_list->r = t2;
				}

				tmp_list->next = NULL;
			}

		/*for( i = 0 ; i < n ;i++ )
			for( j = 0 ; j < k; j++ )
			{
				tmp_list = &signal_table[i][j];

				while( tmp_list )
				{
					printf("%d %d " , tmp_list->l , tmp_list->r );
					tmp_list = tmp_list->next;
				}

				printf("\n");
			}
		*/


		while( 1 )
		{
			SCAN("%d\n" , &l );
			if( l == -1 )
				break;
			
			format_cache();
			valid = 0;

			/*PRINT("REACH 1:l=%d\n" , l);*/

			levels[0] = get_node();
			tmp_node = levels[0];
			tmp_char = GETCHAR();
			GETCHAR();
			tmp_node->type = tmp_char - 'a';
			tmp_node->next = get_node();


			for( i = 1 ; i <= l ; i++ )
			{
				node_prev_level = levels[i-1];

				levels[i] = get_node();

				tmp_node = levels[i];
				do
				{
					/*while( node_prev_level->type == '*'-'a' )
						node_prev_level = node_prev_level->next;*/

					SCAN("%c %c" , &c1 , &c2 );
					/*PRINT("REACH 2: c1=%c c2=%c\n" , c1 , c2 );*/

					tmp_node->next = get_node();
					tmp_node->parent = node_prev_level;
					tmp_node->type = c1 - 'a';
					if( c1 == '*' )
						tmp_node->accept = 0xffff<<(n-m) & 0xffff>>(16-n);
					tmp_node = tmp_node->next;

					tmp_node->next = get_node();
					tmp_node->parent = node_prev_level;
					tmp_node->type = c2 - 'a';
					if( c2 == '*' )
						tmp_node->accept = 0xffff<<(n-m) & 0xffff>>(16-n);
					tmp_node = tmp_node->next;

					node_prev_level = node_prev_level->next;
					if( node_prev_level == NULL )
					{
						malloc(1000000);
						return 0;
					}

					tmp_char = GETCHAR();	
				}while(tmp_char == ' ');

			}

			/*
			for( i = 1 ; i <= l ; i++ )
			{
				tmp_node = levels[i];
				while( tmp_node->next )
				{
					printf("%c(%c) " , tmp_node->type+'a' , tmp_node->parent->type+'a' );
					tmp_node = tmp_node->next;
				}
				printf("\n");
			}
			*/

			tmp_node = levels[l];

			while( tmp_node->next )
			{
				tmp_type = tmp_node->type;
				if( tmp_type != '*' - 'a' )
				{
					tmp_node->accept = 0;
					for( i = 0 ; i < n ; i++ )
					{
						tmp_list = &signal_table[i][tmp_type];

						while( tmp_list )
						{
							if( tmp_list->l >= n-m && tmp_list->r >= n-m )
							{
								tmp_node->accept |= 1<<i;
								break;
							}

							tmp_list = tmp_list->next;
						}
					}

					if( tmp_node-> accept == 0 )
					{
						valid = 0;
						goto print;
					}
				}

				tmp_node = tmp_node->next;
			}

			for( j = l ; j > 0 ; j-- )
			{
				tmp_node = levels[j];

				while( tmp_node-> next )
				{

					tmp_parent = tmp_node->parent;
					tmp_type = tmp_parent->type;
					
					ac1 = tmp_node->accept;
					tmp_node = tmp_node->next;
					ac2 = tmp_node->accept;
					tmp_node = tmp_node->next;

					if( tmp_type == '*' - 'a' )
						continue;
					
					tmp_parent->accept = 0;
					PRINT("REACH 3: node=%c ac1=0x%x ac2=0x%x\n" , tmp_type+'a' , ac1 , ac2 );

					for( i = 0 ; i < n ; i++ )
					{
						tmp_list = &signal_table[i][tmp_type];

						while( tmp_list )
						{
							PRINT("REACH 3.5: l=%d r=%d\n" , tmp_list->l , tmp_list->r );
							if( ( ( ac1 & (1<<tmp_list->l) ) ) && 
							     (  ac2 & (1<<tmp_list->r) ) )
							{
								tmp_parent->accept |= 1<<i;
								PRINT("REACH 4: node=%c signal=%d\n" , tmp_type+'a' , i );
								break;
							}

							tmp_list = tmp_list->next;
						}

					}

					if( tmp_parent->accept == 0 )
					{
						valid = 0;
						goto print;
					}
				}
				PRINT("REACH 5: j=%d\n" , j);
			}


			if( levels[0]->accept & 1 )
				valid = 1;
			else
				valid = 0;

print:
			if( valid )
				printf("Valid\n");
			else
				printf("Invalid\n");
		}
			

		
	}

	return 0;
Exemplo n.º 2
0
void forward_request( int connfd, struct http_request *r_data, char *buffer ) {
  // Check if cached
  int cached = 0, total_return = 0;
  int fwd_sock;
  int sendbytes;
  int returnbytes;
  char response_buffer[4096];
  char cache_buffer[2 * g_max_size];
  //char *cache_buffer;
  struct http_cache *cursor = NULL;

  if ( g_cache_slots > 0 ) {
    pthread_mutex_lock(&cache_mutex);  
    cursor = cache_head;
    while( cursor != NULL && cached == 0 && (cursor->exists != -1) ) {
      if( strcmp( r_data->url, cursor->url) == 0 ) {
        cached = 1;
        if ( cursor == cache_head ) {
          break;
        } else if( cursor == cache_tail ) {
          cursor->prev->next = NULL;
          cache_tail = cursor->prev;
          cursor->next = cache_head;
          cursor->next->prev = cursor;
          cursor->prev = NULL;
          cache_head = cursor;
          break;
        } else {
          cursor->prev->next = cursor->next;
          cursor->next->prev = cursor->prev;
          cursor->prev = NULL;
          cursor->next = cache_head;
          cursor->next->prev = cursor;
          cache_head = cursor;
          break;
        }
      }
      cursor = cursor->next;
    }

    if ( cached ) {
      r_data->cached = 1;
      write( connfd, cursor->contents, g_max_size );
    }
    pthread_mutex_unlock(&cache_mutex);
  }
   
  if ( !cached ) {
    if( (fwd_sock = open_connection( r_data->host, r_data->port, 5 )) < 0 ) {
      #ifdef DEBUG
      fprintf(stderr, "Error code %d, error opening connection.\n", fwd_sock);
      #endif
    } else {
      #ifdef DEBUG
      fprintf(stderr, "Successfully opened socket %d.\n", fwd_sock);
      #endif
    }
  
    // Forward request to remote server
    sendbytes = strlen(buffer);      
    if( write(fwd_sock, buffer, sendbytes) != sendbytes ) {
      #ifdef DEBUG
      fprintf(stderr, "Error writing to forwarding socket.\n");
      #endif
    }

    // cache if return size < max_size TODO
    // return response to client TODO

    // Read response from remote server
    while( (returnbytes = read(fwd_sock, response_buffer, 1000)) > 0 ) {
      if( returnbytes < 0 ) {
        #ifdef DEBUG
        fprintf(stderr, "ERROR\tRead error.\n");
        #endif
      }

      #ifdef DEBUG
      fprintf(stderr, "Read %d bytes, forwarding to client.\n", returnbytes);
      #endif

      total_return += returnbytes;

      
      if( (g_cache_slots > 0) && (total_return < (g_max_size * 2)) ) {
        memcpy(cache_buffer + total_return - returnbytes, response_buffer, returnbytes);
      }

      // Write response back to requesting client
      if ( write(connfd, response_buffer, returnbytes) < 0 ) {
        #ifdef DEBUG
        fprintf(stderr, "Error writing response.\n");
        #endif
      }
    }

    if( (g_cache_slots > 0) && (total_return < 2 * g_max_size) ) {
      cache_buffer[total_return] = '\0';
      format_cache( cache_buffer, r_data, total_return );
    }
    if ( close( fwd_sock ) < 0 ) {
      #ifdef DEBUG
      fprintf(stderr, "ERROR Forwarding Socket Close Error\n");
      #endif
    }
  }
}