Skip to content

Commit

Permalink
parse the daemon stdout to figure out API ip adress and port
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed May 6, 2015
1 parent d9d755d commit db2dc57
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 17 deletions.
2 changes: 0 additions & 2 deletions src/downloaddelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ void DownloadDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti


painter->restore ();

qDebug() << "styling " << dl->name();
}

QSize DownloadDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Expand Down
31 changes: 26 additions & 5 deletions src/ipfs/ipfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ static bool initialized = false;

Ipfs::Ipfs()
: state_(PING_DAEMON),
refreshTimer_(this)
refreshTimer_(this),
api_ip_("127.0.0.1"),
api_port_("5001")
{
}

Expand All @@ -28,8 +30,20 @@ Ipfs& Ipfs::instance()
return instance;
}

QUrl Ipfs::api_url(const QString &command)
{
return QUrl(QString("http://%1:%2/api/v0/%3")
.arg(api_ip_, api_port_, command));
}

void Ipfs::query(const QString &command, AbstractIpfsCommand *originator)
{
query(api_url(command), originator);
}

void Ipfs::query(const QUrl &url, AbstractIpfsCommand *originator)
{
qDebug() << "HTTP query: " << url << endl;
QNetworkRequest request = QNetworkRequest(url);
request.setOriginatingObject(originator);
manager_->get(request);
Expand All @@ -45,7 +59,7 @@ void Ipfs::init()

// Ping the HTTP API to find out if a daemon is running
state_ = PING_DAEMON;
query(QUrl("http://127.0.0.1:5001/api/v0/version/"), NULL);
query("version", NULL);
}

void Ipfs::init_commands()
Expand Down Expand Up @@ -96,7 +110,7 @@ void Ipfs::replyFinished(QNetworkReply *reply)
{
if(reply->error())
{
qDebug() << "Error while pinging the API, lauching daemon manually";
qDebug() << "Could not ping the API, lauching daemon manually";
launch_daemon();
return;
}
Expand Down Expand Up @@ -129,7 +143,7 @@ void Ipfs::replyFinished(QNetworkReply *reply)
if(reply->error())
{
qDebug() << "http error: " << reply->errorString() << endl;
qDebug() << reply->readAll();
qDebug() << "request: " << reply->request().url() << endl;
return;
}

Expand Down Expand Up @@ -166,9 +180,16 @@ void Ipfs::timer()
if(state_ == LAUNCH_DAEMON)
{
QString stdout = daemon_process_->readAllStandardOutput();
QRegExp regex = QRegExp("API server listening on \\/([^\\/]+)\\/([^\\/]+)\\/([^\\/]+)\\/([^\\/]+)\\n");

if(stdout.contains(QRegExp("^API server listening.*")))
int index = regex.indexIn(stdout);
if(index >= 0)
{
//QString network = regex.cap(1);
api_ip_ = regex.cap(2);
//QString transport = regex.cap(3);
api_port_ = regex.cap(4);

refreshTimer_.stop();
state_ = RUNNING;
init_commands();
Expand Down
19 changes: 19 additions & 0 deletions src/ipfs/ipfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ class Ipfs : public QObject
IpfsSwarm swarm;
IpfsVersion version;

/**
* @return the API url for the specified command
*/
QUrl api_url(const QString &command);

/**
* Query the API with only a command name
* @param command
* @param originator
*/
void query(const QString &command, AbstractIpfsCommand *originator);

/**
* Query a specific API url, constructed from api_url()
* @param url
* @param originator
*/
void query(const QUrl &url, AbstractIpfsCommand *originator);

private:
Expand All @@ -51,6 +68,8 @@ private slots:
QNetworkAccessManager *manager_;
QProcess *daemon_process_;
QTimer refreshTimer_;
QString api_ip_;
QString api_port_;
};

#endif // IPFS_H
4 changes: 2 additions & 2 deletions src/ipfs/ipfsid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QJsonObject>
#include <QJsonArray>

const QString API_URL = "http://127.0.0.1:5001/api/v0/id";
const QString API_COMMAND = "id";

IpfsId::IpfsId(QObject *parent)
: AbstractIpfsCommand(parent),
Expand All @@ -18,7 +18,7 @@ IpfsId::IpfsId(QObject *parent)

void IpfsId::init()
{
Ipfs::instance().query(API_URL, this);
Ipfs::instance().query(API_COMMAND, this);
}

void IpfsId::on_reply(const QJsonObject *json)
Expand Down
6 changes: 3 additions & 3 deletions src/ipfs/ipfspin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <QUrl>
#include <QUrlQuery>

const QString API_URL = "http://127.0.0.1:5001/api/v0/pin/";
const QString API_COMMAND = "pin";

IpfsPin::IpfsPin(QObject *parent)
: AbstractIpfsCommand(parent)
Expand All @@ -15,7 +15,7 @@ void IpfsPin::init() { }

void IpfsPin::add_pin(IpfsHash &hash, bool recursive)
{
QUrl url = API_URL + "add";
QUrl url = Ipfs::instance().api_url(API_COMMAND + "/add");
QUrlQuery query;

query.addQueryItem("arg", hash.ToString());
Expand All @@ -29,7 +29,7 @@ void IpfsPin::add_pin(IpfsHash &hash, bool recursive)

void IpfsPin::ls_pin(IpfsPinType pin_type)
{
QUrl url = API_URL + "ls";
QUrl url = Ipfs::instance().api_url(API_COMMAND + "/ls");
QUrlQuery query;

QString type;
Expand Down
5 changes: 2 additions & 3 deletions src/ipfs/ipfsswarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QJsonObject>
#include <QJsonArray>

const QString API_URL = "http://127.0.0.1:5001/api/v0/swarm/";
const QString API_COMMAND = "swarm";

IpfsSwarm::IpfsSwarm(QObject *parent)
: AbstractIpfsCommand(parent),
Expand All @@ -27,8 +27,7 @@ void IpfsSwarm::init()

void IpfsSwarm::refresh_peers()
{
QUrl url = API_URL + "peers";
Ipfs::instance().query(url, this);
Ipfs::instance().query(API_COMMAND + "/peers", this);

qDebug() << "refreshing peers";
}
Expand Down
4 changes: 2 additions & 2 deletions src/ipfs/ipfsversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <QUrl>
#include <QJsonObject>

const QString API_URL = "http://127.0.0.1:5001/api/v0/version/";
const QString API_COMMAND = "version";

IpfsVersion::IpfsVersion(QObject *parent)
: AbstractIpfsCommand(parent),
Expand Down Expand Up @@ -32,7 +32,7 @@ int IpfsVersion::micro() const

void IpfsVersion::init()
{
Ipfs::instance().query(API_URL, this);
Ipfs::instance().query(API_COMMAND, this);
}

void IpfsVersion::on_reply(const QJsonObject *json)
Expand Down

0 comments on commit db2dc57

Please sign in to comment.