예제 #1
0
ll responder( int i, int j ){
	if( next[i][j] )
		return memo[i][j];
			
	if( j == col ){
		next[i][j] = -1;
		return memo[i][j] = matriz[i][j];
	}
	
	ll frente, nord, sud, menor;
	
	menor = frente = responder( i, j + 1 );
	next[i][j] = i;
	
	int indice = (i == 1)? lin : (i - 1);
	nord = responder( indice, j + 1 );
	if( nord < menor ){
		menor = nord;
		next[i][j] = indice;
	}else if( nord == menor )
		next[i][j] = MENOR( next[i][j], indice );
				
	indice = (i == lin)? 1 : ( i + 1 );
	sud = responder( indice, j + 1 );
	if( sud < menor ){
		menor = sud;
		next[i][j] = indice;
	}else if( sud == menor )
		next[i][j] = MENOR( next[i][j], indice );

	return memo[i][j] = matriz[i][j] + menor;
		
}
예제 #2
0
int main(){
	ll melhor, temp;
	
	while( scanf( "%d %d", &lin, &col ) != EOF ){
		for( int i = 1; i <= lin; ++i ){
			for( int j = 1; j <= col; ++j ){
				scanf( "%d", &matriz[i][j] );
				next[i][j] = 0;
			}
		}
		
		melhor = responder( 1, 1 );
		preencher( 1 );
		
		for( int i = 2; i <= lin; ++i ){
			temp = responder( i, 1 );
			if( temp < melhor ){
				melhor = temp;
				preencher( i );
			}
		}
	
		--col;
		for( int i = 0; i < col; ++i )
			printf( "%d ", seq[i] );
		printf( "%d\n", seq[col] );
		printf( "%d\n", melhor );
		
	}
}
int main(int argc, char **argv) {
  std::string key_dir, test_file_location, url;
  if (argc > 1) {
    test_file_location = argv[1];
    key_dir = argv[2];
    url = argv[3];
  }

  bool isSecure = false;
  if (url.find("https") != std::string::npos) {
    isSecure = true;
  }

  SiteToSiteTestHarness harness(isSecure);

  Responder responder(isSecure);

  harness.setKeyDir(key_dir);

  harness.setUrl(url, &responder);

  harness.run(test_file_location);

  return 0;
}
예제 #4
0
void Talker::start_thread() {
    SBL_INFO("RTSP talker thread %d listening to %s:%d", id(), _client_ip, _client_port);
    Parser      parser;
    Responder   responder(this, _tx_buffer, BUFFER_SIZE);
    Method      method = OPTIONS;
    do {
        try {
            MsgType msg_type = receive_msg();
            if (msg_type == MSG_RTSP) {
                SBL_MSG(MSG::SERVER, "RTSP Server thread %d received message length %d:\n%s", 
                         id(), _msg_size, _rx_buffer);
                method = parser.parse(_rx_buffer, _msg_size);
                int reply_size = responder.reply(parser.data);
                SBL_MSG(MSG::SERVER, "RTSP Server thread %d reply:\n%s", id(), _tx_buffer);
                if (!_socket.send(_tx_buffer, reply_size, false)) {
                    SBL_MSG(MSG::SERVER, "RTSP Server thread %d, socket error, exiting thread ...\n", id());
                    break;
                }
                if (method == PLAY) {
                    RTSP_ASSERT(client(), BAD_REQUEST);
                    client()->play();
                }
            } else if (msg_type == MSG_RTCP) {
                SBL_MSG(MSG::SERVER, "RTSP Server thread %d received RTCP message length %d", 
                         id(), _msg_size);
                if (_rtcp_parser) {
                    RTSP_ASSERT(_msg_size >= 4, BAD_REQUEST);
                    _rtcp_parser->parse(_rx_buffer + 4, _msg_size - 4);
                }
                else
                    SBL_WARN("Thread %d, received RTCP message, but RTCP parser is not yet ready", id());
            } else
                method = TEARDOWN;
            _rx_bytes -= _msg_size;
            if (_rx_bytes > 0)
                memmove(_rx_buffer, _rx_buffer + _msg_size, _rx_bytes);
        } catch (Errcode errcode) {
            // If we catch Errcode, it may be possible to continue, so reply with error code
            SBL_MSG(MSG::SERVER, "RTSP talker %d caught error code %d", id(), errcode);
            int reply_size = responder.reply(parser.data, errcode);
            SBL_MSG(MSG::SERVER, "RTSP talker thread %d reply:\n%s\n", id(), _tx_buffer);
            if (!_socket.send(_tx_buffer, reply_size, false)) {
                SBL_MSG(MSG::SERVER, "Talker %d unable to send reply, exiting...", id());
                method = TEARDOWN;
            }
            // We throw out any data already received if we had error
            _rx_bytes = _msg_size = 0;
        } catch (SBL::Exception& ex) {
            // if we catch Exception, it is coming from Socket, so can't send anything back    
            // log the error in the log file and terminate the thread;
            SBL_MSG(MSG::SERVER, "RTSP talker %d exiting, caught exception %s", id(), ex.what());
            method = TEARDOWN;
        }
    } while (method != TEARDOWN);
    teardown();
    SBL_INFO("RTSP talker %d terminating", id());
    delete this;
}
예제 #5
0
파일: abp.c 프로젝트: D4ryus/studium
int main()
{
    struct sigaction actionsender    = {0};
    struct sigaction actionresponder = {0};

    struct sigaction actionalarm = {0};

    actionalarm.sa_handler = abp_alarm;
    sigaction(SIGALRM, &actionalarm, 0);

    if(pipe(first_pipe) == -1 || pipe(second_pipe) == -1)
        error("Can't open pipe");

    pid_t pid = fork();

    if((pid < 0))
        error("unexpected fork error:");
    else if (pid == 0)
    {
        /* child/responder process */
        close(first_pipe[0]);       /* close pipes from parent prozess */
        close(second_pipe[1]);

        actionresponder.sa_handler = abp_responder;
        sigaction(SIGUSR1, &actionresponder, 0);

        responder();

        sleep(2);
        close(first_pipe[1]);
        close(second_pipe[0]);
        exit(0);
    }
    else
    {
        /* parrent/sender processs */
        close(first_pipe[1]);       /* close pipes from child prozess */
        close(second_pipe[0]);

        actionsender.sa_handler       = abp_sender;
        sigaction(SIGUSR2, &actionsender,       0);

        sleep(1);
        sender(pid);

        sleep(2);
        close(first_pipe[0]);
        close(second_pipe[1]);
        exit(0);
    }
    int pid_status;
    if(waitpid(pid, &pid_status, 0) == -1)
        error("Error waiting for child process");
}
예제 #6
0
        static void dethunker(
            const char* name, int name_len, const char* instruction,
            int instruction_len, int num_prompts, 
            const LIBSSH2_USERAUTH_KBDINT_PROMPT* raw_prompts,
            LIBSSH2_USERAUTH_KBDINT_RESPONSE* raw_responses, void **abstract)
        throw()
        {
            challenge_response_translator<ChallengeResponder>& responder =
                *static_cast<challenge_response_translator<ChallengeResponder>*>(*abstract);

            responder(
                name, name_len, instruction, instruction_len, num_prompts,
                raw_prompts, raw_responses);
        }
int main(){
  ZmqStream tcp;
  
  zmq::context_t context(1);

  zmq::socket_t responder(context, ZMQ_REP);
  responder.bind("tcp://*:5559");

  tcp.setSocket(&responder);
    
  /*request.setStream(&tcp);
  
  request.initialize();
  
  int id = request.getInstanceId();
  char * key =  request.getKey();
  printf("Key: (%s) <br>\n", key);
  printf("Instance id: (%d) <br>\n", id);
  printf("...\n");
  */
  
  while(1){
    while(tcp.available()){
      char c = 0;

      while((c = tcp.read()) > -1){
        printf("%c",c);
      }
      
    sleep(1);

    }
    printf("\ntcp not available\n");

    
  }
      sleep(1);

}
void VersionResponderTest_V1::testHandleIncomingRequest() {
	// Set up the test fixture
	MockDataChannel dataChannel;
	VersionResponder_V1 responder(&dataChannel);
	responder.setVersion("Swift", "1.0");

	// Fake incoming request data on the data channel
	dataChannel.onDataReceived(
		"<iq type='get' from='[email protected]/RabbitHole' id='version-1'>"
			"<query xmlns='jabber:iq:version'/>"
		"</iq>");

	// Verify the outcome
	ASSERT_EQUAL(
		"<iq type='result' to='[email protected]/RabbitHole' id='version-1'>"
			"<query xmlns='jabber:iq:version'>"
				"<name>Swift</name>"
				"<version>1.0</version>"
			"</query>"
		"</iq>", 
		dataChannel.sentData);
}
예제 #9
0
파일: sock.c 프로젝트: lianqiw/maos
/**
   Open a port and listen to it. Calls respond(sock) to handle data. If
   timeout_fun is not NULL, it will be called when 1) connection is
   establed/handled, 2) every timeout_sec if timeout_sec>0. This function only
   return at error.
   if localpath is not null and start with /, open the local unix port also
   if localpath is not null and contains ip address, only bind to that ip address
 */
void listen_port(uint16_t port, char *localpath, int (*responder)(int),
		 double timeout_sec, void (*timeout_fun)(), int nodelay){
    register_signal_handler(scheduler_signal_handler);

    fd_set read_fd_set;
    fd_set active_fd_set;
    FD_ZERO (&active_fd_set);
    char *ip=0;
    int sock_local=-1;
    if(localpath){
	if(localpath[0]=='/'){
	    //also bind to AF_UNIX
	    sock_local = bind_socket_local(localpath);
	    if(sock_local==-1){
		dbg("bind to %s failed\n", localpath);
	    }else{
		if(!listen(sock_local, 1)){
		    FD_SET(sock_local, &active_fd_set);
		}else{
		    perror("listen");
		    close(sock_local);
		sock_local=-1;
		}
	    }
	}else{
	    ip=localpath;
	}
    }

    int sock = bind_socket (ip, port);
    if(nodelay){//turn off tcp caching.
	socket_tcp_nodelay(sock);
    }
    if (listen (sock, 1) < 0){
	perror("listen");
	exit(EXIT_FAILURE);
    }

    FD_SET (sock, &active_fd_set);
 
    while(quit_listen!=2){
	if(quit_listen==1){
	    /*
	      shutdown(sock, SHUT_WR) sends a FIN to the peer, therefore
	      initialize a active close and the port will remain in TIME_WAIT state.

	      shutdown(sock, SHUT_RD) sends nother, just mark the scoket as not readable.

	      accept() create a new socket on the existing port. A socket is identified by client ip/port and server ip/port.
	      
	      It is the port that remain in TIME_WAIT state.
	     */
	    //shutdown(sock, SHUT_RDWR);
	    //shutdown(sock_local, SHUT_RDWR);
	    /*Notice existing client to shutdown*/
	    for(int i=0; i<FD_SETSIZE; i++){
		if(FD_ISSET(i, &active_fd_set) && i!=sock && i!=sock_local){
		    int cmd[3]={-1, 0, 0};
		    stwrite(i, cmd, 3*sizeof(int)); //We ask the client to actively close the connection
		    //shutdown(i, SHUT_WR);
		}
	    }
	    usleep(1e5);
	    quit_listen=2;
	    //don't break. Listen for connection close events.
	}
	if(timeout_fun){
	    timeout_fun();
	}

	struct timeval timeout;
	timeout.tv_sec=timeout_sec;
	timeout.tv_usec=(timeout_sec-timeout.tv_sec)*1e6;
	read_fd_set = active_fd_set;
	if(select(FD_SETSIZE, &read_fd_set, NULL, NULL, timeout_sec>0?&timeout:0)<0){
	    if(errno==EINTR){
		warning("select failed: %s\n", strerror(errno));
		continue;
	    }else if(errno==EBADF){
		warning("bad file descriptor: %s\n", strerror(errno));
		break;//bad file descriptor
	    }else{
		warning("unknown error: %s\n", strerror(errno));
		break;
	    }
	}
	for(int i=0; i<FD_SETSIZE; i++){
	    if(FD_ISSET(i, &read_fd_set)){
		if(i==sock){
		    /* Connection request on original socket. */
		    socklen_t size=sizeof(struct sockaddr_in);
		    struct sockaddr_in clientname;
    		    int port2=accept(i, (struct sockaddr*)&clientname, &size);
		    if(port2<0){
			warning("accept failed: %s. close port %d\n", strerror(errno), i);
			FD_CLR(i, &active_fd_set);
			close(i);
		    }else{
			info("port %d is connected\n", port2);
			FD_SET(port2, &active_fd_set);
		    }
		}else if(i==sock_local){
		    socklen_t size=sizeof(struct sockaddr_un);
		    struct sockaddr_un clientname;
		    int port2=accept(i, (struct sockaddr*)&clientname, &size);
		    if(port2<0){
			warning("accept failed: %s. close port %d\n", strerror(errno), i);
			FD_CLR(i, &active_fd_set);
			close(i);
		    }else{
			info("port %d is connected locally\n", port2);
			FD_SET(port2, &active_fd_set);
		    }
		}else{
		    /* Data arriving on an already-connected socket. Call responder to handle.
		       On return:
		       negative value: Close read of socket. 
		       -1: also close the socket.
		     */
		    int ans=responder(i);
		    if(ans<0){
			FD_CLR(i, &active_fd_set);
			if(ans==-1){
			    warning("close port %d\n", i);
			    close(i);
			}else{
			    warning("ans=%d is not understood.\n", ans);
			}
		    }
		}
	    }
	}
    }
    /* Error happened. We close all connections and this server socket.*/
    close(sock);
    close(sock_local);
    FD_CLR(sock, &active_fd_set);
    FD_CLR(sock_local, &active_fd_set);
    warning("listen_port exited\n");
    for(int i=0; i<FD_SETSIZE; i++){
	if(FD_ISSET(i, &active_fd_set)){
	    warning("sock %d is still connected\n", i);
	    close(i);
	    FD_CLR(i, &active_fd_set);
	}
    }
    usleep(100);
    sync();
}