/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2011-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.tyrus.sample.auction;
import java.util.Timer;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.Session;
import org.glassfish.tyrus.sample.auction.message.AuctionMessage;
/**
* Implements the auction protocol.
*
* @author Stepan Kopriva (stepan.kopriva at oracle.com)
*/
public class Auction {
/*
* Current state of the auction
*/
private AuctionState state;
/*
* ID of the auction used for communication
*/
private final String id;
/*
* Assigns id to newly created Auction object
*/
private static final AtomicInteger idCounter = new AtomicInteger(0);
/*
* Auction Item
*/
private final AuctionItem item;
/*
* List of remote clients (Peers)
*/
private final CopyOnWriteArrayList<Session> arcList = new CopyOnWriteArrayList<Session>();
/*
* Timer that sends pre-auction time broadcasts
*/
private Timer auctionRunningTimer;
/*
* Value of the highest bid
*/
private double bestBid;
private String bestBidderName;
public enum AuctionState {
PRE_AUCTION, AUCTION_RUNNING, AUCTION_FINISHED
}
public Auction(AuctionItem item) {
this.item = item;
this.state = AuctionState.PRE_AUCTION;
this.id = Integer.toString(Auction.idCounter.getAndIncrement());
bestBid = item.getPrice();
}
synchronized void addArc(Session arc) {
arcList.add(arc);
}
public synchronized void removeArc(Session arc) {
arcList.remove(arc);
}
/*
* New user logs into the auction.
*/
public void handleLoginRequest(AuctionMessage messsage, Session arc) {
arc.getUserProperties().put("name", messsage.getData());
synchronized (id) {
if (state != AuctionState.AUCTION_FINISHED) {
if (!getRemoteClients().contains(arc)) {
this.addArc(arc);
}
try {
item.setPrice(bestBid);
arc.getBasicRemote().sendObject(
new AuctionMessage.LoginResponseMessage(id, item));
} catch (Exception e) {
Logger.getLogger(Auction.class.getName()).log(Level.SEVERE, null, e);
}
if (state == AuctionState.PRE_AUCTION) {
startAuctionTimeBroadcast();
}
} else {
try {
arc.getBasicRemote().sendObject(
new AuctionMessage.LoginResponseMessage(id, item));
if (bestBidderName != null) {
if (bestBidderName.equals(messsage.getData())) {
arc.getBasicRemote().sendObject(
new AuctionMessage.ResultMessage(id, String.format(
"Congratulations, You won the auction and will pay %.0f.",
bestBid)));
} else {
arc.getBasicRemote().sendObject(
new AuctionMessage.ResultMessage(id, String.format(
"You did not win the auction. The item was sold for %.0f.",
bestBid)));
}
} else {
arc.getBasicRemote().sendObject(
new AuctionMessage.ResultMessage(id,
"No one bid for the item, it was not been sold!"));
}
} catch (Exception e) {
Logger.getLogger(Auction.class.getName()).log(Level.SEVERE, null, e);
}
}
}
}
public void handleBidRequest(AuctionMessage message, Session arc) {
synchronized (id) {
if (state == AuctionState.AUCTION_RUNNING) {
Double bid = Double.parseDouble((String) message.getData());
if (bid > bestBid) {
bestBid = bid;
bestBidderName = (String) arc.getUserProperties().get("name");
sendPriceUpdateMessage();
stopAuctionTimeBroadcast();
startAuctionTimeBroadcast();
}
}
}
}
private void sendPriceUpdateMessage() {
AuctionMessage.PriceUpdateResponseMessage purm =
new AuctionMessage.PriceUpdateResponseMessage(id, "" + bestBid);
for (Session arc : getRemoteClients()) {
try {
arc.getBasicRemote().sendObject(purm);
} catch (Exception e) {
Logger.getLogger(Auction.class.getName()).log(Level.SEVERE, null, e);
}
}
}
public void switchStateToAuctionFinished() {
synchronized (id) {
state = AuctionState.AUCTION_FINISHED;
}
stopAuctionTimeBroadcast();
sendAuctionResults();
}
private void sendAuctionResults() {
Session bestBidder = null;
if (bestBidderName != null) {
for (Session session : getRemoteClients()) {
if (session.getUserProperties().get("name").equals(bestBidderName)) {
bestBidder = session;
}
}
}
if (bestBidder != null) {
AuctionMessage.ResultMessage winnerMessage =
new AuctionMessage.ResultMessage(id, String.format(
"Congratulations, You won the auction and will pay %.0f.", bestBid));
try {
bestBidder.getBasicRemote().sendObject(winnerMessage);
} catch (Exception e) {
Logger.getLogger(Auction.class.getName()).log(Level.SEVERE, null, e);
}
AuctionMessage.ResultMessage loserMessage =
new AuctionMessage.ResultMessage(id, String.format(
"You did not win the auction. The item was sold for %.0f.", bestBid));
for (Session arc : getRemoteClients()) {
if (arc != bestBidder) {
try {
arc.getBasicRemote().sendObject(loserMessage);
} catch (Exception e) {
Logger.getLogger(Auction.class.getName()).log(Level.SEVERE, null, e);
}
}
}
} else {
AuctionMessage.ResultMessage notBeenSoldMessage =
new AuctionM
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
weblogic websocket示例代码 (160个子文件)
Auction.class 8KB
WebSocketMessageBoardEndPoint.class 7KB
AuctionEndpoint.class 4KB
AuctionMessageEncoder.class 4KB
AuctionTimeBroadcasterTask.class 2KB
Client$1.class 2KB
EchoEndpoint.class 2KB
Client.class 2KB
AuctionMessageDecoder.class 2KB
AuctionMessage.class 2KB
SingletonEndpoint.class 2KB
Client$1$1.class 2KB
InjectToSingletonEndpoint.class 1KB
InjectToStatefulEndpoint.class 1KB
Auction$AuctionState.class 1KB
SimpleEndpoint.class 1KB
StatefulEndpoint.class 1KB
LoggingInterceptor.class 1KB
ApplicationScopedEndpoint.class 1KB
AuctionEndpoint$1.class 1KB
AuctionItem.class 1KB
InjectedSingletonBean.class 849B
InjectedStatefulBean.class 845B
InjectedSimpleBean.class 794B
AuctionMessage$LoginResponseMessage.class 751B
AuctionMessage$AuctionTimeBroadcastMessage.class 749B
AuctionMessage$AuctionListResponseMessage.class 709B
AuctionMessage$PriceUpdateResponseMessage.class 689B
AuctionMessage$ResultMessage.class 649B
InjectedApplicationScoped.class 566B
.classpath 1KB
.classpath 1KB
.classpath 1KB
.classpath 1KB
org.eclipse.wst.common.component 547B
org.eclipse.wst.common.component 542B
org.eclipse.wst.common.component 515B
org.eclipse.wst.common.component 492B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.container 49B
main.css 3KB
stylesheet.css 2KB
Thumbs.db 6KB
AuctionMessageDecoder.html 18KB
AuctionMessageDecoder.html 18KB
SingletonEndpoint.html 17KB
SingletonEndpoint.html 17KB
InjectToSingletonEndpoint.html 16KB
InjectToSingletonEndpoint.html 16KB
InjectToStatefulEndpoint.html 16KB
InjectToStatefulEndpoint.html 16KB
SimpleEndpoint.html 15KB
SimpleEndpoint.html 15KB
StatefulEndpoint.html 15KB
StatefulEndpoint.html 15KB
InjectedSingletonBean.html 14KB
InjectedSingletonBean.html 14KB
InjectedStatefulBean.html 14KB
InjectedStatefulBean.html 14KB
LoggingInterceptor.html 14KB
LoggingInterceptor.html 14KB
InjectedSimpleBean.html 13KB
InjectedSimpleBean.html 13KB
ApplicationScopedEndpoint.html 13KB
ApplicationScopedEndpoint.html 13KB
InjectedApplicationScoped.html 12KB
InjectedApplicationScoped.html 12KB
ws-fallback.html 6KB
index.html 6KB
auction.html 5KB
index.html 5KB
index.html 3KB
select.html 3KB
favicon.ico 2KB
wls-api.jar 2.2MB
wls-api.jar 2.2MB
wls-api.jar 2.2MB
wls-api.jar 2.2MB
javax.json_1.0.0.0_1-0.jar 74KB
javax.json_1.0.0.0_1-0.jar 74KB
javax.json_1.0.0.0_1-0.jar 74KB
javax.websocket_1.0.0.0.jar 35KB
javax.websocket_1.0.0.0.jar 35KB
javax.websocket_1.0.0.0.jar 35KB
javax.websocket_1.0.0.0.jar 35KB
Auction.java 9KB
WebSocketMessageBoardEndPoint.java 6KB
AuctionEndpoint.java 5KB
AuctionMessage.java 5KB
AuctionMessageEncoder.java 5KB
AuctionTimeBroadcasterTask.java 3KB
AuctionMessageDecoder.java 3KB
SingletonEndpoint.java 3KB
AuctionItem.java 3KB
EchoEndpoint.java 3KB
Client.java 3KB
InjectToSingletonEndpoint.java 3KB
InjectToStatefulEndpoint.java 3KB
共 160 条
- 1
- 2
既然2015
- 粉丝: 34
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页