To implement the send and receive states:
Implement the
logic for sending a business card in the SendState.doSend
method. The HeaderSet.getResponseCode
method returns
the response code received from the server (defined in the ResponseCodes
class).
/* * This method implements the logic for sending a * business card * */ public void doSend() { try { connection = (ClientSession) Connector.open(url); HeaderSet response = connection.connect(null); // Initiate the PUT request operation = connection.put(null); OutputStream out = operation.openOutputStream(); //getting the own card byte[] vCard = parent.getListener().getOwnBC(); int vlen = vCard.length; byte[] tmpBuf = new byte[vlen + 4]; System.arraycopy(vCard, 0, tmpBuf, 4, vlen); tmpBuf[0] = (byte) ((vlen >>> 24) & 0xff); tmpBuf[1] = (byte) ((vlen >>> 16) & 0xff); tmpBuf[2] = (byte) ((vlen >>> 8) & 0xff); tmpBuf[3] = (byte) ((vlen >>> 0) & 0xff); //sending data out.write(tmpBuf); out.close(); int responseCode = operation.getResponseCode(); operation.close(); if (cancelInvoked) { throw new Exception( "Cancel did not invoke any exception; throw exception then"); } // send is done try { parent.setState(new IdleState(parent)); } catch (Exception e) { } } catch (Exception e1) { parent.setState(new IdleState(parent)); if (cancelInvoked) { // if exception is caused by cancellation parent.getListener().onSendComplete(ExchangerComm.CANCELED); } else { // if exception is caused by error parent.setState(new IdleState(parent)); parent.getListener().onSendComplete(ExchangerComm.ERROR); } } finally { try { // finalizing operation operation.close(); } catch (IOException e3) { } try { // sending DISCONNECT command connection.disconnect(null); connection.close(); } catch (IOException e2) { // Ignore, disconnecting anyway } } }
Implement the
logic for receiving a business card in the ReceiveState.doGet
method. The HeaderSet.getResponseCode
method returns
the response code received from the server (defined in the ResponseCodes
class).
/* * This method implements the logic for * receving a business card * */ public void doGet() { try { connection = (ClientSession) Connector.open(url); HeaderSet response = connection.connect(null); operation = connection.get(null); InputStream in = operation.openInputStream(); byte[] fullResult = null; { byte[] buf = new byte[256]; ByteArrayOutputStream bout = new ByteArrayOutputStream(2048); for (int len = in.read(buf); len >= 0; len = in.read(buf)) { bout.write(buf, 0, len); } fullResult = bout.toByteArray(); } ByteArrayInputStream bin = new ByteArrayInputStream(fullResult); DataInputStream din = new DataInputStream(bin); int size = din.readInt(); byte[] vCard = new byte[size]; din.read(vCard); parent.getListener().onReceiveBC(vCard); // End the transaction in.close(); int responseCode = operation.getResponseCode(); operation.close(); if (cancelInvoked) { throw new Exception( "Cancel did not invoke any exception; throw exception then"); } // receive is done try { parent.setState(new IdleState(parent)); parent.getListener().onReceiveComplete(ExchangerComm.DONE); } catch (Exception e) { } } catch (Exception e1) { if (cancelInvoked) { // if exception was caused by canceling parent.setState(new IdleState(parent)); parent.getListener().onReceiveComplete(ExchangerComm.CANCELED); } else { // if exception was caused by error parent.setState(new IdleState(parent)); parent.getListener().onReceiveComplete(ExchangerComm.ERROR); } } finally { try { // finalizing operation operation.close(); } catch (IOException e3) { } try { // sending DISCONNECT command connection.disconnect(null); } catch (IOException e2) { // Ignore, disconnecting anyway } } }