/** Sends a Message */ public void sendMessage(Message msg) throws IOException { if (tcp_conn != null) { last_time = System.currentTimeMillis(); byte[] data = msg.toString().getBytes(); Log.e("Final Message: ", msg.toString()); int endindex = msg.toString().indexOf("image/jpeg") + 10; String headers = msg.toString().substring(0, endindex) + "\n \n"; Log.e("Initial Header:", headers); byte[] headerBytes = headers.getBytes(); byte[] imageBytes = JpegImage.imageBytes; byte[] finalBytes = new byte[headerBytes.length + imageBytes.length]; System.arraycopy(headerBytes, 0, finalBytes, 0, headerBytes.length); System.arraycopy(imageBytes, 0, finalBytes, headerBytes.length, imageBytes.length); // for(int i=0;i<headerBytes.length;i++){ // finalBytes[i] = headerBytes[i]; // } // int j=0; // for(int i=headerBytes.length;j<(imageBytes.length);i++){ // finalBytes[i]=imageBytes[j]; // j++; // } // Log.e("ImageBytes: ", // "Length:"+String.valueOf(finalBytes[finalBytes.length])+"read:"+String.valueOf(imageBytes[imageBytes.length])); Log.e("Modified headers: ", headers); // tcp_conn.send(data); Log.e("TCPTransport: ", "Total Image length=" + finalBytes.length); tcp_conn.send(finalBytes); } }
public void routeSIP(final Message message, final JID sender) { for (final SipRoutingListener routingListener : routingListeners) { routingListener.routingSIP(message, sender); } // Route Packet final byte[] bytes; try { final String msg = message.toString(); bytes = msg.getBytes("UTF-8"); final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); final SocketAddress destination = getDestinationAddress(message, sender); if (destination == null) { log.warn("Discarding Packet Without Destination: " + msg); return; } try { SipChannel c = message.getArrivedAt(); if (c == null) { c = channels.get(getSipChannelID(sender)); } if (sender != null && c == null) { log.debug("Creating SIP Channel for: " + sender); c = createSipChannel(sender, destination); } if (c == null) { log.warn("Could NOT Bind Message to any SipChannel."); return; } log.debug("Sending SIP: " + msg); c.send(byteBuffer, destination); updateKeepAliveTargets(destination); for (final SipRoutingListener routingListener : getRoutingListeners()) { routingListener.routedSIP(message, sender); } } catch (Exception e) { log.warn("Unresolved Address.", e); for (final SipRoutingListener routingListener : getRoutingListeners()) { routingListener.routingError(message, sender, SipRoutingError.unresolvedAddress); } } } catch (UnsupportedEncodingException e) { log.error("Packet Encoding Error.", e); } }
SocketAddress getDestinationAddress(final Message message, final JID sender) { if (message.getSendTo() != null) { log.debug("Using sendTo Value for: " + message.toString()); return message.getSendTo(); } final SipAccount sipAccount = sipAccountProvider.getSipAccount(sender); if (sipAccount != null) { log.debug("Using sipAccount Value for: " + message.toString()); final SocketAddress result = CachedAddressResolver.getInstance().getSocketAddress(sipAccount.getOutboundproxy()); message.setSendTo(result); return result; } if (message.getToHeader() != null) { log.debug("Using message Header Value for: " + message.toString()); SipURL sipUrl = message.getContactHeader() != null ? message.getContactHeader().getNameAddress().getAddress() : null; if (sipUrl == null) { if (message.isRequest()) { sipUrl = message.getFromHeader().getNameAddress().getAddress(); } else { sipUrl = message.getFromHeader().getNameAddress().getAddress(); } } final SocketAddress result = CachedAddressResolver.getInstance() .getSIPSocketAddress(sipUrl.getHost(), sipUrl.getPort()); message.setSendTo(result); return result; } return null; }