status_t find_rel_time(const BMessage& msg, const char* name, int32 i, AmTime* timeVal) { double v; status_t res = msg.FindDouble(name, i, &v); if (res == B_OK) { *timeVal = bigtime_t(v*PPQN + .5); return res; } return find_time(msg, name, i, timeVal); }
//------------------------------------------------------------------------------ void TMessageEasyFindTest::MessageEasyFindTest1() { BRect r(0, 0, -1, -1); BPoint p(0, 0); BMessage msg; CPPUNIT_ASSERT(msg.FindRect("data") == r); CPPUNIT_ASSERT(msg.FindPoint("data") == p); CPPUNIT_ASSERT(msg.FindString("data") == NULL); CPPUNIT_ASSERT(msg.FindInt8("data") == 0); CPPUNIT_ASSERT(msg.FindInt16("data") == 0); CPPUNIT_ASSERT(msg.FindInt32("data") == 0); CPPUNIT_ASSERT(msg.FindInt64("data") == 0); CPPUNIT_ASSERT(msg.FindBool("data") == false); CPPUNIT_ASSERT(msg.FindFloat("data") == 0); CPPUNIT_ASSERT(msg.FindDouble("data") == 0); }
void JsonToMessageTest::TestArrayA() { BMessage message; BMessage subMessage; BString stringValue; double doubleValue; // ---------------------- BJson::Parse(JSON_SAMPLE_ARRAY_A_IN, message); // ---------------------- CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [0]", B_OK, message.FindString("0", &stringValue)); CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [0]", BString("1234"), stringValue); CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [1]", B_OK, message.FindDouble("1", &doubleValue)); CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [1]", 4567, doubleValue); CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2]", B_OK, message.FindMessage("2", &subMessage)); CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.0]", B_OK, subMessage.FindString("0", &stringValue)); CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [2.0]", BString("A"), stringValue); CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.1]", B_OK, subMessage.FindString("1", &stringValue)); CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [2.1]", BString("b"), stringValue); CPPUNIT_ASSERT_EQUAL_MESSAGE("!find [2.2]", B_OK, subMessage.FindString("2", &stringValue)); CPPUNIT_ASSERT_EQUAL_MESSAGE("!eq [2.2]", BString("C\xc3\xa9zanne"), stringValue); }
status_t BGeolocation::LocateSelf(float& latitude, float& longitude) { // Enumerate wifi network and build JSON message BNetworkRoster& roster = BNetworkRoster::Default(); uint32 interfaceCookie = 0; BNetworkInterface interface; BString query("{\n\t\"wifiAccessPoints\": ["); int32 count = 0; while (roster.GetNextInterface(&interfaceCookie, interface) == B_OK) { uint32 networkCookie = 0; wireless_network network; BNetworkDevice device(interface.Name()); // TODO is that the correct way to enumerate devices? while (device.GetNextNetwork(networkCookie, network) == B_OK) { if (count != 0) query += ','; count++; query += "\n\t\t{ \"macAddress\": \""; query += network.address.ToString().ToUpper(); query += "\", \"signalStrength\": "; query << (int)network.signal_strength; query += ", \"signalToNoiseRatio\": "; query << (int)network.noise_level; query += " }"; } } query += "\n\t]\n}\n"; // Check that we have enough data (we need at least 2 networks) if (count < 2) return B_DEVICE_NOT_FOUND; class GeolocationListener: public BUrlProtocolListener { public: virtual ~GeolocationListener() {}; void DataReceived(BUrlRequest*, const char* data, off_t position, ssize_t size) { result.WriteAt(position, data, size); } BMallocIO result; }; GeolocationListener listener; // Send Request (POST JSON message) BUrlRequest* request = BUrlProtocolRoster::MakeRequest(fService, &listener); if (request == NULL) return B_BAD_DATA; BHttpRequest* http = dynamic_cast<BHttpRequest*>(request); if (http == NULL) { delete request; return B_BAD_DATA; } http->SetMethod(B_HTTP_POST); BMemoryIO* io = new BMemoryIO(query.String(), query.Length()); http->AdoptInputData(io, query.Length()); status_t result = http->Run(); if (result < 0) { delete http; return result; } while (http->IsRunning()) snooze(10000); // Parse reply const BHttpResult& reply = (const BHttpResult&)http->Result(); if (reply.StatusCode() != 200) { delete http; return B_ERROR; } BMessage data; result = BJson::Parse(data, (char*)listener.result.Buffer()); delete http; if (result != B_OK) { return result; } BMessage location; result = data.FindMessage("location", &location); if (result != B_OK) return result; double lat, lon; result = location.FindDouble("lat", &lat); if (result == B_OK) result = location.FindDouble("lng", &lon); latitude = lat; longitude = lon; return result; }
void RatePackageWindow::_QueryRatingThread() { if (!Lock()) { fprintf(stderr, "rating query: Failed to lock window\n"); return; } PackageInfoRef package(fPackage); Unlock(); BAutolock locker(fModel.Lock()); BString username = fModel.Username(); locker.Unlock(); if (package.Get() == NULL) { fprintf(stderr, "rating query: No package\n"); _SetWorkerThread(-1); return; } WebAppInterface interface; BMessage info; const DepotInfo* depot = fModel.DepotForName(package->DepotName()); BString repositoryCode; if (depot != NULL) repositoryCode = depot->WebAppRepositoryCode(); if (repositoryCode.Length() == 0) { printf("unable to obtain the repository code for depot; %s\n", package->DepotName().String()); } else { status_t status = interface.RetrieveUserRating( package->Name(), package->Version(), package->Architecture(), repositoryCode, username, info); // info.PrintToStream(); BMessage result; if (status == B_OK && info.FindMessage("result", &result) == B_OK && Lock()) { result.FindString("code", &fRatingID); result.FindBool("active", &fRatingActive); BString comment; if (result.FindString("comment", &comment) == B_OK) { MarkupParser parser; fRatingText = parser.CreateDocumentFromMarkup(comment); fTextView->SetTextDocument(fRatingText); } if (result.FindString("userRatingStabilityCode", &fStability) == B_OK) { int32 index = 0; for (int32 i = fStabilityCodes.CountItems() - 1; i >= 0; i--) { const StabilityRating& stability = fStabilityCodes.ItemAtFast(i); if (stability.Name() == fStability) { index = i; break; } } BMenuItem* item = fStabilityField->Menu()->ItemAt(index); if (item != NULL) item->SetMarked(true); } if (result.FindString("naturalLanguageCode", &fCommentLanguage) == B_OK) { BMenuItem* item = fCommentLanguageField->Menu()->ItemAt( fModel.SupportedLanguages().IndexOf(fCommentLanguage)); if (item != NULL) item->SetMarked(true); } double rating; if (result.FindDouble("rating", &rating) == B_OK) { fRating = (float)rating; fSetRatingView->SetPermanentRating(fRating); } fRatingActiveCheckBox->SetValue(fRatingActive); fRatingActiveCheckBox->Show(); fSendButton->SetLabel(B_TRANSLATE("Update")); Unlock(); } else { fprintf(stderr, "rating query: Failed response: %s\n", strerror(status)); if (!info.IsEmpty()) info.PrintToStream(); } } _SetWorkerThread(-1); }