⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplejxtaapp.java

📁 基于JXTA的P2P交通仿真程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
				 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);
		            //W_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);
		            //W_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);
		            //W_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);
		            //W_pool.shutdown();
		        }
			}

	}
}


class WaitFromSouth implements Runnable
{
	SimpleJxtaApp myapp;
	public ExecutorService S_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 WaitFromSouth(SimpleJxtaApp myapp,RoadGui myGui){
    	this.myapp=myapp;
    	this.myGui=myGui;
    }
    
	public  void run(){

	        try {
	        	    //System.out.println("Reading in socket.adv");
	                
	                    /*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(),"02SOUTH", 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");
	                    	System.exit(1);
	                    }
	                    CarTest01 car22 = new CarTest01(myapp,235,635,4,1);
	                    CarTest01 car11= new CarTest01(myapp,475,375,5,2);
	                    /*CarTest01 car= new CarTest01(myapp);*/
	                	//CarTest01 car= new CarTest01(myapp);
	                	CarTest01 car[]= new CarTest01[10];
	                	for(int j=0;j<10;j++){
	                		car[j]=new CarTest01(myapp);
	        	            //S_pool.execute(new Car_Run_S(car[j],myapp,myGui));
	            		}

	                    S_pool.execute(new Car_Run_S(car22,myapp,myGui,8));
	                    S_pool.execute(new Car_Run_S(car11,myapp,myGui,9));
	                    try {        
	                    	//socket.setKeepAlive(On);
	                while(true){
	                    System.out.println("ServerSocket for South:Waiting for cars from South");
	                    Socket socket = serverSocket.accept();
	            
	                    // set reliable
//	                  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("South New born j="+j);
	    		            		isActive[j]=1;               //这样传值有问题
	    		            		car[j].v=bufIn[0];
	    		            		car[j].LeftLane=bufIn[1];
	    		            		car[j].direction=2;
	    		            		car[j].RunState=0;
	    		            		if(bufIn[1]==0){
	    		            			car[j].x=355;
	    		            			car[j].y=475;
	    		            		}
	    		            		else if(bufIn[1]==1){
	    		            			car[j].x=375;
	    		            			car[j].y=475;
	    		            		}
			    	                S_pool.execute(new Car_Run_S(car[j],myapp,myGui,j));
	    		            		break;
	    		            	}
	    		            	//break;
	    		            }
	    	             /*     car.direction=2;
		            		car.RunState=0;
		            		if(bufIn[1]==0){
		            			car.x=355;
		            			car.y=475;
		            		}
		            		else if(bufIn[1]==1){
		            			car.x=375;
		            			car.y=475;
		            		}
	    	                S_pool.execute(new Car_Run_S(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_S implements Runnable{
			  SimpleJxtaApp myapp;
			  CarTest01 car;
			  RoadGui myGui;
			  int j;
			  private boolean outgo=true;
			  public Car_Run_S(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);
			            //S_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) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -