📄 genome.cpp
字号:
/*************************************************************************** genome.cpp - description ------------------- begin : Sat Dec 8 2001 copyright : (C) 2001 by Rudiger Koch email : rkoch@rkoch.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include "genome.h"#include <netdb.h>#include <arpa/inet.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <iostream>Genome::Genome(){ gHandler = NULL;}Genome::~Genome(){}void Genome::get(string uri){ if(uri.substr(0,4) == string("file")){ string path = uri.substr(5); getFile(path); } else if(uri.substr(0,7) == string("http://")){ getHttp(uri); }}void Genome::getFile(string path){ cout << "Loading Genefile " << path << endl; int fd; char buffer[20]; string filename = path.substr(path.length()-13); geneId = atoi(filename.c_str()); if((fd = open(path.c_str(), O_RDONLY)) == -1) throw string("cannot open genefile"); int amount; while((amount = read(fd, buffer, sizeof(buffer)))>0) { if(amount <=-1) throw string("read error"); for(int i=0; i<amount;i++){ genome += buffer[i]; } } if (close(fd) < 0) throw string("closing filedescriptor failed");}void Genome::getHttp(string _url){ struct sockaddr_in server; int msgsock; char buffer[100]; string download; URL = _url; string hostname = URL.substr(7, URL.find('/', 9) - 7); string path = URL.substr(URL.find('/', 9)); if ((msgsock = socket( AF_INET, SOCK_STREAM, 0)) < 0) throw string("socket error"); struct hostent *serverh = gethostbyname(hostname.c_str()); if (!serverh) throw string("cannot resolve hostname ") + hostname; memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; memcpy((char *)&server.sin_addr.s_addr, serverh->h_addr, serverh->h_length); server.sin_port = htons(80); if (connect(msgsock, (struct sockaddr*)(void*)&server, sizeof(server)) < 0) throw string("cannot connect to server"); string req = string("GET ") + path + string (" HTTP/1.0\n\n"); if (write(msgsock, req.c_str(), req.length()) < 0) throw string("cannot send request"); int amount; while((amount = read(msgsock, buffer, sizeof(buffer)))>0) { if(amount <=-1){ throw string("error reading from server");} for(int i=0; i<amount;i++){ download += buffer[i]; } } const char* idHeader = "Gene-Id:"; unsigned int pos = download.find(idHeader)+strlen(idHeader); if(pos == string::npos) throw "No Gene-Id header"; string id = download.substr(pos, 10); geneId = atoi(id.c_str()); if(!geneId) throw string("Malformed Gene-Id header. ID = ") + id; const char* formatHeader = "Chromosome-format:"; unsigned int pos1 = download.find(formatHeader); if(pos1 == string::npos) throw string("No Format header"); unsigned int pos2 = download.find("\r\n", pos1); string format = download.substr(pos1, pos2-pos1); if(!geneId) throw string("Malformed Chromosome-Format header"); Format(format); genome = download.substr(download.find("\r\n\r\n")+4); if (close(msgsock) < 0) throw string("cannot close socket");}void Genome::submit(int score){ struct sockaddr_in server; int msgsock; char buffer[100]; if(URL.empty()) throw string("No server URL specified - cannot submit a score"); if ((msgsock = socket( AF_INET, SOCK_STREAM, 0)) < 0) throw string("cannot open socket"); string hostname = URL.substr(7, URL.find('/', 9) - 7); string path = URL.substr(URL.find('/', 9)); struct hostent *serverh = gethostbyname(hostname.c_str()); if (!serverh) throw string("cannot resolve hostname ") + hostname; memset(&server, 0, sizeof(server)); server.sin_family = AF_INET; memcpy((char *)&server.sin_addr.s_addr, serverh->h_addr, serverh->h_length); server.sin_port = htons(80); if (connect(msgsock, (struct sockaddr*)(void*)&server, sizeof(server)) < 0) throw string("cannot connect to server"); char args[50]; sprintf(args, "?%d&%d HTTP/1.0\n\n", geneId, score); string req(string("GET ") + path + string(args)); if (write(msgsock, req.c_str(), req.length()) < 0) throw string("write error"); int amount; while((amount = read(msgsock, buffer, sizeof(buffer)))>0) { if(amount <=-1) throw string("read error"); } if (close(msgsock) < 0) throw string("cannot close socket");}void Genome::setHandler(GenomeHandler *gh){ gHandler = gh;}void Genome::parse(){ if(!gHandler) throw string("no genome handler registered"); int pos=0; for(unsigned int i=0; i<chromosomes.size(); i++){ gHandler->startChromosome(chromosomes[i].second, chromosomes[i].first); for(int j=0; j<chromosomes[i].first; j++){ gHandler->gene(genome.substr(pos, chromosomes[i].second)); pos += chromosomes[i].second; } } gHandler->finish();}void Genome::Format(string fmt){ unsigned int lbracket = 0; unsigned int rbracket, comma; chromosomes.clear(); while ((lbracket = fmt.find('{', lbracket)) != string::npos){ lbracket++; comma = fmt.find(',', lbracket); if(comma == string::npos) throw string("error in format string\n"); rbracket = fmt.find('}', lbracket); if(rbracket == string::npos) throw string("error in format string\n"); pair <int, int> chr; chr.first = atoi(fmt.substr(lbracket, comma).c_str()); chr.second = atoi(fmt.substr(comma+1, rbracket).c_str()); chromosomes.push_back(chr); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -