00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #ifdef SOPCAST
00037
00038 #include "zmm/zmm.h"
00039 #include "sopcast_service.h"
00040 #include "sopcast_content_handler.h"
00041 #include "content_manager.h"
00042 #include "string_converter.h"
00043 #include "config_manager.h"
00044 #include "server.h"
00045
00046 using namespace zmm;
00047 using namespace mxml;
00048
00049 #define SOPCAST_CHANNEL_URL "http://www.sopcast.com/gchlxml"
00050
00051 SopCastService::SopCastService()
00052 {
00053 url = Ref<URL>(new URL());
00054 pid = 0;
00055 curl_handle = curl_easy_init();
00056 if (!curl_handle)
00057 throw _Exception(_("failed to initialize curl!\n"));
00058 }
00059
00060 SopCastService::~SopCastService()
00061 {
00062 if (curl_handle)
00063 curl_easy_cleanup(curl_handle);
00064 }
00065
00066 service_type_t SopCastService::getServiceType()
00067 {
00068 return OS_SopCast;
00069 }
00070
00071 String SopCastService::getServiceName()
00072 {
00073 return _("SopCast");
00074 }
00075
00076 Ref<Object> SopCastService::defineServiceTask(Ref<Element> xmlopt, Ref<Object> params)
00077 {
00078 return nil;
00079 }
00080
00081 Ref<Element> SopCastService::getData()
00082 {
00083 long retcode;
00084 Ref<StringConverter> sc = StringConverter::i2i();
00085
00086 Ref<StringBuffer> buffer;
00087
00088 try
00089 {
00090 log_debug("DOWNLOADING URL: %s\n", SOPCAST_CHANNEL_URL);
00091 buffer = url->download(_(SOPCAST_CHANNEL_URL), &retcode,
00092 curl_handle, false, true, true);
00093
00094 }
00095 catch (Exception ex)
00096 {
00097 log_error("Failed to download SopCast XML data: %s\n",
00098 ex.getMessage().c_str());
00099 return nil;
00100 }
00101
00102 if (buffer == nil)
00103 return nil;
00104
00105 if (retcode != 200)
00106 return nil;
00107
00108 log_debug("GOT BUFFER\n%s\n", buffer->toString().c_str());
00109 Ref<Parser> parser(new Parser());
00110 try
00111 {
00112 return parser->parseString(sc->convert(buffer->toString()))->getRoot();
00113 }
00114 catch (ParseException pe)
00115 {
00116 log_error("Error parsing SopCast XML %s line %d:\n%s\n",
00117 pe.context->location.c_str(),
00118 pe.context->line,
00119 pe.getMessage().c_str());
00120 return nil;
00121 }
00122 catch (Exception ex)
00123 {
00124 log_error("Error parsing SopCast XML %s\n", ex.getMessage().c_str());
00125 return nil;
00126 }
00127
00128 return nil;
00129 }
00130
00131 bool SopCastService::refreshServiceData(Ref<Layout> layout)
00132 {
00133 log_debug("Refreshing SopCast service\n");
00134
00135
00136
00137
00138
00139
00140
00141 if (pid == 0)
00142 pid = pthread_self();
00143
00144 if (pid != pthread_self())
00145 throw _Exception(_("Not allowed to call refreshServiceData from different threads!"));
00146
00147 Ref<Element> reply = getData();
00148
00149 Ref<SopCastContentHandler> sc(new SopCastContentHandler());
00150 if (reply != nil)
00151 sc->setServiceContent(reply);
00152 else
00153 {
00154 log_debug("Failed to get XML content from SopCast service\n");
00155 throw _Exception(_("Failed to get XML content from SopCast service"));
00156 }
00157
00158 Ref<CdsObject> obj;
00159 do
00160 {
00163 obj = sc->getNextObject();
00164 if (obj == nil)
00165 break;
00166
00167 obj->setVirtual(true);
00168
00169 Ref<CdsObject> old = Storage::getInstance()->loadObjectByServiceID(RefCast(obj, CdsItem)->getServiceID());
00170 if (old == nil)
00171 {
00172 log_debug("Adding new SopCast object\n");
00173
00174 if (layout != nil)
00175 layout->processCdsObject(obj, nil);
00176 }
00177 else
00178 {
00179 log_debug("Updating existing SopCast object\n");
00180 obj->setID(old->getID());
00181 obj->setParentID(old->getParentID());
00182 struct timespec oldt, newt;
00183 oldt.tv_nsec = 0;
00184 oldt.tv_sec = old->getAuxData(_(ONLINE_SERVICE_LAST_UPDATE)).toLong();
00185 newt.tv_nsec = 0;
00186 newt.tv_sec = obj->getAuxData(_(ONLINE_SERVICE_LAST_UPDATE)).toLong();
00187 ContentManager::getInstance()->updateObject(obj);
00188 }
00189
00190 if (Server::getInstance()->getShutdownStatus())
00191 return false;
00192
00193 }
00194 while (obj != nil);
00195
00196 return false;
00197 }
00198
00199 #endif//SOPCAST