View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.sms.transport.ucp;
36  
37  import java.io.ByteArrayOutputStream;
38  import java.io.IOException;
39  import java.io.OutputStream;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  import org.marre.util.StringUtil;
44  
45  /***
46   * Baseclass for UcpMsg.
47   * 
48   * @author Markus Eriksson
49   * @version $Id: UcpMsg.java,v 1.7 2005/11/26 16:37:14 c95men Exp $
50   */
51  public abstract class UcpMsg
52  {
53      protected static final byte STX = (byte) 0x02;
54      protected static final byte ETX = (byte) 0x03;
55  
56      private static Logger log_ = LoggerFactory.getLogger(UcpMsg.class);
57  
58      protected String[] ucpFields_;
59      protected char or_; // 'O' or 'R'
60      protected int trn_;
61      protected byte ot_;
62  
63      public UcpMsg(int nFields)
64      {
65          ucpFields_ = new String[nFields];
66      }
67  
68      public void setField(int field, String value)
69      {
70          ucpFields_[field] = value;
71      }
72  
73      public String getField(int field)
74      {
75          return ucpFields_[field];
76      }
77  
78      protected void setOR(char or)
79      {
80          or_ = or;
81      }
82  
83      protected void setOT(byte ot)
84      {
85          ot_ = ot;
86      }
87  
88      public void setTRN(int trn)
89      {
90          trn_ = trn;
91      }
92  
93      public byte calcChecksum(String data)
94      {
95          int checksum = 0;
96          for (int i = 0; i < data.length(); i++)
97          {
98              checksum = (checksum + data.charAt(i)) % 256;
99          }
100         return (byte) (checksum & 0xff);
101     }
102 
103     public String buildCommand()
104     {
105         StringBuffer command = new StringBuffer(200);
106         int length = 0;
107 
108         // CALC LENGTH
109 
110         // length = trn + len + o|r + ot
111         length = 3 + 5 + 2 + 3;
112         // length += data
113         for (int i = 0; i < ucpFields_.length; i++)
114         {
115             if (ucpFields_[i] != null)
116             {
117                 length += ucpFields_[i].length();
118             }
119             length += 1;
120         }
121         // length += checksum
122         length += 3;
123 
124         // HEADER (TRN/LEN/O|R/OT)
125 
126         // TRN (2 num char)
127         command.append(StringUtil.intToString(trn_, 2));
128         command.append('/');
129         // LEN (5 num char)
130         command.append(StringUtil.intToString(length, 5));
131         command.append('/');
132         // O|R (Char 'O' or 'R')
133         command.append(or_);
134         command.append('/');
135         // OT (2 num char)
136         command.append(StringUtil.intToString(ot_, 2));
137         command.append('/');
138 
139         // DATA
140         for (int i = 0; i < ucpFields_.length; i++)
141         {
142             if (ucpFields_[i] != null)
143             {
144                 command.append(ucpFields_[i]);
145             }
146             command.append('/');
147         }
148 
149         // CHECKSUM
150         command.append(StringUtil.byteToHexString(calcChecksum(command.toString())));
151 
152         return command.toString();
153     }
154 
155     public void writeTo(OutputStream os) throws IOException
156     {
157         // STX
158         os.write(STX);
159 
160         // Command
161         os.write(buildCommand().getBytes());
162 
163         // ETX
164         os.write(ETX);
165     }
166 
167     public byte[] getCommand()
168     {
169         ByteArrayOutputStream baos = new ByteArrayOutputStream(200);
170         try
171         {
172             writeTo(baos);
173         }
174         catch (IOException ex)
175         {
176             log_.debug("getCommand() - Failed to write to an ByteArrayOutputStream!");
177         }
178         return baos.toByteArray();
179     }
180 }