Skip to content

Commit

Permalink
Add offset/limit params to getList requests
Browse files Browse the repository at this point in the history
  • Loading branch information
srsudar committed Jul 2, 2017
1 parent 675ec9c commit 1bc3c80
Show file tree
Hide file tree
Showing 22 changed files with 710 additions and 123 deletions.
44 changes: 35 additions & 9 deletions chromeapp/app/polymer-ui/elements/cache-page-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/iron-icon/iron-icon.html">
<link rel="import" href="../../../bower_components/iron-flex-layout/iron-flex-layout-classes.html">
<link rel="import" href="../../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../../bower_components/paper-fab/paper-fab.html">
<link rel="import" href="../../../bower_components/paper-spinner/paper-spinner.html">
Expand All @@ -18,6 +19,13 @@

<dom-module id="cache-page-list">
<template>
<style include="iron-flex iron-flex-alignment"></style>
<style is="custom-style">
.flex-equal-around-justified {
@apply --layout-horizontal;
@apply --layout-around-justified;
}
</style>
<style>
:host {
display: block;
Expand All @@ -29,8 +37,8 @@
}

.cache-controls {
display: flex;
position: relative;
/* display: flex; */
/* position: relative; */
height: 80px;
}

Expand All @@ -51,18 +59,36 @@
margin: 16px;
}

.page-button {
margin-top: 16px;
}

.back-button {
padding-right: 16px;
}

.next-button {
padding-left: 16px;
}

</style>

<div id="control" class="cache-controls">
<div id="control" class="cache-controls container flex-horizontal">
<paper-spinner id="loading" class="ringer hide" active></paper-spinner>
<paper-fab icon="refresh" class="fab" on-tap="refresh"></paper-fab>

<paper-button raised>
<iron-icon icon="icons:chevron-left"></iron-icon>
</paper-button>
<paper-button raised>
<iron-icon icon="icons:chevron-right"></iron-icon>
</paper-button>
<div class="flexchild flex-equal-around-justified">

<paper-button class="page-button back-button" raised>
<iron-icon icon="icons:chevron-left"></iron-icon>
Prev
</paper-button>
<paper-button class="page-button next-button" raised>
Next
<iron-icon icon="icons:chevron-right"></iron-icon>
</paper-button>

</div>

<paper-toast
id="toasterror"
Expand Down
17 changes: 4 additions & 13 deletions chromeapp/app/scripts/app-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ exports.setAbsPathToBaseDir = function(absPath) {
ABS_PATH_TO_BASE_DIR = absPath;
};

/**
* @return {string} the URL for the list of pages in this device's own cache
*/
exports.getListUrlForSelf = function() {
let iface = exports.getListeningHttpInterface();
let host = iface.address;
let port = iface.port;
let result = serverApi.getListPageUrlForCache(host, port);
return result;
};

/**
* @return {Object} the cache object that represents this machine's own cache.
*/
Expand Down Expand Up @@ -402,18 +391,20 @@ exports.getAbsPathToBaseDir = function() {
*
* @param {string} serviceName the full <instance>.<type>.<domain> name of the
* service
* @param {number} offset
* @param {number} limit
*
* @return {Promise.<Object, Error>} Promise that resolves with the JSON
* response representing the list, or rejects with an Error
*/
exports.getListFromService = function(serviceName) {
exports.getListFromService = function(serviceName, offset, limit) {
return new Promise(function(resolve, reject) {
exports.resolveCache(serviceName)
.then(cacheInfo => {
let peerAccessor = peerIfMgr.getPeerAccessor(
cacheInfo.ipAddress, cacheInfo.port
);
return peerAccessor.getList();
return peerAccessor.getList(offset, limit);
})
.then(pageList => {
resolve(pageList);
Expand Down
2 changes: 1 addition & 1 deletion chromeapp/app/scripts/evaluation.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ exports.generateDummyPage = function(index, nonce) {

/**
* Generate a response mirroring the functionality of
* server-api.getResponseForAllCachedPages to be used for evaluation.
* server-api.getResponseForList to be used for evaluation.
*
* @param {integer} numPages the number of responses to return
* @param {string} nonce a string to incorporate into answers
Expand Down
7 changes: 5 additions & 2 deletions chromeapp/app/scripts/peer-interface/webrtc-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ class WebrtcPeerAccessor extends PeerAccessor {
/**
* Retrieve the list of pages in the peer's cache.
*
* @param {number} offset
* @param {number} limit
*
* @return {Promise.<Object, Error>}
*/
getList() {
getList(offset, limit) {
return this.getConnection()
.then(peerConnection => {
return peerConnection.getList();
return peerConnection.getList(offset, limit);
});
}

Expand Down
12 changes: 12 additions & 0 deletions chromeapp/app/scripts/persistence/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ exports.getCachedPageSummaries = function(offset, numDesired) {
return database.getCachedPageSummaries(offset, numDesired);
};

/**
* Get the number of pages saved in the cache.
*
* @return {number}
*/
exports.getNumCachedPages = function() {
return database.getAllCPInfos()
.then(cpinfos => {
return cpinfos.length;
});
};


/**
* Get all the cached pages that are stored in the cache.
Expand Down
2 changes: 1 addition & 1 deletion chromeapp/app/scripts/server/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.ListCachedPagesHandler = function() {
_.extend(exports.ListCachedPagesHandler.prototype,
{
get: function() {
api.getResponseForAllCachedPages()
api.getResponseForList()
.then(response => {
this.setHeader('content-type', 'text/json');
let encoder = new TextEncoder('utf-8');
Expand Down
44 changes: 40 additions & 4 deletions chromeapp/app/scripts/server/server-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,56 @@ exports.getCachedPageHrefFromPath = function(path) {
/**
* Return a JSON object response for the all cached pages endpoint.
*
* @param {number} offset
* @param {number} limit
*
* @return {Promise.<Buffer, Error} Promise that resolves with Buffer from an
* object like the following:
* {
* metadata: {},
* cachedPages: [CPSummary, CPSummary]
* hasPrev: false,
* hasNext: false,
* prevOffset: 0,
* nextOffset: 20,
* cachedPages: [CPSummary.toJSON(), CPSummary.toJSON()]
* }
*/
exports.getResponseForAllCachedPages = function() {
exports.getResponseForList = function(offset, limit) {
if (!Number.isSafeInteger(offset)) {
offset = DEFAULT_OFFSET;
}
if (!Number.isSafeInteger(limit)) {
limit = DEFAULT_LIMIT;
}
return new Promise(function(resolve, reject) {
datastore.getCachedPageSummaries(DEFAULT_OFFSET, DEFAULT_LIMIT)
let result = {};
datastore.getCachedPageSummaries(offset, limit)
.then(cpsums => {
let result = {};
result.metadata = exports.createMetadatObj();

let hasPrev = false;
if (offset > 0) {
hasPrev = true;
let prevOffset = offset - limit;
if (prevOffset < 0) {
prevOffset = 0;
}
result.prevOffset = prevOffset;
}
result.hasPrev = hasPrev;

result.cachedPages = cpsums.map(cpsum => cpsum.toJSON());
return datastore.getNumCachedPages();
})
.then(numPages => {
let hasNext = false;
if (numPages > offset + limit) {
hasNext = true;
let nextOffset = offset + limit;
result.nextOffset = nextOffset;
}
result.hasNext = hasNext;

resolve(Buffer.from(JSON.stringify(result)));
})
.catch(err => {
Expand Down
10 changes: 8 additions & 2 deletions chromeapp/app/scripts/webrtc/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ exports.createChannelName = function() {
};

/**
* @param {number} offset
* @param {number} limit
*
* @return {Object}
*/
exports.createListMessage = function() {
return exports.createMessage(exports.TYPE_LIST);
exports.createListMessage = function(offset, limit) {
let request = { offset, limit };
let result = exports.createMessage(exports.TYPE_LIST);
result.request = request;
return result;
};

/**
Expand Down
7 changes: 5 additions & 2 deletions chromeapp/app/scripts/webrtc/peer-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ class PeerConnection extends EventEmitter {
/**
* Get the list of available files from the peer.
*
* @param {number} offset
* @param {number} limit
*
* @return {Promise.<Object, Error>} Promise that resolves with the JSON list
* of the directory contents
*/
getList() {
getList(offset, limit) {
let self = this;
return new Promise(function(resolve, reject) {
let msg = message.createListMessage();
let msg = message.createListMessage(offset, limit);

self.sendAndGetResponse(msg)
.then(buff => {
Expand Down
6 changes: 4 additions & 2 deletions chromeapp/app/scripts/webrtc/responder.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ exports.onDataChannelMessageHandler = function(channel, event) {
*
* @param {RTCDataChannel} channel the data channel on which to send the
* response
* @param {Object} message
*
* @return {Promise.<undefined, Error>} Promise that returns after sending has
* begun.
*/
exports.onList = function(channel) {
exports.onList = function(channel, message) {
return new Promise(function(resolve, reject) {
serverApi.getResponseForAllCachedPages()
let { offset, limit } = message.request;
serverApi.getResponseForList(offset, limit)
.then(buff => {
return exports.sendBufferOverChannel(channel, buff);
})
Expand Down
Loading

0 comments on commit 1bc3c80

Please sign in to comment.