Ejemplo n.º 1
0
/**
 * Post a notification to an event subscriber. This is called in a device.
 *
 * @param sub The event subscriber
 * @param statVar The evented state variable
 * @return true if succesful; otherwise false
 */
static bool mupnp_subscriber_notifymain(mUpnpSubscriber* sub, mUpnpService* service, mUpnpStateVariable* statVar)
{
  char* host;
  int port;
  mUpnpNotifyRequest* notifyReq;
  mUpnpNotifyResponse* notifyRes;
  bool notifySuccess;

  mupnp_log_debug_l4("Entering...\n");

  host = mupnp_subscriber_getdeliveryhost(sub);
  port = mupnp_subscriber_getdeliveryport(sub);

  notifyReq = mupnp_event_notify_request_new();
  mupnp_event_notify_request_setpropertysetnode(notifyReq, sub, service, statVar);
  notifyRes = mupnp_event_notify_request_post(notifyReq, host, port);
  notifySuccess = mupnp_event_notify_response_issuccessful(notifyRes);

  mupnp_http_request_print(notifyReq->httpReq);
  mupnp_http_response_print(notifyRes->httpRes);

  mupnp_event_notify_request_delete(notifyReq);

  if (notifySuccess == false)
    return false;

  mupnp_subscriber_incrementnotifycount(sub);

  mupnp_log_debug_l4("Leaving...\n");

  return true;
}
Ejemplo n.º 2
0
/**
 * The function that calls all HTTP listener callback functions. Do not call
 * this from applications.
 *
 * @param httpReq The received HTTP request
 */
void mupnp_controlpoint_httprequestreceived(mUpnpHttpRequest *httpReq)
{
	mUpnpControlPoint *ctrlPoint = NULL;
	mUpnpNotifyRequest *notifyReq = NULL;
	mUpnpPropertyList *propList = NULL;
	mUpnpProperty *prop = NULL;
	mUpnpEventListenerList *eventListeners = NULL;
	const char *sid = NULL;
	long seq = 0;
	long timeout = 0;
	mUpnpDevice *dev = NULL;
	mUpnpService *service = NULL;
	int notifyListeners = 0;
	
	mupnp_log_debug_l4("Entering...\n");

	ctrlPoint = (mUpnpControlPoint *)mupnp_http_request_getuserdata(httpReq);

	mupnp_controlpoint_lock(ctrlPoint);

#if !defined(MUPNP_NOUSE_SUBSCRIPTION)
	if (mupnp_http_request_isnotifyrequest(httpReq) == true) {	
		notifyReq = mupnp_event_notify_request_new();
		mupnp_event_notify_request_sethttprequest(notifyReq, httpReq);

		/* Get service according to SID */
		sid = mupnp_event_notify_request_getsid(notifyReq);
		
		for (dev = mupnp_controlpoint_getdevices(ctrlPoint); 
		     dev != NULL;
		     dev = mupnp_device_next(dev))
		{
			service = mupnp_device_getservicebysid(dev, sid);
			if (service != NULL) break;
		}

		if (service != NULL) {
			/* We found a service */
			seq = mupnp_event_notify_request_getseq(notifyReq);
			
			/* Check that event key = previous + 1 */
			if (seq != 0 && seq != mupnp_service_geteventkey(service) + 1)
			{
				/* The sequence does not match, unsubscribe and subscribe */
				timeout = mupnp_service_getsubscriptiontimeout(service);
				mupnp_controlpoint_unsubscribe(ctrlPoint, service);
				mupnp_controlpoint_subscribe(ctrlPoint, service, timeout);
			} else {
				/* Wrap seq, so that assertion is true next time */
				if (seq == MUPNP_EVENT_MAX_SEQ) seq = 0;
				
				/* Set event key */
				mupnp_service_seteventkey(service, seq);
				
				notifyListeners = 1;
				propList = mupnp_event_notify_request_getpropertylist(notifyReq); 
				for (prop=mupnp_propertylist_gets(propList); 
				     prop != NULL; 
				     prop = mupnp_property_next(prop)) 
				{
					/* Update the service's state table from the event */
					mupnp_controlpoint_updatestatetablefromproperty(service, prop);
				}
			}
		}
		eventListeners = mupnp_controlpoint_geteventlisteners(ctrlPoint);
		mupnp_controlpoint_unlock(ctrlPoint);

		if (notifyListeners && propList != NULL) 
		{
			/* Notify listeners out of control point lock */
			for (prop=mupnp_propertylist_gets(propList); 
			     prop != NULL; 
			     prop = mupnp_property_next(prop)) 
			{
//                printf("\n%s\n%s\n%s\n", prop->name->value, prop->sid->value, prop->value->value);
				mupnp_eventlistenerlist_notify(ctrlPoint, eventListeners, prop);
			}
		}

		mupnp_event_notify_request_delete(notifyReq);
		mupnp_http_request_postokrequest(httpReq);
		
		return;
	}
#endif
	
	mupnp_controlpoint_unlock(ctrlPoint);
	mupnp_http_request_postbadrequest(httpReq);

	mupnp_log_debug_l4("Leaving...\n");
}