// Write data to an open socket, repeat until all data has been sent. int wxLuaCSocket::Write(const char *buffer_, wxUint32 length_) { if ((m_sockstate != SOCKET_CONNECTED) && (m_sockstate != SOCKET_ACCEPTED)) { AddErrorMessage(wxT("Unable to write to unconnected or unaccepted socket. ")); return 0; } int length = length_; const char *buffer = buffer_; int num_written = 0; while (num_written < length) { int s = ::send(m_sock, buffer, length - num_written, 0); if (s == SOCKET_ERROR) { AddErrorMessage(wxT("Got a socket error trying to write to socket.")); return num_written; } num_written += s; buffer += s; } return num_written; }
bool wxLuaCSocket::Close() { if (m_sockstate != SOCKET_CLOSED) { #ifdef WIN32 if (::closesocket(m_sock) == SOCKET_ERROR) { AddErrorMessage(wxT("Unable to close socket.")); return false; } #else if (::close(m_sock)) { AddErrorMessage(wxT("Unable to close socket.")); return false; } #endif // WIN32 else { m_sockstate = SOCKET_CLOSED; return true; } } return false; }
// Read data from an open socket, repeat reading until all data has been read int wxLuaCSocket::Read(char *buffer_, wxUint32 length_) { if ((m_sockstate != SOCKET_CONNECTED) && (m_sockstate != SOCKET_ACCEPTED)) { AddErrorMessage(wxT("Unable to read from an unconnected or unaccepted socket. ")); return 0; } int length = length_; char *buffer = buffer_; int num_read = 0; while (num_read < length) { int r = ::recv(m_sock, buffer, length - num_read, 0); if (r == 0) return num_read; if (r == SOCKET_ERROR) { AddErrorMessage(wxT("Got a socket error trying to read.")); return num_read; } num_read += r; buffer += r; } return num_read; }
int wxLuawxSocket::Write(const char *buffer, wxUint32 length) { wxCHECK_MSG(m_socket, 0, wxT("Invalid wxLuawxSocket")); if (!IsConnected()) { AddErrorMessage(wxT("Unable to write to an unconnected or unaccepted socket. ")); return 0; } long num_written = 0; if (m_socket->WaitForWrite(20, 0)) num_written = m_socket->Write(buffer, length).LastCount(); if ((num_written < (long)length) || m_socket->Error()) { wxString s(wxT("Got a socket error trying to read. ")); if (m_socket->Error()) s += GetLastErrorMsg(); AddErrorMessage(s); } return num_written; }
bool ArgList::V2QuotedToV2Raw(char const *v1_input,MyString *v2_raw,MyString *errmsg) { if(!v1_input) return true; ASSERT(v2_raw); // allow leading whitespace while(isspace(*v1_input)) { v1_input++; } ASSERT(IsV2QuotedString(v1_input)); ASSERT(*v1_input == '"'); v1_input++; const char *quote_terminated = NULL; while(*v1_input) { if(*v1_input == '"') { v1_input++; if(*v1_input == '"') { // Repeated (i.e. escaped) double-quote. (*v2_raw) += *(v1_input++); } else { quote_terminated = v1_input-1; break; } } else { (*v2_raw) += *(v1_input++); } } if(!quote_terminated) { AddErrorMessage("Unterminated double-quote.",errmsg); return false; } // allow trailing whitespace while(isspace(*v1_input)) { v1_input++; } if(*v1_input) { if(errmsg) { MyString msg; msg.sprintf( "Unexpected characters following double-quote. " "Did you forget to escape the double-quote by repeating it? " "Here is the quote and trailing characters: %s\n",quote_terminated); AddErrorMessage(msg.Value(),errmsg); } return false; } return true; }
bool wxLuaCSocket::Connect(const wxString &addr, u_short port_number) { m_port_number = port_number; hostent *pHost = NULL; if (m_sockstate != SOCKET_CLOSED) { AddErrorMessage(wxString::Format(wxT("Unable to connect to addr '%s' socket already open."), addr.c_str())); return false; } m_sock = ::socket(AF_INET, SOCK_STREAM, 0); if (m_sock == INVALID_SOCKET) { AddErrorMessage(wxString::Format(wxT("Unable to create client socket for addr '%s'."), addr.c_str())); return false; } unsigned long address = ::inet_addr(wx2lua(addr)); if (address != INADDR_NONE) pHost = ::gethostbyaddr((const char*) &address, 4, AF_INET); else pHost = ::gethostbyname(wx2lua(addr)); if (pHost == NULL) { AddErrorMessage(wxString::Format(wxT("Unable to get hostbyaddr or gethostbyname for addr '%s'."), addr.c_str())); return false; } if (pHost->h_addrtype != AF_INET) { AddErrorMessage(wxString::Format(wxT("Socket for addr '%s' is wrong type, isn't AF_INET."), addr.c_str())); return false; } memset(&m_sockaddress, 0, sizeof(m_sockaddress)); memcpy(&(m_sockaddress.sin_addr), pHost->h_addr_list[0], pHost->h_length); m_sockaddress.sin_family = AF_INET; m_sockaddress.sin_port = htons(port_number); m_address = lua2wx(inet_ntoa(m_sockaddress.sin_addr)); m_port_number = ntohs(m_sockaddress.sin_port); if (::connect(m_sock, (sockaddr *) &m_sockaddress, sizeof(m_sockaddress)) == SOCKET_ERROR) { AddErrorMessage(wxString::Format(wxT("Unable to connect socket to addr '%s'."), addr.c_str())); return false; } m_sockstate = SOCKET_CONNECTED; return true; }
bool Env::getDelimitedStringV1Raw(MyString *result,MyString *error_msg,char delim) const { MyString var, val; bool emptyString = true; if(!delim) delim = env_delimiter; ASSERT(result); _envTable->startIterations(); while( _envTable->iterate( var, val ) ) { if(!IsSafeEnvV1Value(var.Value(),delim) || !IsSafeEnvV1Value(val.Value(),delim)) { if(error_msg) { MyString msg; msg.sprintf("Environment entry is not compatible with V1 syntax: %s=%s",var.Value(),val.Value()); AddErrorMessage(msg.Value(),error_msg); } return false; } // only insert the delimiter if there's already an entry... if( !emptyString ) { (*result) += delim; } WriteToDelimitedString(var.Value(),*result); if(val != NO_ENVIRONMENT_VALUE) { WriteToDelimitedString("=",*result); WriteToDelimitedString(val.Value(),*result); } emptyString = false; } return true; }
bool HDFBaseCallsWriter::_WritePulseIndex(const SMRTSequence & read) { if (HasPulseIndex()) { if (read.copiedFromBam) { const PacBio::BAM::BamRecord & record = read.bamRecord; if (record.HasPulseCall()) { const std::string & pulsecall = record.PulseCall(); std::vector<uint16_t> data(read.length, 0); size_t indx = 0; for(size_t i = 0; i < pulsecall.size(); i++) { const char base = pulsecall[i]; if (base == 'A' or base == 'C' or base == 'G' or base == 'T') { assert(indx < read.length); data[indx] = static_cast<uint16_t>(i); indx ++; } else { assert(base == 'a' or base == 'c' or base == 'g' or base == 't'); } } assert(indx == read.length); pulseIndexArray_.Write(&data[0], read.length); return true; } } AddErrorMessage(std::string(PacBio::GroupNames::pulseindex) + " absent in read " + read.GetTitle()); return false; } return true; }
HDFPulseWriter::HDFPulseWriter(const std::string& filename, const std::string& basecallerVersion, const std::map<char, size_t>& baseMap, const std::vector<PacBio::BAM::BaseFeature>& qvsToWrite, const H5::FileAccPropList& fileAccPropList) : HDFWriterBase(filename) , fileaccproplist_(fileAccPropList) , basecallsWriter_(nullptr) , pulsecallsWriter_(nullptr) , regionsWriter_(nullptr) { // open file outfile_.Open(filename_, H5F_ACC_TRUNC, fileaccproplist_); // Add PulseData group to the root group '/' AddChildGroup(outfile_.rootGroup, pulseDataGroup_, PacBio::GroupNames::pulsedata); if (basecallerVersion.empty()) { AddErrorMessage("Base caller version must be specified."); } // Create a BaseCaller writer. basecallsWriter_.reset( new HDFBaseCallsWriter(filename_, pulseDataGroup_, baseMap, basecallerVersion, qvsToWrite)); // Create a PulseCalls writer pulsecallsWriter_.reset(new HDFPulseCallsWriter(filename_, pulseDataGroup_, baseMap, basecallerVersion, qvsToWrite)); }
bool ArgList::V1WackedToV1Raw(char const *v1_input,MyString *v1_raw,MyString *errmsg) { if(!v1_input) return true; ASSERT(v1_raw); ASSERT(!IsV2QuotedString(v1_input)); while(*v1_input) { if(*v1_input == '"') { if(errmsg) { MyString msg; msg.sprintf("Found illegal unescaped double-quote: %s",v1_input); AddErrorMessage(msg.Value(),errmsg); } return false; } else if(v1_input[0] == '\\' && v1_input[1] == '"') { // Escaped double-quote. v1_input++; (*v1_raw) += *(v1_input++); } else { (*v1_raw) += *(v1_input++); } } return true; }
wxLuaCSocket* wxLuaCSocket::Accept() { if (m_sockstate != SOCKET_LISTENING) { AddErrorMessage(wxT("Unable to accept from a socket that's not listening.")); return NULL; } sockaddr_in fromAddr = { 0 }; socklen_t length = sizeof(fromAddr); socket_type acceptedSocket = ::accept(m_sock, (sockaddr *)&fromAddr, &length); if (acceptedSocket == INVALID_SOCKET) { AddErrorMessage(wxT("Unable to accept socket connection.")); return NULL; } return new wxLuaCSocket(acceptedSocket, fromAddr); }
bool wxLuaCSocket::Listen(u_short port_number, int backLog) { m_port_number = port_number; if (m_sockstate != SOCKET_CLOSED) { AddErrorMessage(wxT("Failed to create a listening socket, socket already open.")); return false; } m_sock = ::socket(AF_INET, SOCK_STREAM, 0); if (m_sock == INVALID_SOCKET) { AddErrorMessage(wxT("Unable to create a listening socket.")); return false; } sockaddr_in localAddr = { 0 }; localAddr.sin_family = AF_INET; localAddr.sin_port = htons(port_number); localAddr.sin_addr.s_addr = htonl(INADDR_ANY); if (::bind(m_sock, (sockaddr *) &localAddr, sizeof(localAddr)) == SOCKET_ERROR) { AddErrorMessage(wxT("Unable to bind to socket to listen for clients.")); return false; } if (::listen(m_sock, backLog) == SOCKET_ERROR) { AddErrorMessage(wxT("Unable to listen to bound socket.")); return false; } memset(&m_sockaddress, 0, sizeof(m_sockaddress)); m_sockstate = SOCKET_LISTENING; return true; }
bool HDFBaseCallsWriter::_WriteSubstitutionTag(const SMRTSequence & read) { if (HasSubstitutionTag()) { if (read.substitutionTag == nullptr) { AddErrorMessage(std::string(PacBio::GroupNames::substitutiontag) + " absent in read " + read.GetTitle()); return false; } else { substitutionTagArray_.Write(read.substitutionTag, read.length); return true; } } return true; }
bool HDFBaseCallsWriter::_WriteMergeQV(const SMRTSequence & read) { if (HasMergeQV()) { if (read.mergeQV.Empty()) { AddErrorMessage(std::string(PacBio::GroupNames::mergeqv) + " absent in read " + read.GetTitle()); return false; } else { mergeQVArray_.Write(read.mergeQV.data, read.length); return true; } } return true; }
bool HDFBaseCallsWriter::_WriteIPD(const SMRTSequence & read) { if (HasIPD()) { if (read.preBaseFrames == nullptr) { AddErrorMessage(std::string(PacBio::GroupNames::prebaseframes) + " absent in read " + read.GetTitle()); return false; } else { ipdArray_.Write(read.preBaseFrames, read.length); return true; } } return true; }
bool HDFBaseCallsWriter::_WritePulseWidth(const SMRTSequence & read) { if (HasPulseWidth()) { if (read.widthInFrames == nullptr) { AddErrorMessage(std::string(PacBio::GroupNames::widthinframes) + " absent in read " + read.GetTitle()); return false; } else { pulseWidthArray_.Write(read.widthInFrames, read.length); return true; } } return true; }
bool HDFRegionsWriter::Write(const RegionAnnotation &annotation) { try { regionsArray_.WriteRow(annotation.row, HDFRegionsWriter::NCOLS); } catch (H5::Exception &e) { std::ostringstream sout; sout << "Failed to write region annotation " << annotation.GetHoleNumber(); AddErrorMessage(sout.str()); return false; } ++curRow_; return true; }
bool Env::SetEnvWithErrorMessage( const char *nameValueExpr, MyString *error_msg ) { char *expr, *delim; int retval; if( nameValueExpr == NULL || nameValueExpr[0] == '\0' ) { return false; } // make a copy of nameValueExpr for modifying expr = strnewp( nameValueExpr ); ASSERT( expr ); // find the delimiter delim = strchr( expr, '=' ); if(delim == NULL && strstr(expr,"$$")) { // This environment entry is an unexpanded $$() macro. // We just want to keep it in the environment verbatim. SetEnv(expr,NO_ENVIRONMENT_VALUE); delete[] expr; return true; } // fail if either name or delim is missing if( expr == delim || delim == NULL ) { if(error_msg) { MyString msg; if(delim == NULL) { msg.sprintf( "ERROR: Missing '=' after environment variable '%s'.", nameValueExpr); } else { msg.sprintf("ERROR: missing variable in '%s'.",expr); } AddErrorMessage(msg.Value(),error_msg); } delete[] expr; return false; } // overwrite delim with '\0' so we have two valid strings *delim = '\0'; // do the deed retval = SetEnv( expr, delim + 1 ); delete[] expr; return retval; }
HDFBaseCallsWriter::HDFBaseCallsWriter(const std::string & filename, HDFGroup & parentGroup, const std::map<char, size_t> & baseMap, const std::string & basecallerVersion, const std::vector<PacBio::BAM::BaseFeature> & qvsToWrite) : HDFWriterBase(filename) , parentGroup_(parentGroup) , qvsToWrite_({}) , basecallerVersion_(basecallerVersion) , arrayLength_(0) , zmwWriter_(nullptr) , zmwMetricsWriter_(nullptr) { // Add BaseCalls as a child group to the parent group. AddChildGroup(parentGroup_, basecallsGroup_, PacBio::GroupNames::basecalls); if (basecallerVersion.empty()) { AddErrorMessage("BaseCallerVersion must not be empty!"); return; } // Initialize the 'basecall' group. basecallArray_.Initialize(basecallsGroup_, PacBio::GroupNames::basecall); qvsToWrite_ = HDFBaseCallsWriter::WritableQVs(qvsToWrite); // It's ok to only write basecall. if (qvsToWrite_.size() > 0 and not InitializeQVGroups()) { AddErrorMessage("Failed to initialize QV Groups."); return; } // Create a zmwWriter. zmwWriter_.reset(new HDFZMWWriter(filename, basecallsGroup_)); // Create a zmwMetricsWriter. zmwMetricsWriter_.reset(new HDFZMWMetricsWriter(filename, basecallsGroup_, baseMap)); }
bool ArgList::AppendArgsV2Quoted(char const *args,MyString *error_msg) { if(!IsV2QuotedString(args)) { AddErrorMessage("Expecting double-quoted input string (V2 format).",error_msg); return false; } MyString v2; if(!V2QuotedToV2Raw(args,&v2,error_msg)) { return false; } return AppendArgsV2Raw(v2.Value(),error_msg); }
bool Env::MergeFromV2Quoted( const char *delimitedString, MyString *error_msg ) { if(!delimitedString) return true; if(IsV2QuotedString(delimitedString)) { MyString v2; if(!V2QuotedToV2Raw(delimitedString,&v2,error_msg)) { return false; } return MergeFromV2Raw(v2.Value(),error_msg); } else { AddErrorMessage("Expecting a double-quoted environment string (V2 format).",error_msg); return false; } }
void HDFBaseCallsWriter::Close(void) { this->Flush(); try { _WriteAttributes(); } catch (H5::Exception e) { AddErrorMessage("Failed to write attributes to " + PacBio::GroupNames::basecalls); } basecallArray_.Close(); if (HasDeletionQV()) deletionQVArray_.Close(); if (HasDeletionTag()) deletionTagArray_.Close(); if (HasInsertionQV()) insertionQVArray_.Close(); if (HasMergeQV()) mergeQVArray_.Close(); if (HasSubstitutionQV()) substitutionQVArray_.Close(); if (HasSubstitutionTag()) substitutionTagArray_.Close(); if (HasIPD()) ipdArray_.Close(); if (HasPulseWidth()) pulseWidthArray_.Close(); if (HasPulseIndex()) pulseIndexArray_.Close(); }
bool HDFRegionsWriter::WriteAttributes(void) { if (curRow_ > 0) { /* AddAttribute<std::vector<std::string>>(regionsArray_, PacBio::AttributeNames::Regions::columnnames, PacBio::AttributeValues::Regions::columnnames); AddAttribute<std::vector<std::string>>( regionsArray_, PacBio::AttributeNames::Regions::regiontypes, regionTypes_); AddAttribute<std::vector<std::string>>( regionsArray_, PacBio::AttributeNames::Regions::regiondescriptions, PacBio::AttributeValues::Regions::regiondescriptions); AddAttribute<std::vector<std::string>>(regionsArray_, PacBio::AttributeNames::Regions::regionsources, PacBio::AttributeValues::Regions::regionsources); */ return true; } else { AddErrorMessage("Could not write attributes when Regions group is empty."); return false; } }
bool Env::InsertEnvIntoClassAd( ClassAd *ad, MyString *error_msg, char const *opsys, CondorVersionInfo *condor_version) const { bool has_env1 = ad->LookupExpr(ATTR_JOB_ENVIRONMENT1) != NULL; bool has_env2 = ad->LookupExpr(ATTR_JOB_ENVIRONMENT2) != NULL; bool requires_env1 = false; if(condor_version) { requires_env1 = CondorVersionRequiresV1(*condor_version); } if(requires_env1) { if(has_env2) { ad->Delete(ATTR_JOB_ENVIRONMENT2); } } if( (has_env2 || !has_env1) && !requires_env1) { MyString env2; if(!getDelimitedStringV2Raw(&env2,error_msg)) { return false; } ad->Assign(ATTR_JOB_ENVIRONMENT2,env2.Value()); } if(has_env1 || requires_env1) { // Record the OPSYS that is being used to delimit the environment. char *lookup_delim = NULL; char delim = '\0'; if(opsys) { // Use delimiter for target opsys. delim = GetEnvV1Delimiter(opsys); } else if(ad->LookupString(ATTR_JOB_ENVIRONMENT1_DELIM,&lookup_delim)) { // Use delimiter that was used previously in this ad. delim = *lookup_delim; } else { // Use delimiter for the opsys we are currently running under. delim = env_delimiter; } if(!lookup_delim) { // Save the delimiter that we have chosen, in case the ad // is read by somebody on a platform that is not the same // as opsys. Example: we are writing the expanded ad in // the schedd for a starter on a different opsys, but we // want shadows to be able to still parse the environment. char delim_str[2]; delim_str[0] = delim; delim_str[1] = '\0'; ad->Assign(ATTR_JOB_ENVIRONMENT1_DELIM,delim_str); } MyString env1; bool env1_success = getDelimitedStringV1Raw(&env1,error_msg,delim); if(lookup_delim) { free(lookup_delim); lookup_delim = NULL; } if(env1_success) { ad->Assign(ATTR_JOB_ENVIRONMENT1,env1.Value()); } else { if(has_env2) { // We failed to convert to V1 syntax, but we started // with V2, so this is a special kind of failure. // Rather than failing outright, simply stick // an invalid environment value in the V1 attribute. // This happens, for example, when the schedd is // generating the expanded ad to send to an older // starter that does not understand V2 syntax. // The end result in this case is to fail when the // starter tries to read the environment, and then // we go back and get a new match, which hopefully // is a machine that understands V2 syntax. A more // direct mechanism would be nice, but this one should // at least prevent incorrect behavior. ad->Assign(ATTR_JOB_ENVIRONMENT1,"ENVIRONMENT_CONVERSION_ERROR"); dprintf(D_FULLDEBUG,"Failed to convert environment to V1 syntax: %s\n",error_msg ? error_msg->Value() : ""); } else { // Failed to convert to V1 syntax, and the ad does not // already contain V2 syntax, so we should assume the // worst. AddErrorMessage("Failed to convert to target environment syntax.",error_msg); return false; } } } return true; }
bool ArgList::AppendArgsV1Raw_win32(char const *args,MyString *error_msg) { // Parse an args string in the format expected by the Windows // function CommandLineToArgv(). while(*args) { char const *begin_arg = args; MyString buf = ""; while(*args) { if(*args == ' ' || *args == '\t' || \ *args == '\n' || *args == '\r') { break; } else if(*args != '"') { buf += *(args++); } else { // quoted section char const *begin_quote = args; args++; //begin quote while(*args) { int backslashes = 0; while(*args == '\\') { backslashes++; args++; } if(backslashes && *args == '"') { //2n backslashes followed by quote // --> n backslashes //2n+1 backslashes followed by quote // --> n backslashes followed by quote while(backslashes >= 2) { backslashes -= 2; buf += '\\'; } if(backslashes) { buf += *(args++); //literal quote } else { break; //terminal quote } } else if(backslashes) { //literal backslashes while(backslashes--) { buf += '\\'; } } else if(*args == '"') { break; //terminal quote } else { buf += *(args++); } } if(*args != '"') { MyString msg; msg.sprintf("Unterminated quote in windows argument string starting here: %s",begin_quote); AddErrorMessage(msg.Value(),error_msg); return false; } args++; } } if(args > begin_arg) { ASSERT(args_list.Append(buf)); } while(*args == ' ' || *args == '\t' || *args == '\n' || *args == '\r') { args++; } } return true; }
STDMETHODIMP CUpdateCallback100Imp::UpdateErrorMessage(const wchar_t *message) { AddErrorMessage(message); return S_OK; }
bool ArgList::InsertArgsIntoClassAd(ClassAd *ad,CondorVersionInfo *condor_version,MyString *error_msg) { bool has_args1 = ad->LookupExpr(ATTR_JOB_ARGUMENTS1) != NULL; bool has_args2 = ad->LookupExpr(ATTR_JOB_ARGUMENTS2) != NULL; bool requires_v1 = false; bool condor_version_requires_v1 = false; if(condor_version) { requires_v1 = CondorVersionRequiresV1(*condor_version); condor_version_requires_v1 = true; } else if(input_was_unknown_platform_v1) { requires_v1 = true; } if( !requires_v1 ) { MyString args2; if(!GetArgsStringV2Raw(&args2,error_msg)) return false; ad->Assign(ATTR_JOB_ARGUMENTS2,args2.Value()); } else if(has_args2) { ad->Delete(ATTR_JOB_ARGUMENTS2); } if(requires_v1) { MyString args1; if(GetArgsStringV1Raw(&args1,error_msg)) { ad->Assign(ATTR_JOB_ARGUMENTS1,args1.Value()); } else { if(condor_version_requires_v1 && !input_was_unknown_platform_v1) { // We failed to convert to V1 syntax, but otherwise we could // have converted to V2 syntax. // Rather than failing outright, simply remove // all arguments from the ClassAd, which will // cause failures in older versions of Condor. // We get here, for example, when the schedd is // generating the expanded ad to send to an older // starter that does not understand V2 syntax. // The end result in this case is to fail when the // starter tries to read the arguments, and then // we go back and get a new match, which hopefully // is a machine that understands V2 syntax. A more // direct mechanism would be nice, but this one should // at least prevent incorrect behavior. ad->Delete(ATTR_JOB_ARGUMENTS1); ad->Delete(ATTR_JOB_ARGUMENTS2); if(error_msg) { dprintf(D_FULLDEBUG,"Failed to convert arguments to V1 syntax: %s\n",error_msg->Value()); } } else { // Failed to convert to V1 syntax, and the ad does not // already contain V2 syntax, so we should assume the // worst. AddErrorMessage("Failed to convert arguments to V1 syntax.",error_msg); return false; } } } else if(has_args1) { ad->Delete(ATTR_JOB_ARGUMENTS1); } return true; }