📄 simplejxtaapp.java
字号:
System.out.println("failed : " + e);
e.printStackTrace();
// System.exit(-1);
//S_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);
//S_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);
//S_pool.shutdown();
}
}
}
}
class WaitFromNorth implements Runnable //作为内部类使用
{
SimpleJxtaApp myapp;
public ExecutorService N_pool = Executors.newFixedThreadPool(10);
//CarTest01 car[]= new CarTest01[10];
CarTest01 car22 = new CarTest01(myapp,235,635,6,1);
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 WaitFromNorth(SimpleJxtaApp myapp,RoadGui myGui){
this.myapp=myapp;
this.myGui = myGui;
}
public void run(){
try {
//System.out.println("Reading in pipe.adv");
/* FileInputStream is = new FileInputStream("pipe.adv"); //每一个Socket的广告不能相同
pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
is.close();*/
String type = PipeService.UnicastType;
PipeID pipeID = MD5ID.createPipeID(myapp.ATSPeerGroup.getPeerGroupID(),"02NORTH", type);
//socEx.pipeAdv = PipeAdvertisement.createPipeAdvertisement(pipeID,type);
pipeAdv =PipeUtilities.createPipeAdvertisement(pipeID,type);
serverSocket = new JxtaServerSocket(myapp.ATSPeerGroup, pipeAdv, 10);
// 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 2 W");
System.exit(1);
}
CarTest01 car[]= new CarTest01[10];
for(int j=0;j<10;j++){
car[j]=new CarTest01(myapp);
//N_pool.execute(new Car_Run_N(car[j],myapp,myGui));
}
/*CarTest01 car= new CarTest01(myapp);*/
try {
System.out.println("ServerSocket for North:Waiting for cars from North");
while(true){
Socket socket = serverSocket.accept();
// set reliable
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("North New born j="+j);
isActive[j]=1; //这样传值有问题
car[j].v=bufIn[0];
car[j].LeftLane=bufIn[1];
car[j].direction=4;
car[j].RunState=0;
if(bufIn[1]==0){
car[j].x=315;
car[j].y=25;
}
else if(bufIn[1]==1){
car[j].x=335;
car[j].y=25;
}
N_pool.execute(new Car_Run_N(car[j],myapp,myGui,j));
break;
}
}
/* car.direction=4;
car.RunState=0;
if(bufIn[1]==0){
car.x=315;
car.y=25;
}
else if(bufIn[1]==1){
car.x=335;
car.y=25;
}
N_pool.execute(new Car_Run_N(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_N implements Runnable{
SimpleJxtaApp myapp;
CarTest01 car;
RoadGui myGui;
int j;
private boolean outgo=true;
public Car_Run_N(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;
outgo=false;
isActive[j]=0;
}
else if((car.y<6)&(car.y>2)){
sendCar2N(car);
car.RunState=5;
outgo=false;
isActive[j]=0;
}
}
}
}
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);
//N_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);
// N_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);
// N_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);
// N_pool.shutdown();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -