Skip to content

Commit

Permalink
Auction functionality added
Browse files Browse the repository at this point in the history
  • Loading branch information
GobinathAL committed Apr 3, 2021
1 parent badb8d8 commit bb9ef69
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 13 deletions.
139 changes: 135 additions & 4 deletions app/src/main/java/com/network/p2pauction/GroupFormation.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textview.MaterialTextView;

import java.io.BufferedReader;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.acl.Group;
import java.text.Normalizer;
Expand All @@ -45,6 +53,7 @@
public class GroupFormation extends AppCompatActivity {
TextView auctionName, noOfBidders;
ListView itemsList;
MaterialButton startButton;
WifiP2pManager manager;
WifiP2pManager.Channel channel;
NsdManager.RegistrationListener registrationListener;
Expand All @@ -53,6 +62,10 @@ public class GroupFormation extends AppCompatActivity {
String ip;
ArrayList<String> clientIpAddress;
NsdManager nsdManager;
MaterialTextView txtName, txtPrice, txtHighest;
RelativeLayout relativeLayout;
ArrayList<String> itemArrList;
private int currentItem = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -62,9 +75,14 @@ protected void onCreate(Bundle savedInstanceState) {
auctionName.setText(AuctionCatalogue.AUCTION_NAME);
noOfBidders = (TextView) findViewById(R.id.NoOfBidders_OwnerSide);
itemsList = (ListView) findViewById(R.id.ItemsList_OwnerSide);
startButton = (MaterialButton) findViewById(R.id.StartButton);
relativeLayout = (RelativeLayout) findViewById(R.id.ItemDisplay_OwnerSide);
txtName = (MaterialTextView) findViewById(R.id.ItemName_OwnerSide);
txtPrice = (MaterialTextView) findViewById(R.id.StartingPrice_OwnerSide);
txtHighest = (MaterialTextView) findViewById(R.id.HighestBid_OwnerSide);
String[] itemList = AuctionCatalogue.AUCTION_CATALOGUE.split(",");
ArrayList<String> arr = new ArrayList<String>(Arrays.asList(itemList));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arr) {
itemArrList = new ArrayList<String>(Arrays.asList(itemList));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, itemArrList) {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Expand All @@ -75,6 +93,7 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
}
};
itemsList.setAdapter(adapter);

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
ip = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
Thread myThread = new Thread(new MyServer());
Expand All @@ -85,6 +104,29 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
} catch (UnknownHostException e) {
e.printStackTrace();
}
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(startButton.getText().equals("Start auction")) {
Log.i("module4", "start button pressed");
Log.i("module4", "thread stopped");
Thread auctionThread = new Thread(new AuctionServer());
auctionThread.start();
Log.i("module4", "calling start");
new BackgroundTask().execute("start");
startButton.setText("Next");
Log.i("module4", "Button text changed");
}
else if(startButton.getText().equals("Next")){
announceResult();
currentItem++;
if(currentItem == itemArrList.size() - 1) {
startButton.setText("Finish");
}
}
updateItemInfo();
}
});
}

private void initializeRegistrationListener() {
Expand Down Expand Up @@ -165,7 +207,7 @@ public void run() {
class BackgroundTask extends AsyncTask<String, Void, String> {
Socket s;
DataOutputStream dataOutputStream;
String message = "Acknowledged";
String message;
@Override
protected String doInBackground(String... strings) {
String command = strings[0];
Expand All @@ -184,7 +226,7 @@ protected String doInBackground(String... strings) {
e.printStackTrace();
}
}
else if(command.contains("noOfBidders")) {
else if(command.contains("noOfBidders") || command.contains("start")) {
for(String sendip : clientIpAddress) {
try {
Socket s = new Socket(sendip, 5825);
Expand All @@ -204,6 +246,95 @@ else if(command.contains("noOfBidders")) {
return null;
}
}
class AuctionServer implements Runnable {

ServerSocket ss;
DataInputStream dataInputStream;
Socket mySocket;;
BufferedReader bufferedReader;
Handler handler = new Handler();
String message;
@Override
public void run() {
try {
ss = new ServerSocket(5826);
while (true) {
mySocket = ss.accept();
dataInputStream = new DataInputStream(mySocket.getInputStream());
message = dataInputStream.readUTF();
Log.i("module4", "received " + message);
handler.post(new Runnable() {
@Override
public void run() {
new BroadcastTask().execute(message);
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class BroadcastTask extends AsyncTask<String, Void, String> {
Socket s;
DataOutputStream dataOutputStream;
String message;
@Override
protected String doInBackground(String... strings) {
if(strings.length == 1 && strings[0].contains("result")) {
for(String sendip : clientIpAddress) {
try {
Socket s = new Socket(sendip, 5826);
dataOutputStream = new DataOutputStream(s.getOutputStream());
dataOutputStream.writeUTF(strings[0]);
dataOutputStream.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
else if(strings[0].contains("update")) {
String[] splitString = strings[0].split(" ");
Log.i("module4", "splitted the received msg");
if(currentItem == Integer.parseInt(splitString[1])) {
Log.i("module4", "updating txtHighest with " + splitString[2]);
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
txtHighest.setText("Highest Bid: " + splitString[2]);
}
});
for(String sendip : clientIpAddress) {
try {
Socket s = new Socket(sendip, 5826);
dataOutputStream = new DataOutputStream(s.getOutputStream());
dataOutputStream.writeUTF(strings[0]);
dataOutputStream.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
}
private void announceResult() {
new BroadcastTask().execute("result");
}
private void updateItemInfo() {
relativeLayout.setVisibility(View.VISIBLE);
String[] itemInfo = itemArrList.get(currentItem).split(" ");
txtName.setText("Name: " + itemInfo[0]);
txtPrice.setText("Starting Price: " + itemInfo[1]);
txtHighest.setText("Highest Bid: " + "0");
}
@Override
protected void onDestroy() {
nsdManager.unregisterService(registrationListener);
Expand Down
112 changes: 107 additions & 5 deletions app/src/main/java/com/network/p2pauction/GroupJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textview.MaterialTextView;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -41,10 +47,15 @@ public class GroupJoin extends AppCompatActivity {
TextView auctionName, noOfBidders;
ListView itemsList;
MaterialButton btnSend;
MaterialTextView txtName, txtPrice, txtHighest;
TextInputEditText txtBidAmount;
NsdManager.DiscoveryListener discoveryListener;
NsdManager nsdManager;
NsdServiceInfo mService;
ArrayList<String> itemArrList;
String ip, myip;
RelativeLayout relativeLayout;
private int currentItem = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -53,6 +64,11 @@ protected void onCreate(Bundle savedInstanceState) {
noOfBidders = (TextView) findViewById(R.id.NoOfBidders);
itemsList = (ListView) findViewById(R.id.ItemsList);
btnSend = (MaterialButton) findViewById(R.id.sendToSocketBtn);
relativeLayout = (RelativeLayout) findViewById(R.id.ItemDisplay);
txtName = (MaterialTextView) findViewById(R.id.ItemName);
txtPrice = (MaterialTextView) findViewById(R.id.StartingPrice);
txtHighest = (MaterialTextView) findViewById(R.id.HighestBid);
txtBidAmount = (TextInputEditText) findViewById(R.id.BidAmount);
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
myip = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
Thread myThread = new Thread(new MyServer());
Expand Down Expand Up @@ -126,10 +142,26 @@ public void onServiceLost(NsdServiceInfo serviceInfo) {
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BackgroundTask b = new BackgroundTask();
b.execute();
Log.i("module2", "background task executed");
btnSend.setVisibility(View.GONE);
if(btnSend.getText().toString().toLowerCase().contains("join")) {
BackgroundTask b = new BackgroundTask();
b.execute();
Log.i("module2", "background task executed");
btnSend.setVisibility(View.GONE);
}
else if(btnSend.getText().toString().toLowerCase().contains("bid")) {

String bidAmount = txtBidAmount.getText().toString();
Log.i("module4", "bid amound: " + bidAmount);
if(Integer.parseInt(bidAmount) > Integer.parseInt(txtHighest.getText().toString().replace("Highest Bid: ", ""))
&& Integer.parseInt(bidAmount) > Integer.parseInt(txtPrice.getText().toString().replace("Starting Price: ", ""))) {
Log.i("module4","calling broadcast");
new BroadcastTask().execute("update " + currentItem + " " + bidAmount);
Log.i("module4", "broadcasted");
}
else {
Toast.makeText(getApplicationContext(), "Enter higher amount", Toast.LENGTH_SHORT).show();
}
}
}
});
}
Expand Down Expand Up @@ -160,6 +192,27 @@ protected String doInBackground(String... strings) {
return null;
}
}
class BroadcastTask extends AsyncTask<String, Void, String> {
Socket s;
DataOutputStream dataOutputStream;
@Override
protected String doInBackground(String... strings) {
try {
s = new Socket(ip, 5826);
dataOutputStream = new DataOutputStream(s.getOutputStream());
dataOutputStream.writeUTF(strings[0]);
Log.i("module4", "message written");
dataOutputStream.close();
s.close();

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
class MyServer implements Runnable {
ServerSocket ss;
Socket mySocket;
Expand All @@ -185,7 +238,7 @@ public void run() {
AUCTION_NAME = auctionArr[0];
auctionName.setText(AUCTION_NAME);
String[] itemArr = auctionArr[1].split(",");
ArrayList<String> itemArrList = new ArrayList<String>(Arrays.asList(itemArr));
itemArrList = new ArrayList<String>(Arrays.asList(itemArr));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, itemArrList) {
@NonNull
@Override
Expand All @@ -202,6 +255,14 @@ else if(message.contains("noOfBidders")) {
String extract = message.substring(12);
noOfBidders.setText("Bidders: " + extract);
}
else if(message.contains("start")) {
Thread auctionThread = new Thread(new AuctionServer());
auctionThread.start();
relativeLayout.setVisibility(View.VISIBLE);
btnSend.setText("Bid");
btnSend.setVisibility(View.VISIBLE);
updateItemInfo();
}
}
});
}
Expand All @@ -210,6 +271,47 @@ else if(message.contains("noOfBidders")) {
}
}
}
class AuctionServer implements Runnable {
ServerSocket ss;
Socket mySocket;
DataInputStream dataInputStream;
BufferedReader bufferedReader;
Handler handler = new Handler();
@Override
public void run() {
try {
ss = new ServerSocket(5826);
while(true) {
mySocket = ss.accept();
dataInputStream = new DataInputStream(mySocket.getInputStream());
String message = dataInputStream.readUTF();
handler.post(new Runnable() {
@Override
public void run() {
if(message.contains("result")) {
currentItem++;
updateItemInfo();
}
else if(message.contains("update")) {
String[] splitMessage = message.split(" ");
if(currentItem == Integer.parseInt(splitMessage[1])) {
txtHighest.setText("Highest Bid: " + splitMessage[2]);
}
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void updateItemInfo() {
String[] itemInfo = itemArrList.get(currentItem).split(" ");
txtName.setText("Name: " + itemInfo[0]);
txtPrice.setText("Starting Price: " + itemInfo[1]);
txtHighest.setText("Highest Bid: " + "0");
}
@Override
protected void onDestroy() {
nsdManager.stopServiceDiscovery(discoveryListener);
Expand Down
Loading

0 comments on commit bb9ef69

Please sign in to comment.