[Beepcore-java-users] Peer-to-peer message exchange
Status: Beta
Brought to you by:
huston
From: <eri...@be...> - 2006-10-03 17:40:10
|
Hi =20 I have the following scenario: =20 Server A listening on port 1234 Server B initiating a session on server A using BEEP Server B start a channel Server B sends a MSG and got a reply from server A. =20 I want at this point that the server A sends a MSG to server B. But server A can't send messages since it doesn't know which channel to use. Also, Server B is not listening at all. How could I tell the Server B to listen for MSG and tell server A which channel to use? Do I need to create a new TCPSession.listen(port+1, profileRegistry) on server B and on server A initiates a connection to the server B ??? =20 Here's the source code for server A: =20 ProfileRegistry =3D new ProfileRegistry(); mFlapProfile =3D new FlapProfile(); =20 // Initialize the profile and add it to the advertised profiles ProfileRegistry.addStartChannelListener("http://sitacs.uow.edu.au/ns/loc ation/flap/beep", =20 mFlapProfile.init("http://sitacs.uow.edu.au/ns/location/flap/beep", null), null); =20 public void run() { try { // Loop listening for new Sessions while (true) { TCPSessionCreator.listen(port, ProfileRegistry); } catch (Exception e) { log.error("Listener exiting", e); } } =20 Here's the source code for server B public synchronized void run() { //Initiate a session with the server try { =20 mSession =3D TCPSessionCreator.initiate(host, port); mChannel =3D mSession.startChannel(FlapProfile.URI); //send first MSG and got reply in this method initiateSync (); mChannel.setRequestHandler(this); =20 while (mSession.getSocket().isConnected()) { //wait for incoming data what to do here ???? } =20 } catch (BEEPError e) { if (e.getCode() =3D=3D 550) { System.err.println("bing: Error host does not support " + "echo profile"); } else { System.err.println("bing: Error starting channel (" + e.getCode() + ": " + e.getMessage() + ")"); } return; } catch (BEEPException e) { System.err.println("bing: Error starting channel (" + e.getMessage() + ")"); return; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }=20 } =20 private void initiateSync() { =20 Reply reply =3D new Reply(); InputStream is =3D null; =20 String lSync =3D "<?xml version=3D'1.0' encoding=3D'UTF-8'?>" + "<sync xsi:type=3D'sync' xmlns=3D'urn:flap'/>"; =20 try { // Send the synchronization request mChannel.sendMSG(new StringOutputDataStream("application/xml", lSync), reply); =20 while (reply.hasNext()) { //Get the reply to the request is =3D reply.getNextReply().getDataStream().getInputStream(); =20 Document lDoc =3D mDomBuilder.parse(is); //Convert the XML request to a Java object Unmarshaller lUnmarshaller =3D mJaxBContext.createUnmarshaller(); JAXBElement instance =3D (JAXBElement)lUnmarshaller.unmarshal(lDoc); SyncrType lSyncResponse =3D (SyncrType)instance.getValue(); =20 //TODO Save information System.out.println("Save information"); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BEEPInterruptedException e) { System.err.println("bing: Error receiving reply (" + e.getMessage() + ")"); e.printStackTrace(); } catch (BEEPException e) { System.err.println("bing: Error sending request (" + e.getMessage() + ")"); e.printStackTrace(); } catch (IOException e) { System.err.println("bing: Error receiving reply (" + e.getMessage() + ")"); e.printStackTrace(); } } =20 =20 Regards, Eric =20 =20 |