Skip to content

Commit

Permalink
Merge pull request google#541 from cco3/json
Browse files Browse the repository at this point in the history
Add basic framework for making requests to PWS
  • Loading branch information
cco3 committed Oct 13, 2015
2 parents 16e4a19 + 9b7dc94 commit d68f8e2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ public class PhysicalWebCollection {
private static final String METADATA_KEY = "metadata";
private static final String REQUESTURL_KEY = "requesturl";
private static final String SITEURL_KEY = "siteurl";
private static final String DEFAULT_PWS_ENDPOINT = "https://url-caster.appspot.com";
private PwsClient mPwsClient;
private Map<String, UrlDevice> mDeviceIdToUrlDeviceMap;
private Map<Class, UrlDeviceJsonSerializer> mUrlDeviceTypeToUrlDeviceJsonSerializer;
private Map<String, PwsResult> mBroadcastUrlToPwsResultMap;
private Set<String> mPendingBroadcastUrls;

/**
* Construct a PhysicalWebCollection.
*/
public PhysicalWebCollection() {
mPwsClient = new PwsClient(DEFAULT_PWS_ENDPOINT);
mDeviceIdToUrlDeviceMap = new HashMap<>();
mUrlDeviceTypeToUrlDeviceJsonSerializer = new HashMap<>();
mBroadcastUrlToPwsResultMap = new HashMap<>();
mPendingBroadcastUrls = new HashSet<>();
}

/**
Expand Down Expand Up @@ -249,4 +254,35 @@ public List<PwPair> getPwPairsSortedByRank() {

return ret;
}

/**
* Set the URL for making PWS requests.
* @param pwsEndpoint The new PWS endpoint.
*/
public void setPwsEndpoint(String pwsEndpoint) {
mPwsClient.setPwsEndpoint(pwsEndpoint);
}

/**
* Triggers an HTTP request to be made to the PWS.
* This method fetches a PwsResult for all broadcast URLs that do not have a
* PwsResult.
* @param pwsResultCallback The callback to run when we get an HTTPResponse.
*/
public void fetchPwsResults(PwsResultCallback pwsResultCallback) {
// Get new URLs to fetch
Set<String> newUrls = new HashSet<>();
for (UrlDevice urlDevice : mDeviceIdToUrlDeviceMap.values()) {
String url = urlDevice.getUrl();
if (!mPendingBroadcastUrls.contains(url)
&& !mBroadcastUrlToPwsResultMap.containsKey(url)) {
newUrls.add(url);
mPendingBroadcastUrls.add(url);
}
}

// TODO(cco3): Create augmented callback that removes the urls from
// mPendingBroadcastUrls.
mPwsClient.resolve(newUrls, pwsResultCallback);
}
}
49 changes: 49 additions & 0 deletions java/libs/src/main/java/org/physical_web/collection/PwsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.physical_web.collection;

import java.util.Collection;

/**
* HTTP client that makes requests to the Physical Web Service.
*/
public class PwsClient {
private String mPwsEndpoint;

/**
* Construct a PwsClient.
* @param pwsEndpoint The URL to send requests to.
*/
public PwsClient(String pwsEndpoint) {
setPwsEndpoint(pwsEndpoint);
}

/**
* Set the URL for making PWS requests.
* @param pwsEndpoint The new PWS endpoint.
*/
public void setPwsEndpoint(String pwsEndpoint) {
mPwsEndpoint = pwsEndpoint;
}

/**
* Send an HTTP request to the PWS to resolve a set of URLs.
* @param broadcastUrls The URLs to resolve.
*/
public void resolve(Collection broadcastUrls, PwsResultCallback pwsResultCallback) {
// TODO(cco3): Implement this
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.physical_web.collection;

/**
* Callback to implement for requests made to the Physical Web Service.
*/
public interface PwsResultCallback {
// TODO(cco3) Implement later
}

0 comments on commit d68f8e2

Please sign in to comment.