-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuctionImpl.java
More file actions
180 lines (154 loc) · 6.11 KB
/
AuctionImpl.java
File metadata and controls
180 lines (154 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package AuctioningSystem;
/*
Code: AuctionImpl remote object Auctionimpl.java
Contains the arithmetic methods that can be remotley invoked
*/
import java.util.*;
import java.io.*;
import java.rmi.RemoteException;
import org.jgroups.JChannel;
import org.jgroups.blocks.RpcDispatcher;
import org.jgroups.util.Util;
import org.jgroups.Address;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
// The implementation Class must implement the rmi interface (Auction)
// and be set as a Remote object on a server
public class AuctionImpl extends ReceiverAdapter implements SellerInterface, BuyingInterface {
private Map<Integer, Auction> auctions;
private JChannel channel;
private RpcDispatcher disp;
private List<Address> members =new LinkedList<Address>();
private View current_view;
// Implementations must have an explicit constructor
// in order to declare the RemoteException exception
public AuctionImpl()
throws Exception {
super();
auctions = new HashMap<Integer, Auction>(); //Hashmap that holds all auctions
channel=new JChannel();
channel.setReceiver(this);
channel.connect("AuctionCluster");
this.disp = new RpcDispatcher(channel, this, this, this);
this.channel.setDiscardOwnMessages(true);
}
@Override
public void receive(Message message){
System.out.println(" yooo" + message);
}
@Override
public void viewAccepted(View view){
members = view.getMembers();
}
@Override
public void getState(OutputStream output) throws Exception{
}
@Override
public void setState(InputStream input) throws Exception{
}
public Collection<Auction> listAllAuctions()
throws java.rmi.RemoteException{
return new ArrayList(auctions.values()); //Returns a list of all currently active auctions
}
public User getUser(int uid){
User user = null;
try{
FileInputStream fis = new FileInputStream("user" + uid + ".ser");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println(fis);
user = (User)ois.readObject();
ois.close();
} catch(Exception e){
System.out.println("YO" + e.toString());
}
return user;
}
public int bid(int auctionId, Float bid, int uid)
throws java.rmi.RemoteException{
if(auctions.containsKey(auctionId)){
Auction auction = auctions.get(auctionId); //returns the auction depending on ID
User bidder = getUser(uid);
if(auction.getSellerID() == uid){ //if seller is bidder
return 3;
}
else{
int outcome = auction.setHighestBid(bid, bidder);
return outcome; //0 - too low, 1 - success.
}
}
else{
return 2; //this auction is no longer active
}
}
//Method that creates a new auction and returns the ID
public int newAuction(Item item)
throws java.rmi.RemoteException{
Auction a = new Auction(item); //creates an auction
a.setId(auctions.size() + 1); //sets the ID
auctions.put(auctions.size()+1, a); //puts auction on hashmap
System.out.println("Auction | ID");
System.out.println(auctions.values() + " | " + a.getId());
return a.getId();
}
public int removeItem(int auctionId)
throws java.rmi.RemoteException{
if(auctions.containsKey(auctionId)){
auctions.remove(auctionId);
return 1;
}
else{
System.out.println("Sorry, this item is not up for auction");
return 0;
}
}
//Method that returns highestbid
public Float getHighestBid(int auctionId)
throws java.rmi.RemoteException{
Float bid;
if(auctions.containsKey(auctionId)){
Auction auction = auctions.get(auctionId);
bid = auction.getHighestBid();
return bid;
}
else{
bid = new Float(0.00);
return bid;
}
}
//Method that takes the id and returns a string depending on the state the auction is at when its closed.
public String closeAuction(int auctionId, int userID)
throws java.rmi.RemoteException{
String message;
if(auctions.containsKey(auctionId)){
Auction auction = auctions.get(auctionId);
if(auction.getSellerID() == userID){
if(auction.getHighestBid() < auction.getItem().getReservePrice()){
message = "The reserve price set by the seller was not reached in this auction. Therefore there is no winner.";
} else{
message ="The winner of this auction is " + auction.getHighestBidder().getName() + ". Email: " + auction.getHighestBidder().getEmail();
}
auctions.remove(auctionId);
return message;
}
else{
return "Sorry, you don't have permission to close this auction";
}
}
else{
return "Sorry, this auction is not active";
}
}
@Override
public byte[] sign(byte[] b) throws RemoteException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public byte[] authUser(int uid) throws RemoteException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean returnSignedNumber(byte[] b, int uid) throws RemoteException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}