]> rtime.felk.cvut.cz Git - orte.git/blob - orte/java/src/org/ocera/orte/types/RecvInfo.java
6ddec5343b9aa39c1d7c128e48d8795236feeb6d
[orte.git] / orte / java / src / org / ocera / orte / types / RecvInfo.java
1 /* RecvInfo.java */
2
3 /**
4  * Class RecvInfo.
5  *
6  * @author Lukas Pokorny (lukas_pokorny@centrum.cz)
7  * @author CTU FEE Prague - Department of Control Engineering (dce.felk.cvut.cz)
8  * @author Project ORTE - OCERA Real Time Ethernet (www.ocera.org)
9  * @author dedication to Kj
10  * @version 0.1
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  */
23
24 package org.ocera.orte.types;
25
26 import java.nio.ByteBuffer;
27 import java.nio.ByteOrder;
28
29 public class RecvInfo
30 {
31
32   private ByteBuffer      info_buffer;
33   
34   /*      Helper buffer structure (all 32-bit integers):
35    *       [0]  sizeof(ORTERecvInfo),
36    *       [1]  sizeof(ORTERecvStatus),
37    *       [2]  sizeof(char*),
38    *       [3]  offsetof(ORTERecvInfo,status),
39    *       [4]  offsetof(ORTERecvInfo,topic),
40    *       [5]  offsetof(ORTERecvInfo,type),
41    *       [6]  offsetof(ORTERecvInfo,senderGUID),
42    *       [7]  offsetof(ORTERecvInfo,localTimeReceived),
43    *       [8]  offsetof(ORTERecvInfo,remoteTimePublished),
44    *       [9]  offsetof(ORTERecvInfo,sn),
45    *     [10]  offsetof(GUID_RTPS,hid),
46    *     [11]  offsetof(GUID_RTPS,aid),
47    *     [12]  offsetof(GUID_RTPS,oid),
48    *     [13]  offsetof(NtpTime,seconds),
49    *     [14]  offsetof(NtpTime,fraction),
50    *     [15]  offsetof(SequenceNumber,high),
51    *     [16]  offsetof(SequenceNumber,low)
52    */
53   static ByteBuffer      helper_buffer;
54
55   static {
56     helper_buffer = ByteBuffer.allocateDirect(17*Integer.SIZE/8);
57     helper_buffer.order(ByteOrder.nativeOrder());
58     c_helper(helper_buffer);
59   }
60
61   public RecvInfo()
62   {
63         //System.out.println(":j: instance of RecvInfo created..");
64     this.info_buffer = ByteBuffer.allocateDirect(helper_buffer.getInt(0));
65     this.info_buffer.order(ByteOrder.nativeOrder());
66   }
67
68
69   public byte getRecvStatus()
70   {
71     byte ret_val;
72     
73     switch(helper_buffer.getInt(Integer.SIZE/8)) {
74       case 4:
75         ret_val = (byte) info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*3/8));
76         break;
77       case 1:
78         ret_val = info_buffer.get(helper_buffer.getInt(Integer.SIZE*3/8));
79         break;
80       case 2:
81         ret_val = (byte) info_buffer.getShort(helper_buffer.getInt(Integer.SIZE*3/8));
82         break;
83       case 8:
84         ret_val = (byte) info_buffer.getLong(helper_buffer.getInt(Integer.SIZE*3/8));
85         break;
86     default:
87         System.out.println("j: ORTERecvStatus of unknown size!");
88         return -1;
89     }
90
91     return ret_val;
92   }
93
94   public String getTopic()
95   {
96     return getString(Integer.SIZE*4/8);
97   }
98
99   public String getTypeName()
100   {
101     return getString(Integer.SIZE*5/8);
102   }
103
104   public GUID_RTPS getSenderGuid()
105   {
106     long hostId   = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*6/8)+helper_buffer.getInt(Integer.SIZE*10/8)) & 0xffffffffL;
107     long appId    = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*6/8)+helper_buffer.getInt(Integer.SIZE*11/8)) & 0xffffffffL;
108     long objectId = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*6/8)+helper_buffer.getInt(Integer.SIZE*12/8)) & 0xffffffffL;
109     
110     
111     return new GUID_RTPS(hostId, appId, objectId);
112   }
113
114   public NtpTime getLocalTimeRecv()
115   {
116     int  seconds  = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*7/8)+helper_buffer.getInt(Integer.SIZE*13/8));
117     long fraction = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*7/8)+helper_buffer.getInt(Integer.SIZE*14/8)) & 0xffffffffL;
118
119     return new NtpTime(seconds, fraction);
120   }
121
122   public NtpTime getRemoteTimePub()
123   {
124     int  seconds  = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*8/8)+helper_buffer.getInt(Integer.SIZE*13/8));
125     long fraction = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*8/8)+helper_buffer.getInt(Integer.SIZE*14/8)) & 0xffffffffL;
126
127     return new NtpTime(seconds, fraction);
128   }
129
130   public SequenceNumber getSeqNumber()
131   {
132     int high = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*9/8)+helper_buffer.getInt(Integer.SIZE*15/8));
133     long low  = info_buffer.getInt(helper_buffer.getInt(Integer.SIZE*9/8)+helper_buffer.getInt(Integer.SIZE*16/8));
134
135     return new SequenceNumber(high, low);
136   }
137
138   /** only for test purposes */
139   public void print()
140   {
141           System.out.println(":j: RecvInfo:");
142           System.out.println(":j:    recvStatus: " + this.getRecvStatus());
143           System.out.println(":j:         topic: " + this.getTopic()); 
144           System.out.println(":j:          type: " + this.getTypeName()); 
145       System.out.println(":j:    senderGuid: " + this.getSenderGuid()); 
146       System.out.println(":j: localTimerecv: " + this.getLocalTimeRecv()); 
147       System.out.println(":j: remoteTimePub: " + this.getRemoteTimePub()); 
148     System.out.println(":j:         seqNr: " + this.getSeqNumber().getDecimal());       
149   }
150   
151   public ByteBuffer getBuffer()
152   {
153     return this.info_buffer;
154   }
155
156   private String getString(int offset) {
157     long string_pointer;
158     
159     switch(helper_buffer.getInt(Integer.SIZE*2/8)) {
160       case 4:
161         string_pointer = info_buffer.getInt(helper_buffer.getInt(offset));
162         break;
163       case 8:
164         string_pointer = info_buffer.getLong(helper_buffer.getInt(offset));
165         break;
166       case 1:
167         string_pointer = info_buffer.get(helper_buffer.getInt(offset));
168         break;
169       case 2:
170         string_pointer = info_buffer.getShort(helper_buffer.getInt(offset));
171         break;
172     }
173     
174     return get_string(string_pointer);
175   }
176
177   /* NATIVE FUNCTIONS */
178   
179   static native String get_string(long string_pointer);
180   static native void c_helper(ByteBuffer buffer);
181   
182 }