diff --git a/java/org/servalproject/servaldna/ServalDClient.java b/java/org/servalproject/servaldna/ServalDClient.java index 244bf6eb..f4e6dbbb 100644 --- a/java/org/servalproject/servaldna/ServalDClient.java +++ b/java/org/servalproject/servaldna/ServalDClient.java @@ -24,6 +24,8 @@ import org.servalproject.codec.Base64; import org.servalproject.servaldna.keyring.KeyringCommon; import org.servalproject.servaldna.keyring.KeyringIdentity; import org.servalproject.servaldna.keyring.KeyringIdentityList; +import org.servalproject.servaldna.meshmb.MeshMBCommon; +import org.servalproject.servaldna.meshmb.MessagePlyList; import org.servalproject.servaldna.meshms.MeshMSCommon; import org.servalproject.servaldna.meshms.MeshMSConversationList; import org.servalproject.servaldna.meshms.MeshMSException; @@ -183,6 +185,20 @@ public class ServalDClient implements ServalDHttpConnectionFactory { return MeshMSCommon.advanceReadOffset(this, sid1, sid2, offset); } + public int meshmbSendMessage(SigningKey id, String text) throws IOException, ServalDInterfaceException { + return MeshMBCommon.sendMessage(this, id, text); + } + + public MessagePlyList meshmbListMessages(SigningKey id) throws IOException, ServalDInterfaceException { + return meshmbListMessagesSince(id, null); + } + + public MessagePlyList meshmbListMessagesSince(SigningKey id, String token) throws IOException, ServalDInterfaceException { + MessagePlyList list = new MessagePlyList(this, id, token); + list.connect(); + return list; + } + // interface ServalDHttpConnectionFactory public HttpURLConnection newServalDHttpConnection(String path) throws ServalDInterfaceException, IOException { diff --git a/java/org/servalproject/servaldna/meshmb/MeshMBCommon.java b/java/org/servalproject/servaldna/meshmb/MeshMBCommon.java new file mode 100644 index 00000000..fda52ce3 --- /dev/null +++ b/java/org/servalproject/servaldna/meshmb/MeshMBCommon.java @@ -0,0 +1,29 @@ +package org.servalproject.servaldna.meshmb; + +import org.servalproject.servaldna.PostHelper; +import org.servalproject.servaldna.ServalDHttpConnectionFactory; +import org.servalproject.servaldna.ServalDInterfaceException; +import org.servalproject.servaldna.SigningKey; + +import java.io.IOException; +import java.net.HttpURLConnection; + +/** + * Created by jeremy on 5/10/16. + */ +public class MeshMBCommon { + + public static int sendMessage(ServalDHttpConnectionFactory connector, SigningKey id, String text) throws IOException, ServalDInterfaceException + { + HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshmb/" + id.toHex() + "/sendmessage"); + PostHelper helper = new PostHelper(conn); + helper.connect(); + helper.writeField("message", text); + helper.close(); + + // TODO handle specific errors + return conn.getResponseCode(); + } + + +} diff --git a/java/org/servalproject/servaldna/meshmb/MessagePlyList.java b/java/org/servalproject/servaldna/meshmb/MessagePlyList.java new file mode 100644 index 00000000..67fc49e1 --- /dev/null +++ b/java/org/servalproject/servaldna/meshmb/MessagePlyList.java @@ -0,0 +1,56 @@ +package org.servalproject.servaldna.meshmb; + +import org.servalproject.json.JSONTableScanner; +import org.servalproject.json.JSONTokeniser; +import org.servalproject.servaldna.AbstractJsonList; +import org.servalproject.servaldna.ServalDHttpConnectionFactory; +import org.servalproject.servaldna.ServalDInterfaceException; +import org.servalproject.servaldna.SigningKey; + +import java.io.IOException; +import java.util.Map; + +/** + * Created by jeremy on 10/10/16. + */ +public class MessagePlyList extends AbstractJsonList { + private final SigningKey bundleId; + private final String sinceToken; + + public MessagePlyList(ServalDHttpConnectionFactory httpConnector, SigningKey bundleId, String sinceToken){ + super(httpConnector, new JSONTableScanner() + .addColumn("offset", Long.class) + .addColumn("token", String.class) + .addColumn("text", String.class) + .addColumn("timestamp", Long.class, JSONTokeniser.Narrow.ALLOW_NULL)); + this.bundleId = bundleId; + this.sinceToken = sinceToken; + } + + @Override + protected void handleResponseError() throws IOException, ServalDInterfaceException { + // TODO handle specific errors + super.handleResponseError(); + } + + @Override + protected String getUrl() { + if (this.sinceToken == null) + return "/restful/meshmb/" + bundleId.toHex() + "/messagelist.json"; + else if(this.sinceToken.equals("")) + return "/restful/meshmb/" + bundleId.toHex() + "/newsince/messagelist.json"; + else + return "/restful/meshmb/" + bundleId.toHex() + "/newsince/" + sinceToken + "/messagelist.json"; + } + + @Override + protected PlyMessage factory(Map row, long rowCount) { + return new PlyMessage( + rowCount, + (Long)row.get("offset"), + (String)row.get("token"), + (Long)row.get("timestamp"), + (String)row.get("text") + ); + } +} diff --git a/java/org/servalproject/servaldna/meshmb/PlyMessage.java b/java/org/servalproject/servaldna/meshmb/PlyMessage.java new file mode 100644 index 00000000..fa89a3cf --- /dev/null +++ b/java/org/servalproject/servaldna/meshmb/PlyMessage.java @@ -0,0 +1,20 @@ +package org.servalproject.servaldna.meshmb; + +/** + * Created by jeremy on 10/10/16. + */ +public class PlyMessage { + + public final long _row; + public final long offset; + public final String token; + public final long timestamp; + public final String text; + public PlyMessage(long _row, long offset, String token, long timestamp, String text){ + this._row = _row; + this.offset = offset; + this.token = token; + this.timestamp = timestamp; + this.text = text; + } +} diff --git a/meshmb_restful.c b/meshmb_restful.c index 0a4e01b2..06422eea 100644 --- a/meshmb_restful.c +++ b/meshmb_restful.c @@ -130,11 +130,13 @@ static int strn_to_position_token(const char *str, uint64_t *position, const cha size_t token_len = base64url_decode(token, sizeof token, str, 0, afterp, 0, NULL); int unpacked; - if ((unpacked = unpack_uint(token, token_len, position))==-1){ + if ((unpacked = unpack_uint(token, token_len, position))!=-1 + && **afterp=='/'){ + (*afterp)++; + } else { *position = 0; *afterp=str; - }else - (*afterp)++; + } return 1; }