Initial pseudo draft of the billing protocol

This commit is contained in:
Daniel Pollithy 2018-01-14 00:50:20 +01:00
parent b430391d7f
commit e05c07b93b
6 changed files with 260 additions and 1 deletions

View File

@ -24,7 +24,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -0,0 +1,88 @@
package com.flashwifi.wifip2p.billing;
import java.util.ArrayList;
public class Accountant {
private static final Accountant ourInstance = new Accountant();
private ArrayList<Bill> bills;
private int totalMegabytes;
private int totalIotaPrice;
private int totalDurance;
private int bookedMegabytes;
private int timeoutMinutes;
private FlashChannelHelper flashChannelHelper;
private boolean closed = true;
static Accountant getInstance() {
return ourInstance;
}
private Accountant() {
}
public void start(int bookedMegabytes, int timeoutMinutes){
if (closed) {
bills = new ArrayList<Bill>();
this.bookedMegabytes = bookedMegabytes;
this.timeoutMinutes = timeoutMinutes;
totalMegabytes = 0;
totalIotaPrice = 0;
totalDurance = 0;
flashChannelHelper = new FlashChannelHelper();
closed = false;
}
}
public int getNextBillNumber() {
return bills.size() + 1;
}
private void appendBill(Bill b) {
bills.add(b);
}
private void applyTransferToFlashChannel(int iota) {
}
public Bill createBill(int megaByte, int priceInIota, int duranceMinutes){
if (!closed) {
Bill b = new Bill(getNextBillNumber(), totalDurance, duranceMinutes, megaByte, priceInIota);
totalMegabytes += megaByte;
totalIotaPrice += priceInIota;
totalDurance += duranceMinutes;
// 1) modify flash channel
applyTransferToFlashChannel(priceInIota);
appendBill(b);
return b;
}
return null;
}
public void close() {
if (!closed) {
closed = true;
}
}
public void markAcceptance(int index, boolean accepted) {
bills.get(index).setAcceptedByPeer(accepted);
}
public boolean billsNotAccepted() {
for (Bill b: bills) {
if (!b.isAcceptedByPeer()) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,28 @@
package com.flashwifi.wifip2p.billing;
public class Bill {
private int index;
private int minuteStart;
private int duranceInMinutes = 1;
private int megabytesUsed;
private int priceInIota;
private boolean acceptedByPeer;
public Bill(int index, int minuteStart, int duranceInMinutes, int megabytesUsed, int priceInIota) {
this.index = index;
this.minuteStart = minuteStart;
this.duranceInMinutes = duranceInMinutes;
this.megabytesUsed = megabytesUsed;
this.priceInIota = priceInIota;
}
public boolean isAcceptedByPeer() {
return acceptedByPeer;
}
public void setAcceptedByPeer(boolean acceptedByPeer) {
this.acceptedByPeer = acceptedByPeer;
}
}

View File

@ -0,0 +1,23 @@
package com.flashwifi.wifip2p.billing;
/**
* 1) This class keeps the socket connection alive.
* 2) It tracks the state of the communication.
* 3) It receives new bills and flash objects sent by the server
* 4) It approves the changes
* 5) It watches the connectivity (no internet -> no money)
*/
public class BillingClient {
private State state = State.NOT_PAIRED;
public BillingClient(){}
enum State {
NOT_PAIRED,
INITIAL,
ROAMING,
CLOSED,
FULLY_ATTACHED
}
}

View File

@ -0,0 +1,60 @@
package com.flashwifi.wifip2p.billing;
/**
* 1) This class keeps the socket connection alive.
* 2) It tracks the state of the communication.
* 3) It triggers the creation of bills
* 4) It sends the bills and flash objects to the peer
* 5) It watches the time and deadlines
* 6)
*/
public class BillingServer {
private State state = State.NOT_PAIRED;
public BillingServer(int bookedMegabytes, int timeoutMinutes){
Accountant.getInstance().start(bookedMegabytes,timeoutMinutes);
}
public void start() {
// 0) create deadline guard
createDeadlineGuard();
// 1) create a socket
while (state != State.ERROR || state != State.FULLY_ATTACHED) {
// 2) accept a connection
if (state == State.NOT_PAIRED) {
// 3) pair client and hotspot and synchronize states
}
while (state == State.ROAMING) {
// 4) sleep(60)
// 5) createNewBill
// 6) send the bill and the signed flash object
}
if (state == State.CLOSED) {
// 7) close the channel and sign the final settlement
// 8) receive the signed settlement from client
}
while (state == State.CLOSED) {
// 9) reattach and rebroadcast the final transaction
}
if (state == State.FULLY_ATTACHED) {
// DONE!
}
}
}
private void createDeadlineGuard() {
// this method measures the time and stops the connection if
// nothing happened after <timeoutMinutes>
}
enum State {
NOT_PAIRED,
INITIAL,
ROAMING,
CLOSED,
FULLY_ATTACHED,
ERROR
}
}

View File

@ -0,0 +1,60 @@
package com.flashwifi.wifip2p.billing;
/**
* This class takes care of flash channels and their states on the tangle.
* It starts new threads which check the iota APIs and takes wrape the
* flash channel
*/
public class FlashChannelHelper {
// ToDo: replace this by real flash channel
private String flashChannel;
private boolean flashChannelRootAttached;
private boolean flashChannelFundedByHotspot;
private boolean flashChannelFundedByConsumer;
private boolean flashChannelSettlementAttached;
public FlashChannelHelper() {
flashChannelRootAttached = false;
flashChannelFundedByHotspot = false;
flashChannelFundedByConsumer = false;
flashChannelSettlementAttached = false;
}
public boolean checkRootAttached() {
if (flashChannelRootAttached) {
return true;
} else {
// ToDo: make an API call to check this
return false;
}
}
public boolean checkFundedByHotspot() {
if (flashChannelFundedByHotspot) {
return true;
} else {
// ToDo: make an API call to check this
return false;
}
}
public boolean checkFundedByConsumer() {
if (flashChannelFundedByConsumer) {
return true;
} else {
// ToDo: make an API call to check this
return false;
}
}
public boolean checkSettlementAttached() {
if (flashChannelSettlementAttached) {
return true;
} else {
// ToDo: make an API call to check this
return false;
}
}
}