check balance before starting

shouldCloseChannel check
This commit is contained in:
Daniel Pollithy 2018-01-26 02:25:12 +01:00
parent 8d792bd2fc
commit 50cee71261
5 changed files with 77 additions and 17 deletions

View File

@ -138,6 +138,19 @@ public class Accountant {
return false;
}
public boolean shouldCloseChannel() {
if (getTotalBytes() >= getBookedBytes()) {
return true;
}
if ((Accountant.getInstance().getTotalDurance() / 60) >= Accountant.getInstance().getBookedMinutes()) {
return true;
}
if (Accountant.getInstance().getTotalIotaPrice() >= Accountant.getInstance().getTotalIotaDeposit()) {
return true;
}
return false;
}
public int getTotalMegabytes() {
return totalBytes / (1024*1024);
}
@ -159,7 +172,7 @@ public class Accountant {
}
public int getBookedBytes() {
return bookedMegabytes;
return bookedMegabytes * 1000000;
}
public int getTimeoutMinutes() {

View File

@ -5,6 +5,7 @@ import android.app.usage.NetworkStatsManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.TrafficStats;
import android.net.wifi.WifiManager;
import android.os.RemoteException;
import android.text.format.Formatter;
@ -114,6 +115,9 @@ public class BillingServer {
e.printStackTrace();
}
long start_bytes_received = TrafficStats.getTotalRxBytes();
long start_bytes_transmitted = TrafficStats.getTotalTxBytes();
int max_errors = 1;
int errors = 0;
@ -180,31 +184,43 @@ public class BillingServer {
// loop until roaming ends
int count = 0;
int billing_interval_seconds = 60;
while (state == State.ROAMING) {
// sleep 1 minute
Log.d(TAG, "start: I go to sleep for 1 minute!");
Thread.sleep(60 * 1000);
Thread.sleep(billing_interval_seconds * 1000);
Log.d(TAG, "start: Good morning!");
// create new bill
// ToDo: integrate real network data
NetworkStats.Bucket bucket;
long total_bytes;
try {
bucket = networkStatsManager.querySummaryForDevice(ConnectivityManager.TYPE_WIFI,
"",
Accountant.getInstance().getLastTime() * 1000,
System.currentTimeMillis());
long bytes_received = bucket.getRxBytes();
long bytes_transmitted = bucket.getTxBytes();
total_bytes = bytes_received + bytes_transmitted;
} catch (RemoteException e) {
Log.d(TAG, "start: Can't get the network traffic.");
total_bytes = 0;
}
NetworkStats networkStats;
long new_bytes_received = TrafficStats.getTotalRxBytes();
long new_bytes_transmitted = TrafficStats.getTotalTxBytes();
long bytes_received = new_bytes_received - start_bytes_received;
long bytes_transmitted = new_bytes_transmitted - start_bytes_transmitted;
start_bytes_received = new_bytes_received;
start_bytes_transmitted = new_bytes_transmitted;
total_bytes = bytes_received + bytes_transmitted;
Log.d(TAG, "Bytes Received" + bytes_received);
Log.d(TAG, "Bytes Transferred" + bytes_transmitted);
b = Accountant.getInstance().createBill((int)total_bytes);
// check if the resources are empty
boolean closeAfterwards = false;
if (Accountant.getInstance().shouldCloseChannel()) {
Log.d(TAG, "start: SHOULD CLOSE CHANNEL!");
closeAfterwards = true;
}
// ToDo: integrate real flash channel
latestBill = new BillMessage(b, "<flash obj>", Accountant.getInstance().isCloseAfterwards());
latestBill = new BillMessage(b, "<flash obj>", Accountant.getInstance().isCloseAfterwards() || closeAfterwards);
latestBillString = gson.toJson(latestBill);
socketWrapper.sendLine(latestBillString);

View File

@ -681,6 +681,7 @@ public class WiFiDirectBroadcastService extends Service {
}
public void startNegotiationServer(final boolean isClient, String macAddress, String peerMac) {
Runnable task = new Runnable() {
@Override

View File

@ -269,6 +269,21 @@ public class Negotiator {
disagree_reason = R.string.err_client_minutes_not_acceptable_for_hotspot;
}
// check consumer has enough iotas
SharedPreferences sharedPref = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
String defaultValue = "0";
String storedBalance = sharedPref.getString("balance", defaultValue);
int iotaBalance = Integer.parseInt(storedBalance);
Log.d(TAG, "runConsumerProtocol: MY IOTA BALANCE: " + iotaBalance);
// Assumption 1 MB per minute
if (iotaBalance < client_roaming_minutes * iotaPerMegabyte) {
Log.d(TAG, "runConsumerProtocol: Balance too low");
agree = false;
disagree_reason = R.string.err_client_not_enough_iota;
}
// SEND NegotiationAnswer
NegotiationOfferAnswer answer = new NegotiationOfferAnswer(agree, client_roaming_minutes, ownMacAddress);
PeerStore.getInstance().setLatestOfferAnswer(otherMac, answer);
@ -321,6 +336,19 @@ public class Negotiator {
if (maxMinutes < 0) {
return error(R.string.err_max_minutes_bad_setting, false);
}
// check consumer has enough iotas
SharedPreferences sharedPref = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
String defaultValue = "0";
String storedBalance = sharedPref.getString("balance", defaultValue);
int iotaBalance = Integer.parseInt(storedBalance);
// Assumption 1 MB per minute
if (iotaBalance < maxMinutes * iotaPerMegabyte) {
Log.d(TAG, "runHotspotProtocol: Balance too low");
return error(R.string.err_hotspot_not_enough_iota, false);
}
NegotiationOffer offer = new NegotiationOffer(minMinutes, maxMinutes, iotaPerMegabyte, ownMacAddress);
String offerString = gson.toJson(offer);

View File

@ -17,4 +17,6 @@
<string name="err_timeout">Timeout</string>
<string name="err_io_exception">IO Exception</string>
<string name="err_peer_quit">Peer quit the conversation</string>
<string name="err_client_not_enough_iota">Client doesn\'t have enough iota balance</string>
<string name="err_hotspot_not_enough_iota">Not enough iota balance</string>
</resources>