📄 simplejxtaapp.java
字号:
InputStream in = socket.getInputStream();
System.out.println("由东驶入");
int read = in.read(bufIn);
for(int j=0;j<10;j++){
if(isActive[j]==0){
System.out.println("East New born j="+j);
isActive[j]=1; //这样传值有问题
car[j].v=bufIn[0];
car[j].LeftLane=bufIn[1];
car[j].direction=1;
car[j].RunState=0;
if(bufIn[1]==0){
car[j].x=675;
car[j].y=235;
}
else if(bufIn[1]==1){
car[j].x=675;
car[j].y=215;
}
E_pool.execute(new Car_Run_E(car[j],myapp,myGui,j));
break;
}
}
/* car.direction=1;
car.RunState=0;
if(bufIn[1]==0){
car.x=675;
car.y=235;
}
else if(bufIn[1]==1){
car.x=675;
car.y=215;
}
E_pool.execute(new Car_Run_E(car,myapp,myGui));
System.out.println("New Car from the pool");
*/
System.out.println("Velecity:"+bufIn[0]+" Direction:"+bufIn[1]);
socket.close();
System.out.println("Closed connection. Ready for next Car.");
}
}
}
} catch (IOException ie) {
ie.printStackTrace();
}
}
class Car_Run_E implements Runnable{
SimpleJxtaApp myapp;
CarTest01 car;
RoadGui myGui;
int j;
private boolean outgo=true;
public Car_Run_E(CarTest01 car,SimpleJxtaApp myapp,RoadGui myGui,int j){
this.car = car;
this.myapp = myapp;
this.myGui = myGui;
this.j=j;
}
public void run(){
while(outgo){
try{
Thread.sleep(250);
}catch (Exception e) {
// could not instantiate the group, print the stack and exit
System.out.println("sleep(250) failure");
//System.exit(1);
}
finally{
//System.out.println("线程池中的线程");
car.runsel();
myGui.carpaint(car.x,car.y,car.direction,car.RunState,car.v,Color.BLUE);
if(car.x>685){
sendCar2E(car);
car.RunState=5;
//E_pool.shutdown();//线程交还线程池
outgo=false;
isActive[j]=0;
}
else if((car.x<6)&(car.x>2)){
sendCar2W(car);
car.RunState=5;
//E_pool.shutdown();
outgo=false;
isActive[j]=0;
}
else if(car.y>485){
sendCar2S(car);
car.RunState=5;
//E_pool.shutdown();
outgo=false;
isActive[j]=0;
}
else if((car.y<6)&(car.y>2)){
sendCar2N(car);
car.RunState=5;
//E_pool.shutdown();
outgo=false;
isActive[j]=0;
}
}
}
//System.out.println("Thread exit.");
}
public void sendCar2E(CarTest01 car){
try{
String type = PipeService.UnicastType;
PipeID pipeID = MD5ID.createPipeID(myapp.ATSPeerGroup.getPeerGroupID(),"02EAST", type);//02EAST//54321
//socEx.pipeAdv = PipeAdvertisement.createPipeAdvertisement(pipeID,type);
PipeAdvertisement pipeAdv =PipeUtilities.createPipeAdvertisement(pipeID,type);
//System.out.println("Connecting to the server");
JxtaSocket socket = new JxtaSocket(myapp.ATSPeerGroup,
//no specific peerid
null,
pipeAdv,
//general TO: 30 seconds
50000,
// reliable connection
true);
System.out.println("向东驶出");
OutputStream out = socket.getOutputStream();
//byte[] payload = new byte[]{(byte)(myapp.car[1].v),(byte)(myapp.car[1].LeftLane)};;
//byte[] payload = new byte[]{5,50};
byte[] payload = new byte[]{(byte)(car.v),(byte)(car.LeftLane)};
out.write(payload, 0, 2);
out.flush();
socket.close();
}catch (Throwable e) {
System.out.println("failed : " + e);
e.printStackTrace();
//System.exit(-1);
//E_pool.shutdown();//如果连接失败则将此线程归还线程驰
}
}
public void sendCar2W(CarTest01 car){
try{
String type = PipeService.UnicastType;
PipeID pipeID = MD5ID.createPipeID(myapp.ATSPeerGroup.getPeerGroupID(),"02WEST", type);
//socEx.pipeAdv = PipeAdvertisement.createPipeAdvertisement(pipeID,type);
PipeAdvertisement pipeAdv =PipeUtilities.createPipeAdvertisement(pipeID,type);
//System.out.println("Connecting to the server");
JxtaSocket socket = new JxtaSocket(myapp.ATSPeerGroup,
//no specific peerid
null,
pipeAdv,
//general TO: 30 seconds
50000,
// reliable connection
true);
System.out.println("向西驶出");
OutputStream out = socket.getOutputStream();
//byte[] payload = new byte[]{(byte)(myapp.car[1].v),(byte)(myapp.car[1].LeftLane)};;
//byte[] payload = new byte[]{5,50};
byte[] payload = new byte[]{(byte)(car.v),(byte)(car.LeftLane)};
out.write(payload, 0, 2);
out.flush();
socket.close();
}catch (Throwable e) {
System.out.println("failed : " + e);
e.printStackTrace();
// System.exit(-1);
//E_pool.shutdown();
}
}
public void sendCar2S(CarTest01 car){
try{
String type = PipeService.UnicastType;
PipeID pipeID = MD5ID.createPipeID(myapp.ATSPeerGroup.getPeerGroupID(),"02SOUTH", type);
//socEx.pipeAdv = PipeAdvertisement.createPipeAdvertisement(pipeID,type);
PipeAdvertisement pipeAdv =PipeUtilities.createPipeAdvertisement(pipeID,type);
//System.out.println("Connecting to the server");
JxtaSocket socket = new JxtaSocket(myapp.ATSPeerGroup,
//no specific peerid
null,
pipeAdv,
//general TO: 30 seconds
50000,
// reliable connection
true);
System.out.println("向南驶出");
OutputStream out = socket.getOutputStream();
//byte[] payload = new byte[]{(byte)(myapp.car[1].v),(byte)(myapp.car[1].LeftLane)};;
//byte[] payload = new byte[]{5,50};
byte[] payload = new byte[]{(byte)(car.v),(byte)(car.LeftLane)};
out.write(payload, 0, 2);
out.flush();
socket.close();
}catch (Throwable e) {
System.out.println("failed : " + e);
e.printStackTrace();
// System.exit(-1);
//E_pool.shutdown();
}
}
public void sendCar2N(CarTest01 car){
try{
String type = PipeService.UnicastType;
PipeID pipeID = MD5ID.createPipeID(myapp.ATSPeerGroup.getPeerGroupID(),"02NORTH", type);
//socEx.pipeAdv = PipeAdvertisement.createPipeAdvertisement(pipeID,type);
PipeAdvertisement pipeAdv =PipeUtilities.createPipeAdvertisement(pipeID,type);
//System.out.println("Connecting to the server");
JxtaSocket socket = new JxtaSocket(myapp.ATSPeerGroup,pipeAdv);
System.out.println("向北驶出");
OutputStream out = socket.getOutputStream();
//byte[] payload = new byte[]{(byte)(myapp.car[1].v),(byte)(myapp.car[1].LeftLane)};;
//byte[] payload = new byte[]{5,50};
byte[] payload = new byte[]{(byte)(car.v),(byte)(car.LeftLane)};
out.write(payload, 0, 2);
out.flush();
socket.close();
}catch (Throwable e) {
System.out.println("failed : " + e);
e.printStackTrace();
// System.exit(-1);
//E_pool.shutdown();
}
}
}
}
class WaitFromWest implements Runnable //作为内部类使用
{
SimpleJxtaApp myapp;
public ExecutorService W_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 WaitFromWest(SimpleJxtaApp myapp,RoadGui myGui){
this.myapp=myapp;
this.myGui=myGui;
}
public void run(){
try {
String type = PipeService.UnicastType;
PipeID pipeID = MD5ID.createPipeID(myapp.ATSPeerGroup.getPeerGroupID(),"02WEST", type);//01WEST
pipeAdv =PipeUtilities.createPipeAdvertisement(pipeID,type);
serverSocket = new JxtaServerSocket(myapp.ATSPeerGroup, pipeAdv, 10);
serverSocket.setSoTimeout(0);
}catch (Exception e){
//could not instantiate the group, print the stack and exit
System.out.println("Setup failure 2 W");
System.exit(1);
}
CarTest01 car22 = new CarTest01(myapp,235,635,6,1);
car22.RunState=0;
//W_pool.execute(new Car_Run_W(myapp.car1,myapp));
W_pool.execute(new Car_Run_W(car22,myapp,myGui,9));
/*CarTest01 car= new CarTest01(myapp); */
CarTest01 car[]= new CarTest01[10];
for(int j=0;j<10;j++){
car[j]=new CarTest01(myapp);
//W_pool.execute(new Car_Run_W(car[j],myapp,myGui));
}
try {
System.out.println("ServerSocket for West:Waiting for cars from West");
while(true){
Socket socket = serverSocket.accept();
if (socket != null) {
synchronized(myapp.str){
InputStream in = socket.getInputStream();
System.out.println("由西驶入");
int read = in.read(bufIn);
for(int j=0;j<10;j++){
if(isActive[j]==0){
System.out.println("West New born j="+j);
isActive[j]=1; //这样传值有问题
car[j].v=bufIn[0];
car[j].LeftLane=bufIn[1];
car[j].direction=3;
car[j].RunState=0;
if(bufIn[1]==0){
car[j].x=25;
car[j].y=255;
}
else if(bufIn[1]==1){
car[j].x=25;
car[j].y=275;
}
W_pool.execute(new Car_Run_W(car[j],myapp,myGui,j));
break;
}
}
/* car.direction=3;
car.RunState=0;
if(bufIn[1]==0){
car.x=25;
car.y=255;
}
else if(bufIn[1]==1){
car.x=25;
car.y=275;
}
System.out.println("New Car from the pool");
W_pool.execute(new Car_Run_W(car,myapp,myGui));*/
System.out.println("Velecity:"+bufIn[0]+" Direction:"+bufIn[1]);
socket.close();
System.out.println("Closed connection. Ready for next Car.");
}
}
}
} catch (IOException ie) {
ie.printStackTrace();
}
}
class Car_Run_W implements Runnable{
SimpleJxtaApp myapp;
CarTest01 car;
RoadGui myGui;
int j;
private boolean outgo=true;
public Car_Run_W(CarTest01 car,SimpleJxtaApp myapp,RoadGui myGui,int j){
this.car = car;
this.myapp = myapp;
this.myGui = myGui;
this.j=j;
}
public void run(){
while(outgo){
try{
Thread.sleep(250);
}catch (Exception e) {
// could not instantiate the group, print the stack and exit
System.out.println("sleep(250) failure");
//System.exit(1);
}
finally{
//System.out.println("线程池中的线程");
car.runsel();
myGui.carpaint(car.x,car.y,car.direction,car.RunState,car.v,Color.BLUE);
if(car.x>685){
sendCar2E(car);
car.RunState=5;
outgo=false;
isActive[j]=0;
}
else if((car.x<6)&(car.x>2)){
sendCar2W(car);
car.RunState=5;
outgo=false;
isActive[j]=0;
}
else if(car.y>485){
sendCar2S(car);
car.RunState=5;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -