001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.tck.wma.cbs;
028:
029: import com.sun.wma.api.server.CBSServer;
030: import com.sun.tck.wma.Message;
031: import com.sun.tck.wma.MessageConnection;
032: import com.sun.tck.wma.MessageTransportConstants;
033: import com.sun.tck.wma.BinaryMessage;
034: import com.sun.tck.wma.TextMessage;
035: import java.io.IOException;
036:
037: /**
038: * Implements CBSserver interface. This class will be used
039: * by the JDTS test harness to test CBS receive functionality.
040: */
041:
042: public class CBSTestServer implements CBSServer {
043:
044: private CBSMessageConnection cbsmess;
045: private MessageConnection conn = null;
046:
047: private char UCS_CHAR = 0x00a4; // this character does not have equivalent
048:
049: /** The URL scheme for the CBS protocol. */
050: private final String CBS_SCHEME = "cbs:";
051:
052: /**
053: * Construct a new CBS test server.
054: */
055: public CBSTestServer() {
056: }
057:
058: /**
059: * Send a CBS message.
060: *
061: * @param type "gsm7", "ucs2", or "binary"
062: * @param segNum Number of message segemnts
063: * @param address CBS address for the message
064: *
065: */
066: public void send(String type, int segNum, String address) {
067:
068: String urlFragment = address.substring(CBS_SCHEME.length());
069:
070: try {
071: conn = cbsmess.openPrim(urlFragment);
072: } catch (IOException ioe) {
073: System.err.println("Exception thrown by CBS Test server: "
074: + ioe);
075: }
076:
077: int num = 0;
078:
079: if (type.equals("gsm7")) {
080: TextMessage tmsg = (TextMessage) cbsmess.newMessage(
081: MessageConnection.TEXT_MESSAGE, address);
082:
083: // construct the message
084: switch (segNum) {
085: case 1:
086: num = 24;
087: break;
088: case 2:
089: num = 26;
090: break;
091: case 3:
092: num = 50;
093: break;
094: case 4:
095: num = 74;
096: break;
097: default:
098: num = 0;
099: }
100:
101: String long_msg = "";
102: ;
103: for (int i = 0; i < num; i++) {
104: long_msg += "Hello "; // the string contains 6 chars
105: }
106:
107: tmsg.setPayloadText(long_msg);
108:
109: try {
110: conn.send(tmsg);
111: } catch (Exception e) {
112: System.err
113: .println("Exception thrown by CBS Test server: "
114: + e);
115: }
116:
117: } else if (type.equals("ucs2")) {
118: TextMessage tmsg = (TextMessage) cbsmess.newMessage(
119: MessageConnection.TEXT_MESSAGE, address);
120:
121: // construct the message
122: switch (segNum) {
123: case 1:
124: num = 60;
125: break;
126: case 2:
127: num = 68;
128: break;
129: case 3:
130: num = 128;
131: break;
132: case 4:
133: num = 190;
134: break;
135: default:
136: num = 0;
137: }
138:
139: char[] ucs_chars = new char[num];
140:
141: for (int i = 0; i < num; i++) {
142: ucs_chars[i] = UCS_CHAR; // char UCS_CHAR = 0x00a4;
143: }
144: String long_msg = new String(ucs_chars);
145:
146: tmsg.setPayloadText(long_msg);
147:
148: try {
149: conn.send(tmsg);
150: } catch (Exception e) {
151: System.err
152: .println("Exception thrown by CBS Test server: "
153: + e);
154: }
155:
156: } else if (type.equals("binary")) {
157: BinaryMessage bmsg = (BinaryMessage) cbsmess.newMessage(
158: MessageConnection.BINARY_MESSAGE, address);
159:
160: // construct the message
161: switch (segNum) {
162: case 1:
163: num = 130;
164: break;
165: case 2:
166: num = 136;
167: break;
168: case 3:
169: num = 260;
170: break;
171: case 4:
172: num = 390;
173: break;
174: default:
175: num = 0;
176: }
177:
178: byte[] byte_msg = new byte[num];
179: for (int i = 0; i < num; i++) {
180: byte_msg[i] = (byte) i; // create a byte[]
181: }
182:
183: bmsg.setPayloadData(byte_msg);
184:
185: try {
186: conn.send(bmsg);
187: } catch (Exception e) {
188: System.err
189: .println("Exception thrown by CBS Test server: "
190: + e);
191: }
192: }
193:
194: }
195:
196: /**
197: * Initialize CBS server.
198: *
199: */
200: public void init() {
201: cbsmess = new CBSMessageConnection();
202: }
203:
204: /**
205: * CBS server terminates any active operations and frees up
206: * resources..
207: *
208: */
209: public void die() {
210: if (conn != null) {
211: try {
212: conn.close();
213: } catch (Exception e) {
214: System.err
215: .println("Exception thrown by CBS Test server: "
216: + e);
217: }
218: }
219: }
220:
221: }
|