Esempio n. 1
0
  /**
   * Receives the bytes that have been sent over the connection, constructs a <code>Message</code>
   * object, and returns it.
   *
   * <p>If there are no <code>Message</code>s waiting on the connection, this method will block
   * until a message is received, or the <code>MessageConnection</code> is closed.
   *
   * @return a <code>Message</code> object
   * @exception IOException if an I/O error occurs.
   */
  public synchronized Message receive() throws IOException {

    dgc.receive(mess);

    /* get message buffer and extract info */
    byte[] buf = mess.getData();
    SMSPacket packet = new SMSPacket(buf);
    int msgType = packet.getEncodingType();
    int smsPort = packet.getPort();
    long time = packet.getTimeStamp();
    String address = packet.getAddress();
    String phoneNumber = packet.getPhoneNumber();
    int msgLen = packet.getMessageLength();
    byte[] messg = packet.getMessage(msgLen);

    debug("SMS PACKET: encodingType = " + msgType);
    debug("SMS PACKET: port = " + smsPort);
    debug("SMS PACKET: time = " + time);
    debug("SMS PACKET: address = " + address);
    debug("SMS PACKET: Sender's phone number = " + phoneNumber);
    debug("SMS PACKET: message length = " + msgLen);
    debug("SMS PACKET: message:" + new String(messg));

    Message msg = null;
    if (msgType == MessageTransportConstants.GSM_TEXT
        || msgType == MessageTransportConstants.GSM_UCS2) {

      TextMessage textmsg = (TextMessage) newMessage(MessageConnection.TEXT_MESSAGE, address);

      /* Always store text messages as UCS 2 bytes. */
      if (msgType == MessageTransportConstants.GSM_TEXT) {
        messg = TextEncoder.decode(messg);
      }
      ((TextObject) textmsg).setPayloadData(messg);
      msg = textmsg;
    } else {
      BinaryMessage binmsg = (BinaryMessage) newMessage(MessageConnection.BINARY_MESSAGE, address);
      ((BinaryObject) binmsg).setPayloadData(messg);
      msg = binmsg;
    }
    ((MessageObject) msg).setTimeStamp(time);

    return msg;
  }
Esempio n. 2
0
  /**
   * Sends a message over the connection. This method extracts the data payload from the <code>
   * Message</code> object so that it can be sent as a datagram.
   *
   * @param dmsg a <code>Message</code> object.
   * @exception ConnectionNotFoundException if the address is invalid or if no address is found in
   *     the message.
   * @exception IOException if an I/O error occurs.
   */
  public void send(Message dmsg) throws IOException {

    /** Saved timestamp for use with multiple segment records. */
    long sendtime = System.currentTimeMillis();

    byte[] buffer = null;
    String type = null;
    if (dmsg instanceof TextMessage) {
      type = MessageConnection.TEXT_MESSAGE;
      buffer = ((TextObject) dmsg).getPayloadData();
    } else if (dmsg instanceof BinaryMessage) {
      type = MessageConnection.BINARY_MESSAGE;
      buffer = ((BinaryObject) dmsg).getPayloadData();
    }

    /*
     * For text messages choose between UCS-2 or GSM 7-bit
     * encoding.
     */
    int encodingType = MessageTransportConstants.GSM_BINARY;
    if (type.equals(MessageConnection.TEXT_MESSAGE)) {
      byte[] gsm7bytes = TextEncoder.encode(buffer);
      if (gsm7bytes != null) {
        encodingType = MessageTransportConstants.GSM_TEXT;
        buffer = gsm7bytes;
      } else {
        encodingType = MessageTransportConstants.GSM_UCS2;
      }
    }

    mess = new DatagramPacket(buf, MessageTransportConstants.DATAGRAM_PACKET_SIZE);
    mess.setAddress(InetAddress.getByName(clientHost));
    mess.setPort(portOut);

    SMSPacket smsPacket = new SMSPacket();
    smsPacket.setEncodingType(encodingType);
    if (port != null) {
      smsPacket.setPort(Integer.parseInt(port));
    } else {
      smsPacket.setPort(0);
    }
    smsPacket.setTimeStamp(sendtime);

    /*
     * Set target address.
     */
    smsPacket.setAddress(dmsg.getAddress());

    /*
     * Set sender's phone number
     */
    smsPacket.setPhoneNumber(phoneNumber);

    smsPacket.setMessageLength(buffer.length);
    smsPacket.setMessage(buffer);

    debug("SMS PACKET: encoding type = " + encodingType);
    debug("SMS PACKET: port = " + port);
    debug("SMS PACKET: timestamp = " + sendtime);
    debug("SMS PACKET: address = " + dmsg.getAddress());
    debug("SMS PACKET: sender's phone number = " + phoneNumber);
    debug("SMS PACKET: message length = " + buffer.length);
    debug("SMS PACKET: message:" + new String(buffer));
    byte[] buf = smsPacket.getData();

    mess.setData(buf, 0, buf.length);
    dgc.send(mess);
  }