connectivity/com.nokia.tcf.test/src/com/nokia/tcf/test/TestPlatSimTRK.java
changeset 59 c892c53c664e
parent 44 e4eb00aa1a3f
child 60 9d2210c8eed2
equal deleted inserted replaced
44:e4eb00aa1a3f 59:c892c53c664e
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.tcf.test;
       
    19 
       
    20 import java.io.IOException;
       
    21 import java.nio.ByteBuffer;
       
    22 
       
    23 import org.eclipse.core.runtime.IStatus;
       
    24 
       
    25 import com.nokia.tcf.api.ITCMessage;
       
    26 import com.nokia.tcf.api.ITCMessageIds;
       
    27 import com.nokia.tcf.api.ITCMessageInputStream;
       
    28 import com.nokia.tcf.api.ITCMessageOptions;
       
    29 import com.nokia.tcf.api.ITCRealTCPConnection;
       
    30 import com.nokia.tcf.api.ITCAPIConnection;
       
    31 import com.nokia.tcf.api.TCFClassFactory;
       
    32 import com.nokia.tcf.impl.TCAPIConnection;
       
    33 
       
    34 import junit.framework.TestCase;
       
    35 
       
    36 /**
       
    37  *
       
    38  */
       
    39 public class TestPlatSimTRK extends TestCase {
       
    40 
       
    41 	public void testPlatSimTRK() {
       
    42 		// connection
       
    43 		// PlatSim 
       
    44 		ITCRealTCPConnection conn = (ITCRealTCPConnection)TCFClassFactory.createITCRealTCPConnection("127.0.0.1", "7654");
       
    45 		conn.setDecodeFormat("platsim");
       
    46 
       
    47 		// message options
       
    48 		ITCMessageOptions options = TCFClassFactory.createITCMessageOptions();
       
    49 		// Ask TCF to encode the protocol (we will send raw TRK messages)
       
    50 		options.setMessageEncodeFormat(ITCMessageOptions.ENCODE_FORMAT);
       
    51 		// ask TCF to delete the protocol on incoming messages
       
    52 		options.setUnWrapFormat(ITCMessageOptions.UNWRAP_DELETE_HEADERS);
       
    53 //		options.setInputStreamOverflowOption(ITCMessageOptions.INPUTSTREAM_OVERFLOW_NONE);
       
    54 		options.setInputStreamSize(64);
       
    55 		
       
    56 		// message Ids to capture (we must capture TRK response messages = id=0x45)
       
    57 		ITCMessageIds ids = TCFClassFactory.createITCMessageIds();
       
    58 		// The PN msg ID for TRK responses is 0x45 per DSS
       
    59 		ids.addMessageId((byte)0x45); // TRK response
       
    60 
       
    61 		// TRK ping message
       
    62 		// RAW message
       
    63 		byte[] trkping = {0x7e, 0, 0, (byte)0xff, 0x7e};
       
    64 		// convert to ITCMessage
       
    65 		ITCMessage tcMsgPing = TCFClassFactory.createITCMessage(trkping);
       
    66 		// Ask TCF to use the PN msg ID for TRK requests (0x44) per DSS
       
    67 		tcMsgPing.setUseMyMessageId(true, (byte)0x44);
       
    68 		
       
    69 		// TRK version message
       
    70 		// RAW message
       
    71 		byte[] trkversion = {0x7e, 0x08, 0x01, (byte)0xf6, 0x7e};
       
    72 		// convert to ITCMessage
       
    73 		ITCMessage tcMsgVersion = TCFClassFactory.createITCMessage(trkversion);
       
    74 		// Ask TCF to use the PN msg ID for TRK requests (0x44) per DSS
       
    75 		tcMsgVersion.setUseMyMessageId(true, (byte)0x44);
       
    76 
       
    77 		// uncomment this to do a non-plugin Junit
       
    78 		TCAPIConnection api2 = new TCAPIConnection();
       
    79 		api2.nativeStartServer();
       
    80 
       
    81 		int iconnect = 0;
       
    82 		for (iconnect=0; iconnect < 1; iconnect++) {
       
    83 			// connect
       
    84 			ITCAPIConnection api = TCFClassFactory.createITCAPIConnection();
       
    85 			IStatus connStatus = api.connect(conn, options, ids);
       
    86 			System.out.printf("iconnect=%d connStatus=%d\n", iconnect, connStatus.getCode());
       
    87 			
       
    88 			// get stream reference
       
    89 			ITCMessageInputStream stream = null;
       
    90 			if (connStatus.isOK()) {
       
    91 				stream = api.getInputStream();
       
    92 				System.out.printf("stream=%s\n", stream.toString());
       
    93 			}
       
    94 			
       
    95 			// send trk ping
       
    96 			if (connStatus.isOK()) {
       
    97 				IStatus sendStatus = api.sendMessage(tcMsgPing);
       
    98 				if (sendStatus.isOK()) {
       
    99 					// wait for response
       
   100 					int numberMessages = 0;
       
   101 					int numberPeeks = 0;
       
   102 					int numberSleeps = 0;
       
   103 					for (int i = 0; i < 200; i ++) {
       
   104 						try {
       
   105 							if (stream != null)
       
   106 								numberMessages = stream.peekMessages();
       
   107 							numberPeeks++;
       
   108 							if (numberMessages < 1) {
       
   109 								try {
       
   110 									Thread.sleep(100);
       
   111 									numberSleeps++;
       
   112 								} catch (InterruptedException e) {
       
   113 									e.printStackTrace();
       
   114 								}
       
   115 							}
       
   116 						} catch (IOException e) {
       
   117 							e.printStackTrace();
       
   118 						}
       
   119 						if (numberMessages >= 1) {
       
   120 							break;
       
   121 						}
       
   122 					}
       
   123 					System.out.printf("NumberMessages = %d numberPeeks = %d numberSleeps = %d\n", numberMessages, numberPeeks, numberSleeps);
       
   124 					if (numberMessages > 0) {
       
   125 						try {
       
   126 							
       
   127 //							ITCMessage[] message = stream.readMessages(1);
       
   128 //							byte[] b = stream.readBytes(1);
       
   129 							ByteBuffer barray = java.nio.ByteBuffer.wrap(stream.readBytes(1));
       
   130 //							byte[] b = message[0].getMessage();
       
   131 							System.out.printf("msg: length=%d\n", barray.limit());
       
   132 							byte[] b = barray.array();
       
   133 							for (int i = 0; i < b.length; i++) {
       
   134 								System.out.printf("%x ", b[i]);
       
   135 							}
       
   136 							System.out.printf("\n");
       
   137 							numberMessages = stream.peekMessages();
       
   138 						} catch (IOException e) {
       
   139 							e.printStackTrace();
       
   140 						}
       
   141 					}
       
   142 				}
       
   143 			}
       
   144 			// send trk version
       
   145 			if (connStatus.isOK()) {
       
   146 				IStatus sendStatus = api.sendMessage(tcMsgVersion); 
       
   147 				if (sendStatus.isOK()) {
       
   148 					// wait for response
       
   149 					int numberMessages = 0;
       
   150 					int numberPeeks = 0;
       
   151 					int numberSleeps = 0;
       
   152 					for (int i = 0; i < 200; i ++) {
       
   153 						try {
       
   154 							if (stream != null)
       
   155 								numberMessages = stream.peekMessages();
       
   156 							numberPeeks++;
       
   157 							if (numberMessages < 1) {
       
   158 								try {
       
   159 									Thread.sleep(100);
       
   160 									numberSleeps++;
       
   161 								} catch (InterruptedException e) {
       
   162 									e.printStackTrace();
       
   163 								}
       
   164 							}
       
   165 						} catch (IOException e) {
       
   166 							e.printStackTrace();
       
   167 						}
       
   168 						if (numberMessages == 1) {
       
   169 							break;
       
   170 						}
       
   171 					}
       
   172 					System.out.printf("NumberMessages = %d numberPeeks = %d numberSleeps = %d\n", numberMessages, numberPeeks, numberSleeps);
       
   173 					if (numberMessages > 0) {
       
   174 						try {
       
   175 							
       
   176 							ITCMessage message = stream.readMessage();
       
   177 							byte[] b = message.getMessage();
       
   178 							System.out.printf("msg: length=%d\n", b.length);
       
   179 							for (int i = 0; i < b.length; i++) {
       
   180 								System.out.printf("%x ", b[i]);
       
   181 							}
       
   182 							System.out.printf("\n");
       
   183 							numberMessages = stream.peekMessages();
       
   184 						} catch (IOException e) {
       
   185 							e.printStackTrace();
       
   186 						}
       
   187 					}
       
   188 				}
       
   189 			}
       
   190 			
       
   191 			if (connStatus.isOK()) {
       
   192 				api.disconnect();
       
   193 			}
       
   194 			try {
       
   195 				Thread.sleep(1000);
       
   196 			} catch (InterruptedException e) {
       
   197 				e.printStackTrace();
       
   198 			}
       
   199 		}
       
   200 		// uncomment this when using a non-plugin junit test
       
   201 		api2.nativeStopServer();
       
   202 		
       
   203 	}
       
   204 }