Ejemplo n.º 1
0
void RemoteSubscriber::timelineFinished(QNetworkReply* reply)
{
  if (reply->error() != QNetworkReply::NoError)
  {
    QLOG_ERROR() << "got error code when sending timeline:" << reply->errorString();
#if 0
    if (++m_errors > 10)
    {
      QLOG_ERROR() << "More than 10 errors received. Dropping this subscriber";
      RemoteComponent::Get()->subscriberRemove(clientIdentifier());
    }
#endif
  }
}
void RemoteComponent::handleSubscription(QHttpRequest* request, QHttpResponse* response, bool poll)
{
  QVariantMap headers = HeaderToMap(request->headers());

  // check for required headers
  if (!headers.contains("x-plex-client-identifier") ||
      !headers.contains("x-plex-device-name"))
  {
    QLOG_ERROR() << "Missing X-Plex headers in /timeline/subscribe request";
    response->setStatusCode(qhttp::ESTATUS_BAD_REQUEST);
    response->end();
    return;
  }

  // check for required arguments
  QVariantMap query = QueryToMap(request->url());

  if (!query.contains("commandID") || ((!query.contains("port")) && !poll))
  {
    QLOG_ERROR() << "Missing arguments to /timeline/subscribe request";
    response->setStatusCode(qhttp::ESTATUS_BAD_REQUEST);
    response->end();
    return;
  }

  QString clientIdentifier(request->headers()["x-plex-client-identifier"]);

  QMutexLocker lk(&m_subscriberLock);
  RemoteSubscriber* subscriber = nullptr;

  if (m_subscriberMap.contains(clientIdentifier))
  {
    QLOG_DEBUG() << "Refreshed subscriber:" << clientIdentifier;
    subscriber = m_subscriberMap[clientIdentifier];
    subscriber->reSubscribe();
  }
  else
  {
    if (poll)
    {
      QLOG_DEBUG() << "New poll subscriber:" << clientIdentifier << request->headers()["x-plex-device-name"];
      subscriber = new RemotePollSubscriber(clientIdentifier, request->headers()["x-plex-device-name"], response, this);
    }
    else
    {
      QUrl address;
      QString protocol = query.contains("protocol") ? query["protocol"].toList()[0].toString() : "http";
      int port = query.contains("port") ? query["port"].toList()[0].toInt() : 32400;

      address.setScheme(protocol);
      address.setHost(request->remoteAddress());
      address.setPort(port);

      QLOG_DEBUG() << "New subscriber:" << clientIdentifier << request->headers()["x-plex-device-name"] << address.toString();
      subscriber = new RemoteSubscriber(clientIdentifier, request->headers()["x-plex-device-name"], address, this);
    }

    m_subscriberMap[clientIdentifier] = subscriber;

    // if it's our first controller, we notify web for subscription
    if (m_subscriberMap.size() == 1)
    {
      QLOG_DEBUG() << "First subscriber added, subscribing to web";
      subscribeToWeb(true);
    }
  }

  subscriber->setCommandId(m_commandId, query["commandID"].toList()[0].toInt());

  if (!poll)
  {
    subscriber->sendUpdate();

    response->setStatusCode(qhttp::ESTATUS_OK);
    response->end();
  }
}