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; 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() { public int getTotalMegabytes() {
return totalBytes / (1024*1024); return totalBytes / (1024*1024);
} }
@ -159,7 +172,7 @@ public class Accountant {
} }
public int getBookedBytes() { public int getBookedBytes() {
return bookedMegabytes; return bookedMegabytes * 1000000;
} }
public int getTimeoutMinutes() { public int getTimeoutMinutes() {

View File

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

View File

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

View File

@ -269,6 +269,21 @@ public class Negotiator {
disagree_reason = R.string.err_client_minutes_not_acceptable_for_hotspot; 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 // SEND NegotiationAnswer
NegotiationOfferAnswer answer = new NegotiationOfferAnswer(agree, client_roaming_minutes, ownMacAddress); NegotiationOfferAnswer answer = new NegotiationOfferAnswer(agree, client_roaming_minutes, ownMacAddress);
PeerStore.getInstance().setLatestOfferAnswer(otherMac, answer); PeerStore.getInstance().setLatestOfferAnswer(otherMac, answer);
@ -321,6 +336,19 @@ public class Negotiator {
if (maxMinutes < 0) { if (maxMinutes < 0) {
return error(R.string.err_max_minutes_bad_setting, false); 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); NegotiationOffer offer = new NegotiationOffer(minMinutes, maxMinutes, iotaPerMegabyte, ownMacAddress);
String offerString = gson.toJson(offer); String offerString = gson.toJson(offer);

View File

@ -17,4 +17,6 @@
<string name="err_timeout">Timeout</string> <string name="err_timeout">Timeout</string>
<string name="err_io_exception">IO Exception</string> <string name="err_io_exception">IO Exception</string>
<string name="err_peer_quit">Peer quit the conversation</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> </resources>