lllandmark.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @file lllandmark.cpp
  3. * @brief Landmark asset class
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include <errno.h>
  34. #include "lllandmark.h"
  35. #include "llregionhandle.h"
  36. #include "llmessage.h"
  37. std::pair<LLUUID, U64> LLLandmark::mLocalRegion;
  38. LLLandmark::region_map_t LLLandmark::mRegions;
  39. LLLandmark::region_callback_map_t LLLandmark::mRegionCallback;
  40. LLLandmark::LLLandmark(const LLUUID& region_id, const LLVector3& local_pos)
  41. : mGlobalPositionKnown(false),
  42. mRegionID(region_id),
  43. mRegionPos(local_pos)
  44. {
  45. }
  46. LLLandmark::LLLandmark(const LLVector3d& global_pos)
  47. : mGlobalPositionKnown(true),
  48. mGlobalPos(global_pos)
  49. {
  50. }
  51. bool LLLandmark::getGlobalPos(LLVector3d& pos)
  52. {
  53. if (mGlobalPositionKnown)
  54. {
  55. pos = mGlobalPos;
  56. }
  57. else if (mRegionID.notNull())
  58. {
  59. F32 g_x = -1.f;
  60. F32 g_y = -1.f;
  61. if (mRegionID == mLocalRegion.first)
  62. {
  63. from_region_handle(mLocalRegion.second, &g_x, &g_y);
  64. }
  65. else
  66. {
  67. region_map_t::iterator it = mRegions.find(mRegionID);
  68. if (it != mRegions.end())
  69. {
  70. from_region_handle(it->second.mRegionHandle, &g_x, &g_y);
  71. }
  72. }
  73. if (g_x > 0.f && g_y > 0.f)
  74. {
  75. pos.mdV[0] = g_x + mRegionPos.mV[0];
  76. pos.mdV[1] = g_y + mRegionPos.mV[1];
  77. pos.mdV[2] = mRegionPos.mV[2];
  78. setGlobalPos(pos);
  79. }
  80. }
  81. return mGlobalPositionKnown;
  82. }
  83. void LLLandmark::setGlobalPos(const LLVector3d& pos)
  84. {
  85. mGlobalPos = pos;
  86. mGlobalPositionKnown = true;
  87. }
  88. bool LLLandmark::getRegionID(LLUUID& region_id)
  89. {
  90. if (mRegionID.notNull())
  91. {
  92. region_id = mRegionID;
  93. return true;
  94. }
  95. return false;
  96. }
  97. LLVector3 LLLandmark::getRegionPos() const
  98. {
  99. return mRegionPos;
  100. }
  101. //static
  102. LLLandmark* LLLandmark::constructFromString(const char* buffer, S32 buff_size)
  103. {
  104. S32 chars_read_total = 0;
  105. S32 chars_read = 0;
  106. // Read the version
  107. U32 version = 0;
  108. S32 count = sscanf(buffer, "Landmark version %u\n%n", &version,
  109. &chars_read);
  110. if (count != 1)
  111. {
  112. llwarns << "Bad landmark asset. Cannot read version." << llendl;
  113. return NULL;
  114. }
  115. chars_read_total += chars_read;
  116. if (chars_read_total >= buff_size)
  117. {
  118. llwarns << "Bad landmark asset (truncated or corrupted)." << llendl;
  119. return NULL;
  120. }
  121. if (version == 1)
  122. {
  123. // Read the global position
  124. LLVector3d pos;
  125. count = sscanf(buffer + chars_read_total, "position %lf %lf %lf\n%n",
  126. pos.mdV + VX, pos.mdV + VY, pos.mdV + VZ, &chars_read);
  127. if (count == 3)
  128. {
  129. return new LLLandmark(pos);
  130. }
  131. llwarns << "Bad landmark asset. Incorrect position." << llendl;
  132. return NULL;
  133. }
  134. if (version != 2)
  135. {
  136. llwarns << "Unsupported landmark asset version !" << llendl;
  137. return NULL;
  138. }
  139. // *NOTE: changing the buffer size will require changing the scanf call
  140. // below.
  141. char region_id_str[MAX_STRING];
  142. count = sscanf(buffer + chars_read_total, "region_id %254s\n%n",
  143. region_id_str, &chars_read);
  144. if (count != 1)
  145. {
  146. llwarns << "Bad landmark asset. Cannot read region Id." << llendl;
  147. return NULL;
  148. }
  149. chars_read_total += chars_read;
  150. if (chars_read_total >= buff_size)
  151. {
  152. llwarns << "Bad landmark asset (truncated or corrupted)." << llendl;
  153. return NULL;
  154. }
  155. if (!LLUUID::validate(region_id_str))
  156. {
  157. llwarns << "Bad landmark asset: invalid region Id: " << region_id_str
  158. << llendl;
  159. return NULL;
  160. }
  161. LLUUID region_id(region_id_str);
  162. if (region_id.isNull())
  163. {
  164. llwarns << "Bad landmark asset: null region Id." << llendl;
  165. return NULL;
  166. }
  167. // Read the local position
  168. LLVector3 lpos;
  169. count = sscanf(buffer + chars_read_total, "local_pos %f %f %f\n%n",
  170. lpos.mV + VX, lpos.mV + VY, lpos.mV + VZ, &chars_read);
  171. if (count != 3)
  172. {
  173. llwarns << "Bad landmark asset. Cannot read position." << llendl;
  174. return NULL;
  175. }
  176. return new LLLandmark(region_id, lpos);
  177. }
  178. //static
  179. void LLLandmark::registerCallbacks(LLMessageSystem* msg)
  180. {
  181. msg->setHandlerFunc("RegionIDAndHandleReply", &processRegionIDAndHandle);
  182. }
  183. //static
  184. void LLLandmark::requestRegionHandle(LLMessageSystem* msg,
  185. const LLHost& upstream_host,
  186. const LLUUID& region_id,
  187. region_handle_callback_t callback)
  188. {
  189. if (region_id.isNull())
  190. {
  191. // Do not bother with checking...
  192. LL_DEBUGS("Landmark") << "Null region Id" << LL_ENDL;
  193. if (callback)
  194. {
  195. constexpr U64 U64_ZERO = 0;
  196. callback(region_id, U64_ZERO);
  197. }
  198. }
  199. else if (region_id == mLocalRegion.first)
  200. {
  201. LL_DEBUGS("Landmark") << "Local region" << LL_ENDL;
  202. if (callback)
  203. {
  204. callback(region_id, mLocalRegion.second);
  205. }
  206. }
  207. else
  208. {
  209. region_map_t::iterator it = mRegions.find(region_id);
  210. if (it == mRegions.end())
  211. {
  212. LL_DEBUGS("Landmark") << "Upstream region" << LL_ENDL;
  213. if (callback)
  214. {
  215. region_callback_map_t::value_type vt(region_id, callback);
  216. mRegionCallback.insert(vt);
  217. }
  218. LL_DEBUGS("Landmark") << "Landmark requesting information about: "
  219. << region_id << LL_ENDL;
  220. msg->newMessage("RegionHandleRequest");
  221. msg->nextBlock("RequestBlock");
  222. msg->addUUID("RegionID", region_id);
  223. msg->sendReliable(upstream_host);
  224. }
  225. else if (callback)
  226. {
  227. // We have the answer locally, just call the callback.
  228. LL_DEBUGS("Landmark") << "Cached upstream region" << LL_ENDL;
  229. callback(region_id, it->second.mRegionHandle);
  230. }
  231. }
  232. // As good a place as any to expire old entries.
  233. expireOldEntries();
  234. }
  235. //static
  236. void LLLandmark::setRegionHandle(const LLUUID& region_id, U64 region_handle)
  237. {
  238. mLocalRegion.first = region_id;
  239. mLocalRegion.second = region_handle;
  240. }
  241. //static
  242. void LLLandmark::processRegionIDAndHandle(LLMessageSystem* msg, void**)
  243. {
  244. LLUUID region_id;
  245. msg->getUUID("ReplyBlock", "RegionID", region_id);
  246. mRegions.erase(region_id);
  247. CacheInfo info;
  248. constexpr F32 CACHE_EXPIRY_SECONDS = 600.f; // 10 minutes
  249. info.mTimer.setTimerExpirySec(CACHE_EXPIRY_SECONDS);
  250. msg->getU64("ReplyBlock", "RegionHandle", info.mRegionHandle);
  251. mRegions[region_id] = info;
  252. #if LL_DEBUG
  253. U32 grid_x, grid_y;
  254. grid_from_region_handle(info.mRegionHandle, &grid_x, &grid_y);
  255. LL_DEBUGS("Landmark") << "Landmark got reply for region: " << region_id
  256. << " " << grid_x << "," << grid_y << LL_ENDL;
  257. #endif
  258. // Make all the callbacks here.
  259. region_callback_map_t::iterator it;
  260. while ((it = mRegionCallback.find(region_id)) != mRegionCallback.end())
  261. {
  262. it->second(region_id, info.mRegionHandle);
  263. mRegionCallback.erase(it);
  264. }
  265. }
  266. //static
  267. void LLLandmark::expireOldEntries()
  268. {
  269. for (region_map_t::iterator it = mRegions.begin(), end = mRegions.end();
  270. it != end; )
  271. {
  272. if (it->second.mTimer.hasExpired())
  273. {
  274. mRegions.hmap_erase(it++);
  275. }
  276. else
  277. {
  278. ++it;
  279. }
  280. }
  281. }