worldclock.c
#include <stdio.h>
#include "SDO.h"
#include "SCA.h"
static int utc_timezone_offset(char *city);
static int calculate_time_diff(int hour, char *city, char *otherCity);
/*
* Implementation of worldclock service.
*/
DATAOBJECT getLocalTimeForCity(DATAOBJECT request) {
SDOFACTORY df = getDataFactory();
SDOTYPE requestType = getType(request);
if (requestType == 0) {
requestType = findType(df,
"http://www.example.com/worldclock.xsd",
"worldclockRequest");
}
SDOPROPERTY requestTimeProp = getTypePropertyByName(requestType, "local-time");
DATAOBJECT requestTimeObj = getDataObject(request, requestTimeProp);
SDOPROPERTY cityProp = getTypePropertyByName(getPropertyType(requestTimeProp), "city");
char* requestCity = getCString(requestTimeObj, cityProp);
SDOPROPERTY hourProp = getTypePropertyByName(getPropertyType(requestTimeProp), "hour");
int requestTime = getInt(requestTimeObj, hourProp);
SDOPROPERTY otherCityProp = getTypePropertyByName(requestType, "city-to-request-time-for");
char *requestOtherCity = getCString(request, otherCityProp);
int time_diff = calculate_time_diff(requestTime, requestCity, requestOtherCity);
// error handling: create a SOAP fault
if (time_diff < -24 || time_diff > 24) {
int compcode, reason;
DATAOBJECT faultResponse = doAlloc(df,
"http://www.w3.org/2001/XMLSchema",
"anyType");
setCStringByName(faultResponse,
"faultstring",
"unrecognized city name");
SCASetFaultMessage("worldclock",
"getLocalTimeForCity",
"worldclock-fault",
0,
faultResponse,
&compcode,
&reason);
return 0;
}
SDOTYPE responseType = findType(df,
/* "http://www.example.com/worldclock.xsd", */
getURI(requestType),
"worldclockResponse");
SDOTYPE timeType = findType(df,
/* "http://www.example.com/worldclock.xsd", */
getURI(requestType),
"time");
DATAOBJECT response = doAllocByType(getDataFactory(), responseType);
DATAOBJECT responseTime = doAllocByType(getDataFactory(), timeType);
setCStringByName(responseTime, "city", requestOtherCity);
setIntByName(responseTime, "hour", time_diff);
SDOPROPERTY responseTimeProp = getTypePropertyByName(responseType, "remote-time");
setDataObject(response, responseTimeProp, responseTime);
return response;
}
static int calculate_time_diff(int hour, char *city, char *otherCity) {
return hour + utc_timezone_offset(city) - utc_timezone_offset(otherCity);
}
static int utc_timezone_offset(char *city) {
if (!strcmp("Bangalore", city)) return +5;
if (!strcmp("Berlin", city)) return +1;
if (!strcmp("San Francisco", city)) return -8;
// ...
// return 100 to indicate unknown argument city
return 100;
}