void ThreadSendAlert(CConnman& connman) { if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert")) return; // Wait one minute so we get well connected. If we only need to print // but not to broadcast - do this right away. if (mapArgs.count("-sendalert")) MilliSleep(60*1000); // // Alerts are relayed around the network until nRelayUntil, flood // filling to every node. // After the relay time is past, new nodes are told about alerts // when they connect to peers, until either nExpiration or // the alert is cancelled by a newer alert. // Nodes never save alerts to disk, they are in-memory-only. // CAlert alert; alert.nRelayUntil = GetAdjustedTime() + 15 * 60; alert.nExpiration = GetAdjustedTime() + 30 * 60 * 60; alert.nID = 1; // keep track of alert IDs somewhere alert.nCancel = 0; // cancels previous messages up to this ID number // These versions are protocol versions alert.nMinVer = 70000; alert.nMaxVer = 70103; // // 1000 for Misc warnings like out of disk space and clock is wrong // 2000 for longer invalid proof-of-work chain // Higher numbers mean higher priority alert.nPriority = 5000; alert.strComment = ""; alert.strStatusBar = "URGENT: Upgrade required: see https://www.terracoin.io"; // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done: // alert.setSubVer.insert(std::string("/Terracoin Core:0.12.0.58/")); // Sign if(!alert.Sign()) { LogPrintf("ThreadSendAlert() : could not sign alert\n"); return; } // Test CDataStream sBuffer(SER_NETWORK, CLIENT_VERSION); sBuffer << alert; CAlert alert2; sBuffer >> alert2; if (!alert2.CheckSignature(Params().AlertKey())) { printf("ThreadSendAlert() : CheckSignature failed\n"); return; } assert(alert2.vchMsg == alert.vchMsg); assert(alert2.vchSig == alert.vchSig); alert.SetNull(); printf("\nThreadSendAlert:\n"); printf("hash=%s\n", alert2.GetHash().ToString().c_str()); printf("%s", alert2.ToString().c_str()); printf("vchMsg=%s\n", HexStr(alert2.vchMsg).c_str()); printf("vchSig=%s\n", HexStr(alert2.vchSig).c_str()); // Confirm if (!mapArgs.count("-sendalert")) return; while (connman.GetNodeCount(CConnman::CONNECTIONS_ALL) == 0 && !ShutdownRequested()) MilliSleep(500); if (ShutdownRequested()) return; // Send printf("ThreadSendAlert() : Sending alert\n"); int nSent = 0; { connman.ForEachNode([&alert2, &connman, &nSent](CNode* pnode) { if (alert2.RelayTo(pnode, connman)) { printf("ThreadSendAlert() : Sent alert to %s\n", pnode->addr.ToString().c_str()); nSent++; } }); } printf("ThreadSendAlert() : Alert sent to %d nodes\n", nSent); }
bool CDB::Rewrite(const std::string &strFile, const char *pszSkip) { while (true) { { LOCK(bitdb.cs_db); if (!bitdb.mapFileUseCount.count(strFile) || bitdb.mapFileUseCount[strFile] == 0) { // Flush log data to the dat file bitdb.CloseDb(strFile); bitdb.CheckpointLSN(strFile); bitdb.mapFileUseCount.erase(strFile); bool fSuccess = true; LogPrintf("CDB::Rewrite: Rewriting %s...\n", strFile); std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} CDB db(strFile.c_str(), "r"); Db *pdbCopy = new Db(bitdb.dbenv, 0); int ret = pdbCopy->open(NULL, // Txn pointer strFileRes.c_str(), // Filename "main", // Logical db name DB_BTREE, // Database type DB_CREATE, // Flags 0); if (ret > 0) { LogPrintf("CDB::Rewrite: Can't create database file %s\n", strFileRes); pdbCopy->close(0); fSuccess = false; } Dbc *pcursor = db.GetCursor(); if (pcursor) { while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret1 = db.ReadAtCursor(pcursor, ssKey, ssValue, DB_NEXT); if (ret1 == DB_NOTFOUND) { pcursor->close(); break; } else if (ret1 != 0) { pcursor->close(); fSuccess = false; break; } if (pszSkip && strncmp(&ssKey[0], pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) continue; if (strncmp(&ssKey[0], "\x07version", 8) == 0) { // Update version: ssValue.clear(); ssValue << CLIENT_VERSION; } Dbt datKey(&ssKey[0], ssKey.size()); Dbt datValue(&ssValue[0], ssValue.size()); int ret2 = pdbCopy->put(NULL, &datKey, &datValue, DB_NOOVERWRITE); if (ret2 > 0) fSuccess = false; } } if (fSuccess) { db.Close(); bitdb.CloseDb(strFile); if (pdbCopy->close(0)) { fSuccess = false; } else { pdbCopy->close(0); } delete pdbCopy; } } if (fSuccess) { Db dbA(bitdb.dbenv, 0); if (dbA.remove(strFile.c_str(), NULL, 0)) fSuccess = false; Db dbB(bitdb.dbenv, 0); if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0)) fSuccess = false; } if (!fSuccess) LogPrintf("CDB::Rewrite: Failed to rewrite database file %s\n", strFileRes); return fSuccess; } } MilliSleep(100); } return false; }
bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip) { if (database.IsDummy()) { return true; } BerkeleyEnvironment *env = database.env; const std::string& strFile = database.strFile; while (true) { { LOCK(cs_db); if (!env->mapFileUseCount.count(strFile) || env->mapFileUseCount[strFile] == 0) { // Flush log data to the dat file env->CloseDb(strFile); env->CheckpointLSN(strFile); env->mapFileUseCount.erase(strFile); bool fSuccess = true; LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile); std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} BerkeleyBatch db(database, "r"); std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename "main", // Logical db name DB_BTREE, // Database type DB_CREATE, // Flags 0); if (ret > 0) { LogPrintf("BerkeleyBatch::Rewrite: Can't create database file %s\n", strFileRes); fSuccess = false; } Dbc* pcursor = db.GetCursor(); if (pcursor) while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret1 = db.ReadAtCursor(pcursor, ssKey, ssValue); if (ret1 == DB_NOTFOUND) { pcursor->close(); break; } else if (ret1 != 0) { pcursor->close(); fSuccess = false; break; } if (pszSkip && strncmp(ssKey.data(), pszSkip, std::min(ssKey.size(), strlen(pszSkip))) == 0) continue; if (strncmp(ssKey.data(), "\x07version", 8) == 0) { // Update version: ssValue.clear(); ssValue << CLIENT_VERSION; } Dbt datKey(ssKey.data(), ssKey.size()); Dbt datValue(ssValue.data(), ssValue.size()); int ret2 = pdbCopy->put(nullptr, &datKey, &datValue, DB_NOOVERWRITE); if (ret2 > 0) fSuccess = false; } if (fSuccess) { db.Close(); env->CloseDb(strFile); if (pdbCopy->close(0)) fSuccess = false; } else { pdbCopy->close(0); } } if (fSuccess) { Db dbA(env->dbenv.get(), 0); if (dbA.remove(strFile.c_str(), nullptr, 0)) fSuccess = false; Db dbB(env->dbenv.get(), 0); if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0)) fSuccess = false; } if (!fSuccess) LogPrintf("BerkeleyBatch::Rewrite: Failed to rewrite database file %s\n", strFileRes); return fSuccess; } } MilliSleep(100); } }
void Sleep::SleepFloat(float seconds) { return MilliSleep(static_cast<int64_t>(seconds * 1000.0f)); }
int CommandLineRPC(int argc, char *argv[]) { std::string strPrint; int nRet = 0; try { // Skip switches while (argc > 1 && IsSwitchChar(argv[1][0])) { argc--; argv++; } std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]); if (GetBoolArg("-stdin", false)) { // Read one arg per line from stdin and append std::string line; while (std::getline(std::cin,line)) args.push_back(line); } if (args.size() < 1) throw std::runtime_error("too few parameters (need at least command)"); std::string strMethod = args[0]; args.erase(args.begin()); // Remove trailing method name from arguments vector UniValue params; if(GetBoolArg("-named", DEFAULT_NAMED)) { params = RPCConvertNamedValues(strMethod, args); } else { params = RPCConvertValues(strMethod, args); } // Execute and handle connection failures with -rpcwait const bool fWait = GetBoolArg("-rpcwait", false); do { try { const UniValue reply = CallRPC(strMethod, params); // Parse reply const UniValue& result = find_value(reply, "result"); const UniValue& error = find_value(reply, "error"); if (!error.isNull()) { // Error int code = error["code"].get_int(); if (fWait && code == RPC_IN_WARMUP) throw CConnectionFailed("server in warmup"); strPrint = "error: " + error.write(); nRet = abs(code); if (error.isObject()) { UniValue errCode = find_value(error, "code"); UniValue errMsg = find_value(error, "message"); strPrint = errCode.isNull() ? "" : "error code: "+errCode.getValStr()+"\n"; if (errMsg.isStr()) strPrint += "error message:\n"+errMsg.get_str(); } } else { // Result if (result.isNull()) strPrint = ""; else if (result.isStr()) strPrint = result.get_str(); else strPrint = result.write(2); } // Connection succeeded, no need to retry. break; } catch (const CConnectionFailed&) { if (fWait) MilliSleep(1000); else throw; } } while (fWait); } catch (const boost::thread_interrupted&) { throw; } catch (const std::exception& e) { strPrint = std::string("error: ") + e.what(); nRet = EXIT_FAILURE; } catch (...) { PrintExceptionContinue(NULL, "CommandLineRPC()"); throw; } if (strPrint != "") { fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str()); } return nRet; }
static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &) { // JSONRPC handles only POST if (req->GetRequestMethod() != HTTPRequest::POST) { req->WriteReply(HTTP_BAD_METHOD, "JSONRPC server handles only POST requests"); return false; } // Check authorization std::pair<bool, std::string> authHeader = req->GetHeader("authorization"); if (!authHeader.first) { req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA); req->WriteReply(HTTP_UNAUTHORIZED); return false; } JSONRPCRequest jreq; jreq.peerAddr = req->GetPeer().ToString(); if (!RPCAuthorized(authHeader.second, jreq.authUser)) { LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr); /* Deter brute-forcing If this results in a DoS the user really shouldn't have their RPC port exposed. */ MilliSleep(250); req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA); req->WriteReply(HTTP_UNAUTHORIZED); return false; } try { // Parse request UniValue valRequest; if (!valRequest.read(req->ReadBody())) throw JSONRPCError(RPC_PARSE_ERROR, "Parse error"); // Set the URI jreq.URI = req->GetURI(); std::string strReply; // singleton request if (valRequest.isObject()) { jreq.parse(valRequest); UniValue result = tableRPC.execute(jreq); // Send reply strReply = JSONRPCReply(result, NullUniValue, jreq.id); // array of requests } else if (valRequest.isArray()) strReply = JSONRPCExecBatch(jreq, valRequest.get_array()); else throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error"); req->WriteHeader("Content-Type", "application/json"); req->WriteReply(HTTP_OK, strReply); } catch (const UniValue& objError) { JSONErrorReply(req, objError, jreq.id); return false; } catch (const std::exception& e) { JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id); return false; } return true; }
json_spirit::Object CallRPC(const std::string& strMethod, const json_spirit::Array& params) { if (mapArgs["-rpcuser"] == "" && mapArgs["-rpcpassword"] == "") throw std::runtime_error(strprintf( _("You must set rpcpassword=<password> in the configuration file:\n%s\n" "If the file does not exist, create it with owner-readable-only file permissions."), GetConfigFile().string().c_str())); // Connect to localhost bool fUseSSL = GetBoolArg("-rpcssl", false); boost::asio::io_service io_service; boost::asio::ssl::context context(io_service, boost::asio::ssl::context::sslv23); context.set_options(boost::asio::ssl::context::no_sslv2); boost::asio::ssl::stream<boost::asio::ip::tcp::socket> sslStream(io_service, context); SSLIOStreamDevice<boost::asio::ip::tcp> d(sslStream, fUseSSL); boost::iostreams::stream< SSLIOStreamDevice<boost::asio::ip::tcp> > stream(d); bool fWait = GetBoolArg("-rpcwait", false); // -rpcwait means try until server has started do { bool fConnected = d.connect(GetArg("-rpcconnect", "127.0.0.1"), GetArg("-rpcport", itostr(Params().RPCPort()))); if (fConnected) break; if (fWait) MilliSleep(1000); else throw std::runtime_error("couldn't connect to server"); } while (fWait); // HTTP basic authentication std::string strUserPass64 = EncodeBase64(mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"]); std::map<std::string, std::string> mapRequestHeaders; mapRequestHeaders["Authorization"] = std::string("Basic ") + strUserPass64; // Send request std::string strRequest = JSONRPCRequest(strMethod, params, 1); std::string strPost = HTTPPost(strRequest, mapRequestHeaders); stream << strPost << std::flush; // Receive HTTP reply status int nProto = 0; int nStatus = ReadHTTPStatus(stream, nProto); // Receive HTTP reply message headers and body std::map<std::string, std::string> mapHeaders; std::string strReply; ReadHTTPMessage(stream, mapHeaders, strReply, nProto); if (nStatus == HTTP_UNAUTHORIZED) throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); else if (nStatus >= 400 && nStatus != HTTP_BAD_REQUEST && nStatus != HTTP_NOT_FOUND && nStatus != HTTP_INTERNAL_SERVER_ERROR) throw std::runtime_error(strprintf("server returned HTTP error %d", nStatus)); else if (strReply.empty()) throw std::runtime_error("no response from server"); // Parse reply json_spirit::Value valReply; if (!read_string(strReply, valReply)) throw std::runtime_error("couldn't parse reply from server"); const json_spirit::Object& reply = valReply.get_obj(); if (reply.empty()) throw std::runtime_error("expected reply to have result, error and id properties"); return reply; }
void ThreadSendAlert() { if (!mapArgs.count("-sendalert") && !mapArgs.count("-printalert")) return; MilliSleep(60*1000); // Wait a minute so we get connected // // Alerts are relayed around the network until nRelayUntil, flood // filling to every node. // After the relay time is past, new nodes are told about alerts // when they connect to peers, until either nExpiration or // the alert is cancelled by a newer alert. // Nodes never save alerts to disk, they are in-memory-only. // CAlert alert; alert.nRelayUntil = GetTime() + 15 * 60; alert.nExpiration = GetTime() + 365 * 60 * 60; alert.nID = 1000; // use https://github.com/zcash/zcash/wiki/specification#assigned-numbers to keep track of alert IDs alert.nCancel = 0; // cancels previous messages up to this ID number // These versions are protocol versions // 170002 : 1.0.0 alert.nMinVer = 170002; alert.nMaxVer = 170002; // // main.cpp: // 1000 for Misc warnings like out of disk space and clock is wrong // 2000 for longer invalid proof-of-work chain // Higher numbers mean higher priority // 4000 or higher will put the RPC into safe mode alert.nPriority = 5000; alert.strComment = ""; alert.strStatusBar = "URGENT: Upgrade required: see https://z.cash"; alert.strRPCError = "URGENT: Upgrade required: see https://z.cash"; // Set specific client version/versions here. If setSubVer is empty, no filtering on subver is done: // alert.setSubVer.insert(std::string("/MagicBean:0.7.2/")); // Sign const CChainParams& chainparams = Params(); std::string networkID = chainparams.NetworkIDString(); bool fIsTestNet = networkID.compare("test") == 0; std::vector<unsigned char> vchTmp(ParseHex(fIsTestNet ? pszTestNetPrivKey : pszPrivKey)); CPrivKey vchPrivKey(vchTmp.begin(), vchTmp.end()); CDataStream sMsg(SER_NETWORK, CLIENT_VERSION); sMsg << *(CUnsignedAlert*)&alert; alert.vchMsg = std::vector<unsigned char>(sMsg.begin(), sMsg.end()); CKey key; if (!key.SetPrivKey(vchPrivKey, false)) { printf("ThreadSendAlert() : key.SetPrivKey failed\n"); return; } if (!key.Sign(Hash(alert.vchMsg.begin(), alert.vchMsg.end()), alert.vchSig)) { printf("ThreadSendAlert() : key.Sign failed\n"); return; } // Test CDataStream sBuffer(SER_NETWORK, CLIENT_VERSION); sBuffer << alert; CAlert alert2; sBuffer >> alert2; if (!alert2.CheckSignature(chainparams.AlertKey())) { printf("ThreadSendAlert() : CheckSignature failed\n"); return; } assert(alert2.vchMsg == alert.vchMsg); assert(alert2.vchSig == alert.vchSig); alert.SetNull(); printf("\nThreadSendAlert:\n"); printf("hash=%s\n", alert2.GetHash().ToString().c_str()); printf("%s\n", alert2.ToString().c_str()); printf("vchMsg=%s\n", HexStr(alert2.vchMsg).c_str()); printf("vchSig=%s\n", HexStr(alert2.vchSig).c_str()); // Confirm if (!mapArgs.count("-sendalert")) return; while (vNodes.size() < 1 && !ShutdownRequested()) MilliSleep(500); if (ShutdownRequested()) return; // Send printf("ThreadSendAlert() : Sending alert\n"); int nSent = 0; { LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) { if (alert2.RelayTo(pnode)) { printf("ThreadSendAlert() : Sent alert to %s\n", pnode->addr.ToString().c_str()); nSent++; } } } printf("ThreadSendAlert() : Alert sent to %d nodes\n", nSent); }
int main(void) { uint16_t U_Bat; /* voltage of power supply */ uint8_t Test; /* test value */ /* * init */ /* switch on power to keep me alive */ CONTROL_DDR = (1 << POWER_CTRL); /* set pin as output */ CONTROL_PORT = (1 << POWER_CTRL); /* set pin to drive power management transistor */ /* setup MCU */ MCUCR = (1 << PUD); /* disable pull-up resistors globally */ ADCSRA = (1 << ADEN) | ADC_CLOCK_DIV; /* enable ADC and set clock divider */ #ifdef HW_RELAY /* init relay (safe mode) */ /* ADC_PORT should be 0 */ ADC_DDR = (1 << TP_REF); /* short circuit probes */ #endif /* catch watchdog */ Test = (MCUSR & (1 << WDRF)); /* save watchdog flag */ MCUSR &= ~(1 << WDRF); /* reset watchdog flag */ wdt_disable(); /* disable watchdog */ /* init LCD module */ LCD_BusSetup(); /* setup bus */ LCD_Init(); /* initialize LCD */ LCD_NextLine_Mode(MODE_NONE); /* reset line mode */ /* * watchdog was triggered (timeout 2s) * - This is after the MCU done a reset driven by the watchdog. * - Does only work if the capacitor at the base of the power management * transistor is large enough to survive a MCU reset. Otherwise the * tester simply loses power. */ if (Test) { LCD_Clear(); /* display was initialized before */ LCD_EEString(Timeout_str); /* display: timeout */ LCD_NextLine_EEString(Error_str); /* display: error */ MilliSleep(2000); /* give user some time to read */ CONTROL_PORT = 0; /* power off myself */ return 0; /* exit program */ } /* * operation mode selection */ Config.SleepMode = SLEEP_MODE_PWR_SAVE; /* default: power save */ UI.TesterMode = MODE_CONTINOUS; /* set default mode: continous */ Test = 0; /* key press */ /* catch long key press */ if (!(CONTROL_PIN & (1 << TEST_BUTTON))) /* if test button is pressed */ { RunsMissed = 0; while (Test == 0) { MilliSleep(20); if (!(CONTROL_PIN & (1 << TEST_BUTTON))) /* if button is still pressed */ { RunsMissed++; if (RunsMissed > 100) Test = 3; /* >2000ms */ } else /* button released */ { Test = 1; /* <300ms */ if (RunsMissed > 15) Test = 2; /* >300ms */ } } } /* key press >300ms sets autohold mode */ if (Test > 1) UI.TesterMode = MODE_AUTOHOLD; /* * load saved offsets and values */ if (Test == 3) /* key press >2s resets to defaults */ { SetAdjustDefaults(); /* set default values */ } else /* normal mode */ { LoadAdjust(); /* load adjustment values */ } /* set extra stuff */ #ifdef SW_CONTRAST LCD_Contrast(NV.Contrast); /* set LCD contrast */ #endif /* * welcome user */ LCD_EEString(Tester_str); /* display: Component Tester */ LCD_NextLine_EEString_Space(Edition_str); /* display firmware edition */ LCD_EEString(Version_str); /* display firmware version */ MilliSleep(1500); /* let the user read the display */ /* * init variables */ /* cycling */ RunsMissed = 0; RunsPassed = 0; /* default offsets and values */ Config.Samples = ADC_SAMPLES; /* number of ADC samples */ Config.AutoScale = 1; /* enable ADC auto scaling */ Config.RefFlag = 1; /* no ADC reference set yet */ Config.Vcc = UREF_VCC; /* voltage of Vcc */ wdt_enable(WDTO_2S); /* enable watchdog (timeout 2s) */ /* * main processing cycle */ start: /* reset variabels */ Check.Found = COMP_NONE; Check.Type = 0; Check.Done = 0; Check.Diodes = 0; Check.Resistors = 0; Semi.U_1 = 0; Semi.I_1 = 0; Semi.F_1 = 0; /* reset hardware */ ADC_DDR = 0; /* set all pins of ADC port as input */ /* also remove short circuit by relay */ LCD_NextLine_Mode(MODE_KEEP); /* line mode: keep first line */ LCD_Clear(); /* clear LCD */ /* * voltage reference */ #ifdef HW_REF25 /* external 2.5V reference */ Config.Samples = 200; /* do a lot of samples for high accuracy */ U_Bat = ReadU(TP_REF); /* read voltage of reference (mV) */ Config.Samples = ADC_SAMPLES; /* set samples back to default */ if ((U_Bat > 2250) && (U_Bat < 2750)) /* check for valid reference */ { uint32_t Temp; /* adjust Vcc (assuming 2.495V typically) */ Temp = ((uint32_t)Config.Vcc * UREF_25) / U_Bat; Config.Vcc = (uint16_t)Temp; } #endif /* internal bandgap reference */ Config.Bandgap = ReadU(0x0e); /* dummy read for bandgap stabilization */ Config.Samples = 200; /* do a lot of samples for high accuracy */ Config.Bandgap = ReadU(0x0e); /* get voltage of bandgap reference (mV) */ Config.Samples = ADC_SAMPLES; /* set samples back to default */ Config.Bandgap += NV.RefOffset; /* add voltage offset */ /* * battery check */ /* * ADC pin is connected to a voltage divider Rh = 10k and Rl = 3k3. * Ul = (Uin / (Rh + Rl)) * Rl -> Uin = (Ul * (Rh + Rl)) / Rl * Uin = (Ul * (10k + 3k3)) / 3k3 = 4 * Ul */ /* get current voltage */ U_Bat = ReadU(TP_BAT); /* read voltage (mV) */ U_Bat *= 4; /* calculate U_bat (mV) */ U_Bat += BAT_OFFSET; /* add offset for voltage drop */ /* display battery voltage */ LCD_EEString_Space(Battery_str); /* display: Bat. */ DisplayValue(U_Bat / 10, -2, 'V'); /* display battery voltage */ LCD_Space(); /* check limits */ if (U_Bat < BAT_POOR) /* low level reached */ { LCD_EEString(Low_str); /* display: low */ MilliSleep(2000); /* let user read info */ goto power_off; /* power off */ } else if (U_Bat < BAT_POOR + 1000) /* warning level reached */ { LCD_EEString(Weak_str); /* display: weak */ } else /* ok */ { LCD_EEString(OK_str); /* display: ok */ } /* * probing */ /* display start of probing */ LCD_NextLine_EEString(Running_str); /* display: probing... */ /* try to discharge any connected component */ DischargeProbes(); if (Check.Found == COMP_ERROR) /* discharge failed */ { goto result; /* skip all other checks */ } /* enter main menu if requested by short-circuiting all probes */ if (AllProbesShorted() == 3) { MainMenu(); /* enter mainmenu */; goto end; /* new cycle after job is is done */ } /* check all 6 combinations of the 3 probes */ CheckProbes(TP1, TP2, TP3); CheckProbes(TP2, TP1, TP3); CheckProbes(TP1, TP3, TP2); CheckProbes(TP3, TP1, TP2); CheckProbes(TP2, TP3, TP1); CheckProbes(TP3, TP2, TP1); /* if component might be a capacitor */ if ((Check.Found == COMP_NONE) || (Check.Found == COMP_RESISTOR)) { /* tell user to be patient with large caps :-) */ LCD_Space(); LCD_Char('C'); /* check all possible combinations */ MeasureCap(TP3, TP1, 0); MeasureCap(TP3, TP2, 1); MeasureCap(TP2, TP1, 2); } /* * output test results */ result: LCD_Clear(); /* clear LCD */ LCD_NextLine_Mode(MODE_KEEP | MODE_KEY); /* call output function based on component type */ switch (Check.Found) { case COMP_ERROR: Show_Error(); goto end; break; case COMP_DIODE: Show_Diode(); break; case COMP_BJT: Show_BJT(); break; case COMP_FET: Show_FET(); break; case COMP_IGBT: Show_IGBT(); break; case COMP_THYRISTOR: Show_Special(); break; case COMP_TRIAC: Show_Special(); break; case COMP_RESISTOR: Show_Resistor(); break; case COMP_CAPACITOR: Show_Capacitor(); break; default: /* no component found */ Show_Fail(); goto end; } /* component was found */ RunsMissed = 0; /* reset counter */ RunsPassed++; /* increase counter */ /* * take care about cycling and power-off */ end: #ifdef HW_RELAY ADC_DDR = (1<<TP_REF); /* short circuit probes */ #endif LCD_NextLine_Mode(MODE_NONE); /* reset next line mode */ /* get key press or timeout */ Test = TestKey((uint16_t)CYCLE_DELAY, 12); if (Test == 0) /* timeout (no key press) */ { /* check if we reached the maximum number of rounds (continious mode only) */ if ((RunsMissed >= CYCLE_MAX) || (RunsPassed >= CYCLE_MAX * 2)) { goto power_off; /* -> power off */ } } else if (Test == 1) /* short key press */ { /* a second key press triggers extra functions */ MilliSleep(50); Test = TestKey(300, 0); if (Test > 0) /* short or long key press */ { #ifdef HW_RELAY ADC_DDR = 0; /* remove short circuit */ #endif MainMenu(); /* enter main menu */ goto end; /* re-run cycle control */ } } else if (Test == 2) /* long key press */ { goto power_off; /* -> power off */ } #ifdef HW_ENCODER else if (Test == 4) /* rotary encoder: left turn */ { MainMenu(); /* enter main menu */ goto end; /* re-run cycle control */ } #endif /* default action (also for rotary encoder) */ goto start; /* -> next round */ power_off: /* display feedback (otherwise the user will wait :-) */ LCD_Clear(); LCD_EEString(Bye_str); wdt_disable(); /* disable watchdog */ CONTROL_PORT &= ~(1 << POWER_CTRL); /* power off myself */ return 0; }
void ImAdapter::timeCheck(size_t queueIndex){ /* * 清空的循环 */ while(1){ MilliTimer timer; if(stateBuffer_[queueIndex].empty()){ LOG_DEBUG("EMPTY"); MilliSleep(300); continue ; } /* * 取缓冲队列队头 */ // 取缓冲队列队头 UserOnlineStatePtr statePtr = stateBuffer_[queueIndex].front(); /* * 健壮性设计 */ // 确保队头指针引用的目标的存在 if(!statePtr){ continue; // return ; } /* * 日志打印缓冲队列当前大小 */ // LOG_DEBUG("SB[" << queueIndex << "]|"<<stateBuffer_[queueIndex].size()); /* * 健壮性设计 */ if(statePtr->timeQueue.empty()){ // 离线请求对象的时间戳队列为空的情况,不易出现的数值异常情形 LOG_ERROR("unexpected error|"<<stateBuffer_[queueIndex].size()); // statePtr->timeQueue.pop_front(); stateBuffer_[queueIndex].pop_front(); continue ; //return ; } /* * 队头离线请求对象超时的情况,需要让队头出队 */ long firstTime = statePtr->timeQueue.front(); if(time(NULL) - firstTime > CUSHIONSTATE_TIME){ //LOG_DEBUG("SB[" << queueIndex << "]POP|"<<stateBuffer_[queueIndex].size()); // 弹出离线请求指针 stateBuffer_[queueIndex].pop_front(); // 弹出离线请求对象的最早时间戳 statePtr->timeQueue.pop_front(); /* * 离线请求缓冲队列中离线请求指针惟一且缓冲状态为下线状态的情况下,可以调用IM对象的离线接口 */ { int count = 0 ; { boost::mutex::scoped_lock lock(statePtr->countMutex); count = statePtr->offlineCount ; } if(statePtr->imState == ONLINE && statePtr->cushionState == OFFLINE && ((count - 1) < 1)){ // 调用离线接口 LOG_INFO("IMOFF|"<<statePtr->userId); iMGateForPhoneAdapter_.offline(statePtr->userId); removeUser(statePtr->userId); }else{ // 不能删除离线请求的情况,修改受影响的字段 LOG_INFO("OFF|--|"<<statePtr->userId); { boost::mutex::scoped_lock lock(statePtr->countMutex); statePtr->offlineCount--; } LOG_INFO("OFF|C|"<<statePtr->userId<<"|"<<statePtr->offlineCount); } } }else{ /* * 队头时间戳不超时,则无须继续检查队头以后的离线请求对象指针的时间戳,退出循环 */ MilliSleep(150); continue ; // break; } } }
int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) { #ifdef WIN32 u_long nOne = 1; if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR) { printf("ConnectSocket() : ioctlsocket non-blocking setting failed, error %d\n", WSAGetLastError()); #else if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == SOCKET_ERROR) { printf("ConnectSocket() : fcntl non-blocking setting failed, error %d\n", errno); #endif return -2; } struct timeval timeout = {10, 0}; struct pkt *msg = new pkt; struct pkt *prt = new pkt; time_t seconds_transmit; int len = 48; msg->li_vn_mode=227; msg->stratum=0; msg->ppoll=4; msg->precision=0; msg->rootdelay=0; msg->rootdispersion=0; msg->ref.Ul_i.Xl_i=0; msg->ref.Ul_f.Xl_f=0; msg->org.Ul_i.Xl_i=0; msg->org.Ul_f.Xl_f=0; msg->rec.Ul_i.Xl_i=0; msg->rec.Ul_f.Xl_f=0; msg->xmt.Ul_i.Xl_i=0; msg->xmt.Ul_f.Xl_f=0; int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen); if (retcode < 0) { printf("sendto() failed: %d\n", retcode); seconds_transmit = -3; goto _end; } fd_set fdset; FD_ZERO(&fdset); FD_SET(sockfd, &fdset); retcode = select(sockfd + 1, &fdset, NULL, NULL, &timeout); if (retcode <= 0) { printf("recvfrom() error\n"); seconds_transmit = -4; goto _end; } recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL); ntohl_fp(&msg->xmt, &prt->xmt); Ntp2Unix(prt->xmt.Ul_i.Xl_ui, seconds_transmit); _end: delete msg; delete prt; return seconds_transmit; } int64_t NtpGetTime(CNetAddr& ip) { struct sockaddr cliaddr; SOCKET sockfd; socklen_t servlen; if (!InitWithRandom(sockfd, servlen, &cliaddr)) return -1; ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr); int64_t nTime = DoReq(sockfd, servlen, cliaddr); closesocket(sockfd); return nTime; } int64_t NtpGetTime(const std::string &strHostName) { struct sockaddr cliaddr; SOCKET sockfd; socklen_t servlen; if (!InitWithHost(strHostName, sockfd, servlen, &cliaddr)) return -1; int64_t nTime = DoReq(sockfd, servlen, cliaddr); closesocket(sockfd); return nTime; } // NTP server, which we unconditionally trust. This may be your own installation of ntpd somewhere, for example. // "localhost" means "trust no one" std::string strTrustedUpstream = "localhost"; // Current offset int64_t nNtpOffset = INT64_MAX; int64_t GetNtpOffset() { return nNtpOffset; } void ThreadNtpSamples(void* parg) { const int64_t nMaxOffset = 86400; // Not a real limit, just sanity threshold. printf("Trying to find NTP server at localhost...\n"); std::string strLocalHost = "127.0.0.1"; if (NtpGetTime(strLocalHost) == GetTime()) { printf("There is NTP server active at localhost, we don't need NTP thread.\n"); nNtpOffset = 0; return; } printf("ThreadNtpSamples started\n"); vnThreadsRunning[THREAD_NTP]++; // Make this thread recognisable as time synchronization thread RenameThread("Chipcoin-ntp-samples"); CMedianFilter<int64_t> vTimeOffsets(200,0); while (!fShutdown) { if (strTrustedUpstream != "localhost") { // Trying to get new offset sample from trusted NTP server. int64_t nClockOffset = NtpGetTime(strTrustedUpstream) - GetTime(); if (abs64(nClockOffset) < nMaxOffset) { // Everything seems right, remember new trusted offset. printf("ThreadNtpSamples: new offset sample from %s, offset=%" PRId64 ".\n", strTrustedUpstream.c_str(), nClockOffset); nNtpOffset = nClockOffset; } else { // Something went wrong, disable trusted offset sampling. nNtpOffset = INT64_MAX; strTrustedUpstream = "localhost"; int nSleepMinutes = 1 + GetRandInt(9); // Sleep for 1-10 minutes. for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++) MilliSleep(1000); continue; } } else { // Now, trying to get 2-4 samples from random NTP servers. int nSamplesCount = 2 + GetRandInt(2); for (int i = 0; i < nSamplesCount; i++) { CNetAddr ip; int64_t nClockOffset = NtpGetTime(ip) - GetTime(); if (abs64(nClockOffset) < nMaxOffset) { // Skip the deliberately wrong timestamps printf("ThreadNtpSamples: new offset sample from %s, offset=%" PRId64 ".\n", ip.ToString().c_str(), nClockOffset); vTimeOffsets.input(nClockOffset); } } if (vTimeOffsets.size() > 1) { nNtpOffset = vTimeOffsets.median(); } else { // Not enough offsets yet, try to collect additional samples later. nNtpOffset = INT64_MAX; int nSleepMinutes = 1 + GetRandInt(4); // Sleep for 1-5 minutes. for (int i = 0; i < nSleepMinutes * 60 && !fShutdown; i++) MilliSleep(1000); continue; } } if (GetNodesOffset() == INT_MAX && abs64(nNtpOffset) > 40 * 60) { // If there is not enough node offsets data and NTP time offset is greater than 40 minutes then give a warning. std::string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Chipcoin will not work properly."); strMiscWarning = strMessage; printf("*** %s\n", strMessage.c_str()); uiInterface.ThreadSafeMessageBox(strMessage+" ", std::string("Chipcoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION); } printf("nNtpOffset = %+" PRId64 " (%+" PRId64 " minutes)\n", nNtpOffset, nNtpOffset/60); int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours. for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++) MilliSleep(1000); } vnThreadsRunning[THREAD_NTP]--; printf("ThreadNtpSamples exited\n"); }
int readResults(ISocket * socket, bool readBlocked, bool useHTTP, StringBuffer &result) { if (readBlocked) socket->set_block_mode(BF_SYNC_TRANSFER_PULL,0,60*1000); unsigned len; bool is_status; bool isBlockedResult; for (;;) { if (delay) MilliSleep(delay); is_status = false; isBlockedResult = false; try { if (useHTTP) len = 0x10000; else if (readBlocked) len = socket->receive_block_size(); else { socket->read(&len, sizeof(len)); _WINREV(len); } } catch(IException * e) { if (manyResults) showMessage("End of result multiple set\n"); else pexception("failed to read len data", e); e->Release(); return 1; } if (len == 0) { if (manyResults) { showMessage("----End of result set----\n"); continue; } break; } bool isSpecial = false; bool pluginRequest = false; bool dataBlockRequest = false; if (len & 0x80000000) { unsigned char flag; isSpecial = true; socket->read(&flag, sizeof(flag)); switch (flag) { case '-': if (echoResults) fputs("Error:", stdout); if (saveResults && trace != NULL) fputs("Error:", trace); break; case 'D': showMessage("request for datablock\n"); dataBlockRequest = true; break; case 'P': showMessage("request for plugin\n"); pluginRequest = true; break; case 'S': if (showStatus) showMessage("Status:"); is_status=true; break; case 'T': showMessage("Timing:\n"); break; case 'X': showMessage("---Compound query finished---\n"); return 1; case 'R': isBlockedResult = true; break; } len &= 0x7FFFFFFF; len--; // flag already read } char * mem = (char*) malloc(len+1); char * t = mem; unsigned sendlen = len; t[len]=0; try { if (useHTTP) { try { socket->read(t, 0, len, sendlen); } catch (IException *E) { if (E->errorCode()!= JSOCKERR_graceful_close) throw; E->Release(); break; } if (!sendlen) break; } else if (readBlocked) socket->receive_block(t, len); else socket->read(t, len); } catch(IException * e) { pexception("failed to read data", e); return 1; } if (pluginRequest) { //Not very robust! A poor man's implementation for testing... StringBuffer dllname, libname; const char * dot = strchr(t, '.'); dllname.append("\\edata\\bin\\debug\\").append(t); libname.append("\\edata\\bin\\debug\\").append(dot-t,t).append(".lib"); sendFile(dllname.str(), socket); sendFile(libname.str(), socket); } else if (dataBlockRequest) { //Not very robust! A poor man's implementation for testing... offset_t offset; memcpy(&offset, t, sizeof(offset)); _WINREV(offset); sendFileChunk(t+sizeof(offset), offset, socket); } else { if (isBlockedResult) { t += 8; t += strlen(t)+1; sendlen -= (t - mem); } if (echoResults && (!is_status || showStatus)) { fwrite(t, sendlen, 1, stdout); fflush(stdout); } if (!is_status) result.append(sendlen, t); } free(mem); if (abortAfterFirst) return 0; } return 0; }
void cmd_dispatch::action() { // printf("%s\n",name); char x[100]; sprintf(x,"elapsed time : %s",name); // elapsed_time_trace t(x); IHRPCtransport * transport = MakeTcpTransportFromUrl(name, HOAGENT_PORT); hoagent agent; agent.UseTransport(transport); transport->SetConnectTimeout(calltimeout?(calltimeout*1000):(num_threads==1?600:0)); transport->SetTimeout(calltimeout?calltimeout:3); StringBuffer result; result.append(name).appendf("(%d) ",node_number); if (stricmp(argv[2], "alive") == 0) result.append(agent.alive(atoi(argv[3]))); else if (stricmp(argv[2], "start") == 0) { StringBuffer cmdbuf; for (char *cp=argv[3]; *cp; cp++) { if (*cp == '%' && *(cp+1)) { cp++; switch (*cp) { case 'n': // Node number cmdbuf.append(node_number); break; case 'a': // Node address cmdbuf.append(name); break; case 'l': // Node list cmdbuf.append(argv[1]); break; case 'x': // Next Node if(next != NULL) cmdbuf.append(next); break; default: // treat as literal (?) cmdbuf.append('%').append(*cp); break; } } else cmdbuf.append(*cp); } result.append(agent.start_process(cmdbuf.str())); } else if (stricmp(argv[2], "stop") == 0) result.append(agent.stop_process(atoi(argv[3]))); else if (stricmp(argv[2], "account") == 0) { transport->SetTimeout(calltimeout?calltimeout:15); int cd=25; bool success=false; while (cd&&!success) { char * u=unscr(strdup(argv[3])); StringBuffer pw; if (encrypted) decrypt(pw, argv[4]); else pw.append(argv[4]); char *p = unscr(pw.detach()); if (cd>1) { try { agent.account(u, p, argv[5]); if (agent.start_process(statcmd) ) success=true; } catch (...) { } if (!success) { srand(GetCurrentThreadId()+clock()); MilliSleep((rand() * 3000) / RAND_MAX); } } else { agent.account(u, p, argv[5]); if (agent.start_process(statcmd) ) success=true; } cd--; } if (!success) result.append(" failed"); else result.appendf(" ok (retries=%i)",24-cd); } else if (stricmp(argv[2], "dir") == 0) { transport->SetTimeout(15); agent.set_dir(argv[3]); } if (result.length()) { printf("%s\n", result.toCharArray()); } transport->Release(); }
int CMCastRecvServer::run() { SocketEndpoint ackEp(broadcastRoot); ackEp.port = ackPort; StringBuffer s; ackEp.getIpText(s); ackSock.setown(ISocket::udp_connect(ackEp.port, s.str())); ackSock->set_send_buffer_size(UDP_SEND_SIZE); StringBuffer ipStr; mcastEp.getIpText(ipStr); sock.setown(ISocket::multicast_create(mcastEp.port, ipStr.str())); sock->set_receive_buffer_size(UDP_RECV_SIZE); SocketEndpoint ep(ipStr.str()); sock->join_multicast_group(ep); MemoryBuffer mbAck; MCAckPacketHeader *ackPacket = (MCAckPacketHeader *)mbAck.reserveTruncate(MC_ACK_PACKET_SIZE); ackPacket->node = groupMember; LOG(MCdebugProgress(10), unknownJob, "Running as client %d connected to server %s", groupMember, broadcastRoot.get()); unsigned *nackList = (unsigned *)(((byte *)ackPacket)+sizeof(MCAckPacketHeader)); const unsigned *nackUpper = (unsigned *)((byte *)ackPacket)+MC_ACK_PACKET_SIZE-sizeof(unsigned); Owned<CDataPacket> dataPacket = new CDataPacket(); CTimeMon logTm(10000), logTmCons(5000), logTmPoll(5000), logTmOld(5000), logTmNoRecv(5000); loop { try { unsigned startTime = msTick(); loop { try { size32_t szRead; sock->read(dataPacket->header, sizeof(MCPacketHeader), MC_PACKET_SIZE, szRead, 5000); break; } catch (IException *e) { if (JSOCKERR_timeout_expired != e->errorCode()) throw; else e->Release(); LOG(MCdebugProgress(1), unknownJob, "Waiting on packet read socket (waited=%d)", msTick()-startTime); } } if (stopped) break; if (MCPacket_Stop == dataPacket->header->cmd) { stopped = true; break; } ackPacket->tag = dataPacket->header->tag; ackPacket->jobId = dataPacket->header->jobId; if (oldJobIds.find(dataPacket->header->jobId)) { if (MCPacket_Poll == dataPacket->header->cmd) { ackPacket->ackDone = true; MilliSleep(MAX_POLL_REPLY_DELAY/(groupMember+1)); ackSock->write(ackPacket, sizeof(MCAckPacketHeader)); } if (tracingPeriod && logTmOld.timedout()) { LOG(MCdebugProgress(1), unknownJob, "Old job polled=%s", MCPacket_Poll == dataPacket->header->cmd?"true":"false"); logTmOld.reset(tracingPeriod); } } else { CMCastReceiver *receiver = getReceiver(dataPacket->header->tag); if (receiver) { if (MCPacket_Poll == dataPacket->header->cmd) { size32_t sz; bool res = receiver->buildNack(ackPacket, sz, dataPacket->header->total); MilliSleep(MAX_POLL_REPLY_DELAY/(groupMember+1)); ackSock->write(ackPacket, sz); if (tracingPeriod && logTmPoll.timedout()) { LOG(MCdebugProgress(1), unknownJob, "Send nack back sz=%d, res=%s, done=%s", sz, res?"true":"false", ackPacket->ackDone?"true":"false"); logTmPoll.reset(tracingPeriod); } } else { unsigned total = dataPacket->header->total; bool done; if (receiver->packetReceived(*dataPacket, done)) // if true, packet consumed { unsigned level; if (tracingPeriod && logTmCons.timedout()) { level = 1; logTmCons.reset(5000); } else level = 110; LOG(MCdebugProgress(level), unknownJob, "Pkt %d taken by receiver", dataPacket->header->id); if (done) { LOG(MCdebugProgress(10), unknownJob, "Client (tag=%x, jobId=%d) received all %d packets", dataPacket->header->tag, dataPacket->header->jobId, dataPacket->header->total); oldJobIds.replace(* new CUIntValue(dataPacket->header->jobId)); } // JCSMORE should use packet pool. // init new packet dataPacket.setown(new CDataPacket()); } else if (tracingPeriod && logTm.timedout()) { LOG(MCdebugProgress(150), unknownJob, "throwing away packet %d", dataPacket->header->id); logTm.reset(tracingPeriod); } if (!done) { size32_t sz; if (receiver->buildNack(ackPacket, sz, total)) ackSock->write(ackPacket, sz); } } } else if (tracingPeriod && logTmNoRecv.timedout()) { LOG(MCdebugProgress(1), unknownJob, "No Receiver tag=%d", dataPacket->header->tag); logTmNoRecv.reset(tracingPeriod); } } } catch (IException *e) { pexception("Client Exception",e); break; } } PROGLOG("Receive server stopping, aborting receivers"); { CriticalBlock b(receiversCrit); SuperHashIteratorOf<CMCastReceiver> iter(receivers); ForEach (iter) iter.query().stop(); } return 0; }
void static HonghuoMiner(CWallet *pwallet,int targetConter) { LogPrint("INFO","Miner started\n"); SetThreadPriority(THREAD_PRIORITY_LOWEST); RenameThread("Honghuo-miner"); auto CheckIsHaveMinerKey = [&]() { LOCK2(cs_main, pwalletMain->cs_wallet); set<CKeyID> setMineKey; setMineKey.clear(); pwalletMain->GetKeys(setMineKey, true); return !setMineKey.empty(); }; if (!CheckIsHaveMinerKey()) { LogPrint("INFO", "HonghuoMiner terminated\n"); ERRORMSG("ERROR:%s ", "no key for minering\n"); return ; } auto getcurhigh = [&]() { LOCK(cs_main); return chainActive.Height(); }; targetConter = targetConter+getcurhigh(); try { SetMinerStatus(true); while (true) { if (SysCfg().NetworkID() != CBaseParams::REGTEST) { // Busy-wait for the network to come online so we don't waste time mining // on an obsolete chain. In regtest mode we expect to fly solo. while (vNodes.empty() || (chainActive.Tip() && chainActive.Tip()->nHeight>1 && GetAdjustedTime()-chainActive.Tip()->nTime > 60*60)) MilliSleep(1000); } // // Create new block // unsigned int LastTrsa = mempool.GetTransactionsUpdated(); CBlockIndex* pindexPrev = chainActive.Tip(); CAccountViewCache accview(*pAccountViewTip, true); CTransactionDBCache txCache(*pTxCacheTip, true); CScriptDBViewCache ScriptDbTemp(*pScriptDBTip, true); int64_t lasttime1 = GetTimeMillis(); shared_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(accview, txCache, ScriptDbTemp)); if (!pblocktemplate.get()){ throw runtime_error("Create new block fail."); } LogPrint("MINER", "CreateNewBlock tx count:%d used time :%d ms\n", pblocktemplate.get()->block.vptx.size(), GetTimeMillis() - lasttime1); CBlock *pblock = &pblocktemplate.get()->block; MiningBlock(pblock, pwallet, pindexPrev, LastTrsa, accview, txCache, ScriptDbTemp); if (SysCfg().NetworkID() != CBaseParams::MAIN) if(targetConter <= getcurhigh()) { throw boost::thread_interrupted(); } } } catch (...) { LogPrint("INFO","HonghuoMiner terminated\n"); SetMinerStatus(false); throw; } }
void processMessage(CMessageBuffer &mb) { ICoven &coven=queryCoven(); SessionId id; int fn; mb.read(fn); switch (fn) { case MSR_REGISTER_PROCESS_SESSION: { acceptConnections.wait(); acceptConnections.signal(); Owned<INode> node(deserializeINode(mb)); Owned<INode> servernode(deserializeINode(mb)); // hopefully me, but not if forwarded int role=0; if (mb.length()-mb.getPos()>=sizeof(role)) { // a capability block present mb.read(role); if (!manager.authorizeConnection(role,false)) { SocketEndpoint sender = mb.getSender(); mb.clear(); coven.reply(mb); MilliSleep(100+getRandom()%1000); // Causes client to 'work' for a short time. Owned<INode> node = createINode(sender); coven.disconnect(node); break; } #ifdef _DEBUG StringBuffer eps; PROGLOG("Connection to %s authorized",mb.getSender().getUrlStr(eps).str()); #endif } IGroup *covengrp; id = manager.registerClientProcess(node.get(),covengrp,(DaliClientRole)role); mb.clear().append(id); if (covengrp->rank(servernode)==RANK_NULL) { // must have been redirected covengrp->Release(); // no good, so just use one we know about (may use something more sophisticated later) INode *na = servernode.get(); covengrp = createIGroup(1, &na); } covengrp->serialize(mb); covengrp->Release(); coven.reply(mb); } break; case MSR_SECONDARY_REGISTER_PROCESS_SESSION: { mb.read(id); Owned<INode> node (deserializeINode(mb)); int role; mb.read(role); manager.addProcessSession(id,node.get(),(DaliClientRole)role); mb.clear(); coven.reply(mb); } break; case MSR_REGISTER_SESSION: { SecurityToken tok; SessionId parentid; mb.read(tok).read(parentid); SessionId id = manager.registerSession(tok,parentid); mb.clear().append(id); coven.reply(mb); } break; case MSR_SECONDARY_REGISTER_SESSION: { mb.read(id); manager.addSession(id); mb.clear(); coven.reply(mb); } break; case MSR_LOOKUP_PROCESS_SESSION: { // looks up from node or from id Owned<INode> node (deserializeINode(mb)); if (node->endpoint().isNull()&&(mb.length()-mb.getPos()>=sizeof(id))) { mb.read(id); INode *n = manager.getProcessSessionNode(id); if (n) node.setown(n); node->serialize(mb.clear()); } else { id = manager.lookupProcessSession(node.get()); mb.clear().append(id); } coven.reply(mb); } break; case MSR_STOP_SESSION: { SessionId sessid; bool failed; mb.read(sessid).read(failed); manager.stopSession(sessid,failed); mb.clear(); coven.reply(mb); } break; case MSR_LOOKUP_LDAP_PERMISSIONS: { StringAttr key; StringAttr obj; Owned<IUserDescriptor> udesc=createUserDescriptor(); StringAttr username; StringAttr passwordenc; mb.read(key).read(obj); udesc->deserialize(mb); #ifndef _NO_DALIUSER_STACKTRACE //following debug code to be removed StringBuffer sb; udesc->getUserName(sb); if (0==sb.length()) { DBGLOG("UNEXPECTED USER (NULL) in dasess.cpp CSessionRequestServer::processMessage() line %d", __LINE__); } #endif unsigned auditflags = 0; if (mb.length()-mb.getPos()>=sizeof(auditflags)) mb.read(auditflags); int err = 0; int ret=manager.getPermissionsLDAP(key,obj,udesc,auditflags,&err); mb.clear().append(ret); if (err) mb.append(err); coven.reply(mb); } break; } }
// Sanity test: this should loop ten times, and // min/max/average should be close to 100ms. static void Sleep100ms(benchmark::State& state) { while (state.KeepRunning()) { MilliSleep(100); } }
int readResults(ISocket * socket, bool readBlocked, bool useHTTP, StringBuffer &result, const char *query, size32_t queryLen) { if (readBlocked) socket->set_block_mode(BF_SYNC_TRANSFER_PULL,0,60*1000); MemoryBuffer remoteReadCursorMb; unsigned len; bool is_status; bool isBlockedResult; for (;;) { if (delay) MilliSleep(delay); is_status = false; isBlockedResult = false; try { if (useHTTP) len = 0x10000; else if (readBlocked) len = socket->receive_block_size(); else { socket->read(&len, sizeof(len)); _WINREV(len); } } catch(IException * e) { if (manyResults) showMessage("End of result multiple set\n"); else pexception("failed to read len data", e); e->Release(); return 1; } if (len == 0) { if (manyResults) { showMessage("----End of result set----\n"); continue; } break; } bool isSpecial = false; bool pluginRequest = false; bool dataBlockRequest = false; bool remoteReadRequest = false; if (len & 0x80000000) { unsigned char flag; isSpecial = true; socket->read(&flag, sizeof(flag)); switch (flag) { case '-': if (echoResults) fputs("Error:", stdout); if (saveResults && trace != NULL) fputs("Error:", trace); break; case 'D': showMessage("request for datablock\n"); dataBlockRequest = true; break; case 'P': showMessage("request for plugin\n"); pluginRequest = true; break; case 'S': if (showStatus) showMessage("Status:"); is_status=true; break; case 'T': showMessage("Timing:\n"); break; case 'X': showMessage("---Compound query finished---\n"); return 1; case 'R': isBlockedResult = true; break; case 'J': remoteReadRequest = true; break; } len &= 0x7FFFFFFF; len--; // flag already read } MemoryBuffer mb; mb.setEndian(__BIG_ENDIAN); char *mem = (char *)mb.reserveTruncate(len+1); char * t = mem; size32_t sendlen = len; t[len]=0; try { if (useHTTP) { try { socket->read(t, 0, len, sendlen); } catch (IException *E) { if (E->errorCode()!= JSOCKERR_graceful_close) throw; E->Release(); break; } if (!sendlen) break; } else if (readBlocked) socket->receive_block(t, len); else socket->read(t, len); } catch(IException * e) { pexception("failed to read data", e); e->Release(); return 1; } if (pluginRequest) { //Not very robust! A poor man's implementation for testing... StringBuffer dllname, libname; const char * dot = strchr(t, '.'); dllname.append("\\edata\\bin\\debug\\").append(t); libname.append("\\edata\\bin\\debug\\").append(dot-t,t).append(".lib"); sendFile(dllname.str(), socket); sendFile(libname.str(), socket); } else if (dataBlockRequest) { //Not very robust! A poor man's implementation for testing... offset_t offset; mb.read(offset); sendFileChunk((const char *)mb.readDirect(offset), offset, socket); } else if (remoteReadRequest) { Owned<IPropertyTree> requestTree = createPTreeFromJSONString(queryLen, query); Owned<IPropertyTree> responseTree; // used if response is xml or json const char *outputFmtStr = requestTree->queryProp("format"); const char *response = nullptr; if (!outputFmtStr || strieq("xml", outputFmtStr)) { response = (const char *)mb.readDirect(len); responseTree.setown(createPTreeFromXMLString(len, response)); } else if (strieq("json", outputFmtStr)) { response = (const char *)mb.readDirect(len); responseTree.setown(createPTreeFromJSONString(len, response)); } unsigned cursorHandle; if (responseTree) cursorHandle = responseTree->getPropInt("cursor"); else mb.read(cursorHandle); bool retrySend = false; if (cursorHandle) { PROGLOG("Got handle back: %u; len=%u", cursorHandle, len); StringBuffer xml; if (responseTree) { if (echoResults && response) { fputs(response, stdout); fflush(stdout); } if (!responseTree->getPropBin("cursorBin", remoteReadCursorMb.clear())) break; } else { size32_t dataLen; mb.read(dataLen); if (!dataLen) break; const void *rowData = mb.readDirect(dataLen); // JCSMORE - output binary row data? // cursor size32_t cursorLen; mb.read(cursorLen); if (!cursorLen) break; const void *cursor = mb.readDirect(cursorLen); memcpy(remoteReadCursorMb.clear().reserveTruncate(cursorLen), cursor, cursorLen); } if (remoteStreamForceResend) cursorHandle = NotFound; // fake that it's a handle dafilesrv doesn't know about Owned<IPropertyTree> requestTree = createPTree(); requestTree->setPropInt("cursor", cursorHandle); // Only the handle is needed for continuation, but this tests the behaviour of some clients which may send cursor per request (e.g. to refresh) if (remoteStreamSendCursor) requestTree->setPropBin("cursorBin", remoteReadCursorMb.length(), remoteReadCursorMb.toByteArray()); requestTree->setProp("format", outputFmtStr); StringBuffer requestStr; toJSON(requestTree, requestStr); #ifdef _DEBUG fputs(requestStr, stdout); #endif sendlen = requestStr.length(); _WINREV(sendlen); try { if (!rawSend && !useHTTP) socket->write(&sendlen, sizeof(sendlen)); socket->write(requestStr.str(), requestStr.length()); } catch (IJSOCK_Exception *e) { retrySend = true; EXCLOG(e, nullptr); e->Release(); } } else // dafilesrv didn't know who I was, resent query + serialized cursor retrySend = true; if (retrySend) { PROGLOG("Retry send for handle: %u", cursorHandle); requestTree->setPropBin("cursorBin", remoteReadCursorMb.length(), remoteReadCursorMb.toByteArray()); StringBuffer requestStr; toJSON(requestTree, requestStr); PROGLOG("requestStr = %s", requestStr.str()); sendlen = requestStr.length(); _WINREV(sendlen); if (!rawSend && !useHTTP) socket->write(&sendlen, sizeof(sendlen)); socket->write(requestStr.str(), requestStr.length()); } } else { if (isBlockedResult) { t += 8; t += strlen(t)+1; sendlen -= (t - mem); } if (echoResults && (!is_status || showStatus)) { fwrite(t, sendlen, 1, stdout); fflush(stdout); } if (!is_status) result.append(sendlen, t); } if (abortAfterFirst) return 0; } return 0; }
static int CommandLineRPC(int argc, char *argv[]) { std::string strPrint; int nRet = 0; try { // Skip switches while (argc > 1 && IsSwitchChar(argv[1][0])) { argc--; argv++; } std::string rpcPass; if (gArgs.GetBoolArg("-stdinrpcpass", false)) { if (!std::getline(std::cin, rpcPass)) { throw std::runtime_error("-stdinrpcpass specified but failed to read from standard input"); } gArgs.ForceSetArg("-rpcpassword", rpcPass); } std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]); if (gArgs.GetBoolArg("-stdin", false)) { // Read one arg per line from stdin and append std::string line; while (std::getline(std::cin, line)) { args.push_back(line); } } std::unique_ptr<BaseRequestHandler> rh; std::string method; if (gArgs.GetBoolArg("-getinfo", false)) { rh.reset(new GetinfoRequestHandler()); method = ""; } else { rh.reset(new DefaultRequestHandler()); if (args.size() < 1) { throw std::runtime_error("too few parameters (need at least command)"); } method = args[0]; args.erase(args.begin()); // Remove trailing method name from arguments vector } // Execute and handle connection failures with -rpcwait const bool fWait = gArgs.GetBoolArg("-rpcwait", false); do { try { const UniValue reply = CallRPC(rh.get(), method, args); // Parse reply const UniValue& result = find_value(reply, "result"); const UniValue& error = find_value(reply, "error"); if (!error.isNull()) { // Error int code = error["code"].get_int(); if (fWait && code == RPC_IN_WARMUP) throw CConnectionFailed("server in warmup"); strPrint = "error: " + error.write(); nRet = abs(code); if (error.isObject()) { UniValue errCode = find_value(error, "code"); UniValue errMsg = find_value(error, "message"); strPrint = errCode.isNull() ? "" : "error code: "+errCode.getValStr()+"\n"; if (errMsg.isStr()) strPrint += "error message:\n"+errMsg.get_str(); if (errCode.isNum() && errCode.get_int() == RPC_WALLET_NOT_SPECIFIED) { strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to bitcoin-cli command line."; } } } else { // Result if (result.isNull()) strPrint = ""; else if (result.isStr()) strPrint = result.get_str(); else strPrint = result.write(2); } // Connection succeeded, no need to retry. break; } catch (const CConnectionFailed&) { if (fWait) MilliSleep(1000); else throw; } } while (fWait); } catch (const boost::thread_interrupted&) { throw; } catch (const std::exception& e) { strPrint = std::string("error: ") + e.what(); nRet = EXIT_FAILURE; } catch (...) { PrintExceptionContinue(nullptr, "CommandLineRPC()"); throw; } if (strPrint != "") { fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str()); } return nRet; }
void FireUpJobs( MyServer& server, MyClient& client, int n_thread, int n_extra, Checker* checker ) { REMARK("client %d: calling adjust_job_count_estimate(%d)\n", client.client_id(),n_thread); // Exercise independent_thread_number_changed, even for zero values. server.independent_thread_number_changed( n_extra ); #if _WIN32||_WIN64 ::rml::server::execution_resource_t me; server.register_master( me ); #endif /* _WIN32||_WIN64 */ // Experiments indicate that when oversubscribing, the main thread should wait a little // while for the RML worker threads to do some work. if( checker ) { // Give RML time to respond to change in number of threads. MilliSleep(1); for( int k=0; k<n_thread; ++k ) client.job_array[k].processing_count = 0; } //close the gate to keep worker threads from returning to RML until a snapshot is taken client.close_the_gate(); server.adjust_job_count_estimate( n_thread ); int n_used = 0; if( checker ) { MilliSleep(100); for( int k=0; k<n_thread; ++k ) if( client.job_array[k].processing_count ) ++n_used; } // open the gate client.open_the_gate(); // Logic further below presumes that jobs never starve, so undo previous call // to independent_thread_number_changed before waiting on those jobs. server.independent_thread_number_changed( -n_extra ); REMARK("client %d: wait for each job to be processed at least once\n",client.client_id()); // Calculate the number of jobs that are expected to get threads. int expected = n_thread; // Wait for expected number of jobs to be processed. #if RML_USE_WCRM int default_concurrency = server.default_concurrency(); if( N_TestConnections>0 ) { if( default_concurrency+1>=8 && n_thread<=3 && N_TestConnections<=3 && (default_concurrency/int(N_TestConnections)-1)>=n_thread ) { #endif /* RML_USE_WCRM */ for(;;) { int n = 0; for( int k=0; k<n_thread; ++k ) if( client.job_array[k].processing_count!=0 ) ++n; if( n>=expected ) break; server.yield(); } #if RML_USE_WCRM } else if( n_thread>0 ) { for( int m=0; m<20; ++m ) { int n = 0; for( int k=0; k<n_thread; ++k ) if( client.job_array[k].processing_count!=0 ) ++n; if( n>=expected ) break; MilliSleep(1); } } } #endif /* RML_USE_WCRM */ server.adjust_job_count_estimate(-n_thread); #if _WIN32||_WIN64 server.unregister_master( me ); #endif // Give RML some time to respond if( checker ) { MilliSleep(1); checker->check_number_of_threads_delivered( n_used, n_thread, n_extra ); } }