Ejemplo n.º 1
0
void DviSessionUpnp::Subscribe()
{
    LOG(kDvEvent, "Subscription request from ");
    iHeaderCallback.Log();
    LOG(kDvEvent, "\n");
    if (iHeaderSid.Received()) {
        Renew();
        return;
    }

    if (!iHeaderCallback.Received() || !iHeaderNt.Received() || !iHeaderTimeout.Received()) {
        Error(HttpStatus::kPreconditionFailed);
    }
    DviDevice* device;
    DviService* service;
    ParseRequestUri(DviProtocolUpnp::kEventUrlTail, &device, &service);
    if (device == NULL || service == NULL) {
        Error(HttpStatus::kPreconditionFailed);
    }
    TUint duration = iHeaderTimeout.Timeout();
    Brh sid;
    device->CreateSid(sid);
    SubscriptionDataUpnp* data = new SubscriptionDataUpnp(iHeaderCallback.Endpoint(), iHeaderCallback.Uri());
    DviSubscription* subscription = new DviSubscription(*device, *iPropertyWriterFactory, data, sid, duration);
    iPropertyWriterFactory->SubscriptionAdded(*subscription);
    DviSubscriptionManager::AddSubscription(*subscription);

    if (iHeaderExpect.Continue()) {
        iWriterResponse->WriteStatus(HttpStatus::kContinue, Http::eHttp11);
        iWriterResponse->WriteFlush();
    }
    // respond to subscription request
    iResponseStarted = true;
    iWriterResponse->WriteStatus(HttpStatus::kOk, Http::eHttp11);
    WriteServerHeader(*iWriterResponse);
    IWriterAscii& writerSid = iWriterResponse->WriteHeaderField(HeaderSid::kHeaderSid);
    writerSid.Write(HeaderSid::kFieldSidPrefix);
    writerSid.Write(subscription->Sid());
    writerSid.WriteFlush();
    IWriterAscii& writerTimeout = iWriterResponse->WriteHeaderField(HeaderTimeout::kHeaderTimeout);
    writerTimeout.Write(HeaderTimeout::kFieldTimeoutPrefix);
    writerTimeout.WriteUint(duration);
    writerTimeout.WriteFlush();
    iWriterResponse->WriteHeader(Http::kHeaderConnection, Http::kConnectionClose);
    iWriterResponse->WriteFlush();
    iResponseEnded = true;

    LOG(kDvEvent, "Subscription complete for ");
    iHeaderCallback.Log();
    LOG(kDvEvent, ", sid is ");
    LOG(kDvEvent, subscription->Sid());
    LOG(kDvEvent, "\n");

    // Start subscription, prompting delivery of the first update (covering all state variables)
    service->AddSubscription(subscription);
}
void DviProviderSubscriptionLongPoll::Subscribe(IDvInvocation& aInvocation, const Brx& aClientId, const Brx& aUdn, const Brx& aService, TUint aRequestedDuration,
                                                IDvInvocationResponseString& aSid, IDvInvocationResponseUint& aDuration)
{
    if (aRequestedDuration > kTimeoutLongPollSecs) {
        aRequestedDuration = kTimeoutLongPollSecs;
    }

    DviDevice* device = iDvStack.DeviceMap().Find(aUdn);
    if (device == NULL) {
        aInvocation.Error(kErrorCodeBadDevice, kErrorDescBadDevice);
    }
    DviService* service = NULL;
    const TUint serviceCount = device->ServiceCount();
    for (TUint i=0; i<serviceCount; i++) {
        DviService* s = &device->Service(i);
        if (s->ServiceType().PathUpnp() == aService) {
            service = s;
            break;
        }
    }
    if (service == NULL) {
        aInvocation.Error(kErrorCodeBadService, kErrorDescBadService);
    }
    Brh sid;
    device->CreateSid(sid);
    TUint timeout = aRequestedDuration;
    DviSubscription* subscription = new DviSubscription(iDvStack, *device, iPropertyUpdateCollection, NULL, sid, timeout);

    aInvocation.StartResponse();
    aSid.Write(subscription->Sid());
    aSid.WriteFlush();
    aDuration.Write(timeout);
    aInvocation.EndResponse();

    // Start subscription, prompting availability of the first update (covering all state variables)
    iPropertyUpdateCollection.AddSubscription(aClientId, subscription);
    iDvStack.SubscriptionManager().AddSubscription(*subscription);
    service->AddSubscription(subscription);
}