crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs | LICENSE

execute.cc (2491B)


      1 #include "tcpdispatcher"
      2 
      3 void TcpDispatcher::execute() {
      4     Threadlist::clientfd(clientfd());
      5     Threadlist::clientip(clientfd().clientaddr().sin_addr);
      6 
      7     if (!check_dos() ||
      8 	!check_acl())
      9 	return;
     10     
     11     debugmsg("Dispatch request for client fd " << clientfd().fd() << '\n');
     12 
     13     // Try to determine the back end.
     14     try {
     15 	Threadlist::desc("Dispatching");
     16 	dispatch();
     17     } catch (Error const &e) {
     18 	warnmsg(e.what() << " ("
     19 		<< inet2string(clientfd().clientaddr().sin_addr)
     20 		<< ")\n");
     21 	return;
     22     }
     23 
     24     // Verify that the target is within the allowed set.
     25     if (targetbackend() < 0 || targetbackend() >= (int)balancer.nbackends()) {
     26 	warnmsg("Target back end " << targetbackend() << "out of range\n");
     27 	return;
     28     }
     29 
     30     // Dispatch!
     31     msg("Dispatching client fd " << clientfd().fd() << " to " <<
     32 	balancer.backend(targetbackend()).description() << ", fd " <<
     33 	backendfd().fd() << '\n');
     34 
     35     Threadlist::desc("Serving");
     36     Threadlist::backend(targetbackend());
     37     Threadlist::backendfd(backendfd());
     38     
     39     balancer.backend(targetbackend()).startconnection();
     40     if (config.onstart().length()) {
     41 	ostringstream o;
     42 	o << config.onstart() << ' '
     43 	  << inet2string(clientfd().clientaddr().sin_addr)
     44 	  << balancer.backend(targetbackend()).description()
     45 	  << ' ' << balancer.backend(targetbackend()).connections();
     46 	msg("Running onstart script: " << o.str() << '\n');
     47 	sysrun(o.str());
     48     }
     49 
     50     bool failed = false;
     51     try {
     52 	handle();
     53     } catch (Error const &e) {
     54 	warnmsg(e.what() << " ("
     55 		<< inet2string(clientfd().clientaddr().sin_addr)
     56 		<< ")\n");
     57 	failed = true;
     58 	if (config.onfail().length()) {
     59 	    ostringstream o;
     60 	    o << config.onfail() << ' '
     61 	      << inet2string(clientfd().clientaddr().sin_addr)
     62 	      << balancer.backend(targetbackend()).description() << ' '
     63 	      << balancer.backend(targetbackend()).connections();
     64 	    msg("Running onfail script: " << o.str() << '\n');
     65 	    sysrun(o.str());
     66 	}
     67     }
     68 
     69     balancer.backend(targetbackend()).endconnection();
     70     if (!failed && config.onend().length()) {
     71 	ostringstream o;
     72 	o << config.onend() << ' '
     73 	  << inet2string(clientfd().clientaddr().sin_addr)
     74 	  << balancer.backend(targetbackend()).description() << ' '
     75 	  << balancer.backend(targetbackend()).connections();
     76 	msg("Running onend script: " << o.str() << '\n');
     77 	sysrun(o.str());
     78     }
     79 
     80     msg ("Done dispatching to back end fd " << backendfd().fd() << " at " <<
     81 	 balancer.backend(targetbackend()).description() << '\n');
     82 }