target.cc (1134B)
1 #include "external" 2 3 unsigned External::target(struct in_addr clientip, 4 BackendVector &targetlist) { 5 6 // Prepare command to run 7 ostringstream o; 8 o << config.externalalgorithm() << ' ' << balancer.nbackends(); 9 for (unsigned i = 0; i < balancer.nbackends(); i++) 10 o << ' ' << balancer.backend(i).description() << ' ' 11 << balancer.backend(i).availablestr() 12 << ' ' << balancer.backend(i).connections(); 13 msg("External algorithm: invoking '" << o.str() << '\n'); 14 15 FILE *f; 16 if (! (f = popen (o.str().c_str(), "r")) ) 17 throw Error("Cannot start '" + o.str() + ": " + strerror(errno)); 18 unsigned i; 19 if (fscanf (f, "%u", &i) < 1) { 20 pclose(f); 21 throw Error("External algorithm '" + o.str() + 22 "' did not reply with a number"); 23 } 24 msg("External algorithm says: " << i << '\n'); 25 26 if (i >= balancer.nbackends()) { 27 pclose(f); 28 ostringstream o; 29 o << "External algorithm '" << o.str() << "': answer " 30 << i << " out of bounds"; 31 throw Error(o.str()); 32 } 33 if (pclose (f)) 34 throw Error("External algorithm '" + o.str() + 35 "' terminated with error"); 36 37 return (i); 38 }