diff --git a/app/src/main/java/com/flashwifi/wifip2p/billing/Accountant.java b/app/src/main/java/com/flashwifi/wifip2p/billing/Accountant.java index e518379..f5ce704 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/billing/Accountant.java +++ b/app/src/main/java/com/flashwifi/wifip2p/billing/Accountant.java @@ -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() { diff --git a/app/src/main/java/com/flashwifi/wifip2p/billing/BillingServer.java b/app/src/main/java/com/flashwifi/wifip2p/billing/BillingServer.java index 3ee2bbe..618b97d 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/billing/BillingServer.java +++ b/app/src/main/java/com/flashwifi/wifip2p/billing/BillingServer.java @@ -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, "", Accountant.getInstance().isCloseAfterwards()); + latestBill = new BillMessage(b, "", Accountant.getInstance().isCloseAfterwards() || closeAfterwards); latestBillString = gson.toJson(latestBill); socketWrapper.sendLine(latestBillString); diff --git a/app/src/main/java/com/flashwifi/wifip2p/broadcast/WiFiDirectBroadcastService.java b/app/src/main/java/com/flashwifi/wifip2p/broadcast/WiFiDirectBroadcastService.java index a8f14fa..6c670ce 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/broadcast/WiFiDirectBroadcastService.java +++ b/app/src/main/java/com/flashwifi/wifip2p/broadcast/WiFiDirectBroadcastService.java @@ -681,6 +681,7 @@ public class WiFiDirectBroadcastService extends Service { } + public void startNegotiationServer(final boolean isClient, String macAddress, String peerMac) { Runnable task = new Runnable() { @Override diff --git a/app/src/main/java/com/flashwifi/wifip2p/negotiation/Negotiator.java b/app/src/main/java/com/flashwifi/wifip2p/negotiation/Negotiator.java index 36d05ad..0fbd523 100644 --- a/app/src/main/java/com/flashwifi/wifip2p/negotiation/Negotiator.java +++ b/app/src/main/java/com/flashwifi/wifip2p/negotiation/Negotiator.java @@ -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); diff --git a/app/src/main/res/values/errorCodes.xml b/app/src/main/res/values/errorCodes.xml index 863ccda..89f4365 100644 --- a/app/src/main/res/values/errorCodes.xml +++ b/app/src/main/res/values/errorCodes.xml @@ -17,4 +17,6 @@ Timeout IO Exception Peer quit the conversation + Client doesn\'t have enough iota balance + Not enough iota balance \ No newline at end of file