📄 simplejxtaapp.java
字号:
/*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
*/
//import java.io.FileInputStream;
import java.io.IOException;
//import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupID;
//import net.jxta.endpoint.*;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.pipe.PipeID;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
//import net.jxta.discovery.DiscoveryService;
import net.jxta.exception.PeerGroupException;
import net.jxta.rendezvous.RendezVousService;
import net.jxta.socket.JxtaServerSocket;
import net.jxta.socket.*;
import net.jxta.util.PipeUtilities;
import java.awt.*;
import java.util.concurrent.*;
/**
* This is a simple example of how an application would start jxta
*/
public class SimpleJxtaApp{
public static final int ITERATIONS = 5;
static PeerGroup netPeerGroup = null;
PeerGroup ATSPeerGroup = null;
RoadGui myGui;
//connect2E mySoc2E = new connect2E(this); ///容器类/部件类的用法
boolean isEastSetUp=false;
//connect2W mySoc2W = new connect2W(this);
boolean isWestSetUp=false;
//connect2S mySoc2S = new connect2S(this);
boolean isSouthSetUp=false;
//connect2N mySoc2N = new connect2N(this);
boolean isNorthSetUp=false;
//boolean ok = isEastSetUp&&isWestSetUp&&isSouthSetUp&&isNorthSetUp;
public JxtaServerSocket serverSocketE;
public JxtaServerSocket serverSocketW;
public JxtaServerSocket serverSocketS;
public JxtaServerSocket serverSocketN;
JxtaSocket socketE;
JxtaSocket socketW;
JxtaSocket socketS;
JxtaSocket socketN;
int start=0;
String str = new String("12"); //线程同步用对象
String str1 = new String("123"); //线程同步用对象
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
//Timer timer;
int flag1=0;
int flag2=0;
int flag3=0;
int flag4=0;
int flag5=0;
int flag0=0;
int phasestatus[][]={{0,2,2,0,2,2,2,2},{2,2,2,2,0,2,2,0},{2,0,0,2,2,2,2,2},{2,2,2,2,2,0,0,2}}; //相位状态,4个相位,4组灯
int lightstatus[]={0,2,2,0,2,2,2,2}; //8组灯的状态,0-绿,1-黄,2-红
int phasetime[]={4,4,4,4}; //8相位的时间
int allredtime=2;
int yellowtime=2;
int count=0;
int phaseno=0;
int xCenter=350;
int yCenter=250;
int s[][]=new int[700][500]; //整个区域的一个映射数组;
CarTest01 cartest;
CarTest01 car1;
CarTest01 car2;
CarTest01 car[]= new CarTest01[10];
public int [] isActive = new int[]{0,0,0,0,0,0,0,0,0,0}; //10辆车的标志位
static PeerGroup myGroup = null;
//private DiscoveryService discoSvc;
//private ExecutorService pool = null;
public ExecutorService E_pool= null;
public ExecutorService W_pool= null;
public ExecutorService S_pool= null;
public ExecutorService N_pool= null;
public static void main(String args[]) {
System.out.println("Starting JXTA ....");
SimpleJxtaApp myapp = new SimpleJxtaApp();
myapp.setup(); //建立交通环境
WaitFromEast com1 = new WaitFromEast(myapp,myapp.myGui);
WaitFromWest com2 = new WaitFromWest(myapp,myapp.myGui);
WaitFromSouth com3 = new WaitFromSouth(myapp,myapp.myGui);
WaitFromNorth com4 = new WaitFromNorth(myapp,myapp.myGui);
//SocketRequest sock = myapp.new SocketRequest();
try{
myapp.startJxta(); //启动Jxta平台,创建ATS组
}catch(Exception e){
e.printStackTrace();
}
new Thread(com1).start(); //启动Server Socket通讯线程
new Thread(com2).start();
new Thread(com3).start();
new Thread(com4).start();
/* myapp.mySoc2S.setup(); //调用Client Socket通讯测试
myapp.mySoc2N.setup();
myapp.mySoc2E.setup();
myapp.mySoc2W.setup();
try{
Thread.sleep(3000);
}catch (Exception e) {
// could not instantiate the group, print the stack and exit
System.out.println("DrawATS failure");
System.exit(1);
}*/
//new Thread(sock).start(); //启动Client Socket通讯线程
while(true){//myapp.ok//true//myapp.start==1 //当所有方向的连接都建好的启动
myapp.DrawATS();
//myapp.transfer();
try{
Thread.sleep(250);
}catch (Exception e) {
// could not instantiate the group, print the stack and exit
System.out.println("DrawATS failure");
System.exit(1);
}
// pps.send(msg);
}
}
private void startJxta() throws Exception{
try {
// create and start the default JXTA NetPeerGroup
netPeerGroup = PeerGroupFactory.newNetPeerGroup();
} catch (PeerGroupException e) {
// could not instantiate the group, print the stack and exit
System.out.println("fatal error : group creation failure");
e.printStackTrace();
System.exit(1);
}
/**创建特定对等组"CASATS"**/
PeerGroupID peerGroupBinaryID = MD5ID.createPeerGroupID(netPeerGroup.getPeerGroupID(),"Artificial Transportation Systems V.01", "Simulation");
ATSPeerGroup = PeerGroupTool.createAndJoinPeerGroup(netPeerGroup, "CASATS",peerGroupBinaryID);
//waitForRendezVous(netPeerGroup);
}
/** Wait for the peer to connect to a Rendezvous. If there is no rende after 30 seconds, become one.*/
public boolean waitForRendezVous(PeerGroup peerGroup) throws Exception {
RendezVousService rdv = peerGroup.getRendezVousService();
if (rdv == null) {
throw new RuntimeException("No Rendezvous Service is not configured");
}
if(rdv.isRendezVous()){
return true;
}
if (rdv.isConnectedToRendezVous() ) {
return true;
}
System.out.println("Waiting for RendezVous");
int count = 0;
while (!rdv.isConnectedToRendezVous()){// && blockRendezVousWait) {
//LOG.info("*");
Thread.currentThread().sleep(5*1000);
if (++count >= 6){
System.out.println("becomming a rendezvous");
rdv.startRendezVous();
return true;
}
}
System.out.println("\nisConnectedToRendezVous()==true");
return rdv.isConnectedToRendezVous();
}
void setup() {
/**/
//pool = Executors.newFixedThreadPool(10);
if(isSouthSetUp){
System.out.println(counter2+"South Socket set up!");//"counter2="+counter2+
}
if(isNorthSetUp){
System.out.println(counter4+"North Socket set up!");
}
if(isEastSetUp){
System.out.println(+counter1+"East Socket set up!");
}
if(isWestSetUp){
System.out.println(counter3+"West Socket set up!");
}
start=counter1*counter2*counter3*counter4;
for(int t=0;t<=699;t++)
{
for(int f=0;f<=499;f++)
{
s[t][f]=0;
}
}
myGui = new RoadGui(this);
myGui.show();
//内部车
cartest = new CarTest01(this,215,675,6,1);
car1 = new CarTest01(this,480,355,7,2);
car2 = new CarTest01(this,235,650,5,1);
//动态车
for(int i=0;i<10;i++){
car[i]=new CarTest01(this);
}
/*/第一辆车/从线程池中产生
car[0].x=315;
car[0].y=27;
car[0].direction=4;
pool.execute(new Car_Run(car[0]));*/
}
void DrawATS(){
/**********信号灯控制************/
int i=0;
//System.out.println(count);
count++;
if (count==(phasetime[phaseno]))
{
for (i=0;i<8;i++)
{
if (phasestatus[phaseno][i]==0)
{
lightstatus[i]=1;
}
}
}
if (count==(phasetime[phaseno]+3))
{
for (i=0;i<8;i++)
{
if (phasestatus[phaseno][i]==0)
{
lightstatus[i]=2;
}
}
}
if (count==(phasetime[phaseno]+6))
{
phaseno++;
if (phaseno>=4)
{
phaseno=0;
}
for (i=0;i<8;i++)
{
lightstatus[i]=phasestatus[phaseno][i];
}
count=0;
}
cartest.runsel();
myGui.carpaint(cartest.x,cartest.y,cartest.direction,cartest.RunState,cartest.v,Color.ORANGE);
car1.runsel();
myGui.carpaint(car1.x,car1.y,car1.direction,car1.RunState,car1.v,Color.PINK);
car2.runsel();
myGui.carpaint(car2.x,car2.y,car2.direction,car2.RunState,car2.v,Color.BLACK);
/*car[0].runsel();
myGui.carpaint(car[0].x,car[0].y,car[0].direction,car[0].RunState,car[0].v);*/
}
}//总类SimpleJxtaApp结束
class WaitFromEast implements Runnable
{
SimpleJxtaApp myapp;
public ExecutorService E_pool = Executors.newFixedThreadPool(10);
RoadGui myGui;
private PipeAdvertisement pipeAdv;
public JxtaServerSocket serverSocket;
//public JxtaServerSocket serverSocket;
byte[] bufIn = new byte[2];
byte[] connect=new byte[1];
private int[] isActive=new int[]{0,0,0,0,0,0,0,0,0,0};
public WaitFromEast(SimpleJxtaApp myapp,RoadGui myGui){
this.myapp=myapp;
this.myGui=myGui;
}
public void run(){
try {
//System.out.println("Creating pipeadv");
/*FileInputStream is = new FileInputStream("socket.adv");
pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
is.close();*/
String type = PipeService.UnicastType;
PipeID pipeID = MD5ID.createPipeID(myapp.ATSPeerGroup.getPeerGroupID(),"02EAST", type);//1234567//02EAST//12345
//socEx.pipeAdv = PipeAdvertisement.createPipeAdvertisement(pipeID,type);
pipeAdv =PipeUtilities.createPipeAdvertisement(pipeID,type);
serverSocket = new JxtaServerSocket(myapp.ATSPeerGroup, pipeAdv);
// block until a connection is available
serverSocket.setSoTimeout(0);
}catch (Exception e){
// could not instantiate the group, print the stack and exit
System.out.println("Setup failure");
System.exit(1);
}
CarTest01 car[]= new CarTest01[10];
for(int j=0;j<10;j++){
car[j]=new CarTest01(myapp);
//E_pool.execute(new Car_Run_E(car[j],myapp,myGui));
}
/*CarTest01 car= new CarTest01(myapp);*/
try {
//socket.setKeepAlive(On);
while(true){
System.out.println("ServerSocket for East:Waiting for cars from East");
Socket socket = serverSocket.accept();
if (socket != null) {
synchronized(myapp.str){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -