/Sourcecode/zisko/multicastor/program/data/MulticastData.java

https://github.com/Samuirai/MultiCastor · Java · 682 lines · 311 code · 90 blank · 281 comment · 28 complexity · b81e7ead8c363f686ad061dd35c30a64 MD5 · raw file

  1. package zisko.multicastor.program.data;
  2. import java.net.InetAddress;
  3. import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
  4. /**
  5. * Diese Bean-Klasse haellt Informationen ueber einen Multicast.
  6. * Objekte von dieser Klasse werden dafuer benutzt Multicast-
  7. * Informationen innerhalb des Programms zu verteilen.
  8. *
  9. * !!In dieser Klasse ist keinerlei Logik implementiert!!
  10. */
  11. public class MulticastData {
  12. //********************************************
  13. // Daten, die gehalten werden
  14. //********************************************
  15. private InetAddress groupIp = null;
  16. private InetAddress sourceIp = null;
  17. private int udpPort = -1;
  18. private int packetLength = -1;
  19. private int ttl = -1;
  20. private int packetRateDesired = -1;
  21. private int packetRateMeasured = -1;
  22. private Typ typ = Typ.UNDEFINED;
  23. private int threadID = -1;
  24. private String hostID = "";
  25. private boolean active = false;
  26. private int maxInterruptionTime = -1;
  27. private int numberOfInterruptions = -1;
  28. private int averageInterruptionTime = -1;
  29. private int packetLossPerSecond = -1;
  30. private int jitter = -1;
  31. private int traffic = -1;
  32. /** Shows if data from multiple Senders is received */
  33. private senderState senders = senderState.NONE;
  34. /** Total packets sent/received */
  35. private long packetCount = -1;
  36. /** Packet source program */
  37. private Source packetSource = Source.UNDEFINED;
  38. private byte[] mmrpGroupMac;
  39. private byte[] mmrpSourceMac;
  40. private long jitterAvg = -1;
  41. private long packetRateAvg = -1;
  42. private long packetLossPerSecondAvg = -1;
  43. private long trafficAvg = -1;
  44. private int packetLostCount = 0;
  45. private String randomID = "0";
  46. //********************************************
  47. // Eigene Datentypen
  48. //********************************************
  49. public enum Typ {
  50. UNDEFINED, CONFIG, L3_SENDER, L3_RECEIVER, L2_SENDER, L2_RECEIVER
  51. }
  52. public enum Source {
  53. UNDEFINED, HIRSCHMANN, MULTICASTOR
  54. }
  55. public enum senderState {
  56. NONE, SINGLE, RECENTLY_CHANGED, MULTIPLE, NETWORK_ERROR
  57. }
  58. /**
  59. * sets default values for the MulticastData object
  60. */
  61. public void resetValues(){
  62. int d = 0;
  63. if(typ == Typ.L3_RECEIVER){
  64. ttl = d;
  65. packetRateDesired = d;
  66. packetLength = d;
  67. }
  68. maxInterruptionTime = d;
  69. packetRateAvg = d;
  70. packetRateMeasured = d;
  71. numberOfInterruptions = d;
  72. averageInterruptionTime = d;
  73. packetLossPerSecondAvg = d;
  74. jitter = d;
  75. jitterAvg = d;
  76. packetCount = d;
  77. packetLostCount = d;
  78. traffic = d;
  79. trafficAvg = d;
  80. packetSource = Source.UNDEFINED;
  81. }
  82. //********************************************
  83. // Getters und Setters
  84. //********************************************
  85. /**
  86. * returns the multicast ip address
  87. * @return returns the multicast ip address
  88. */
  89. public InetAddress getGroupIp() {
  90. return groupIp;
  91. }
  92. /**
  93. * sets the multicast ip address
  94. * @param groupIp the new ip address
  95. */
  96. public void setGroupIp(InetAddress groupIp) {
  97. this.groupIp = groupIp;
  98. }
  99. /**
  100. * returns the network adapter
  101. * @return returns the network adapter
  102. */
  103. public InetAddress getSourceIp() {
  104. return sourceIp;
  105. }
  106. /**
  107. * sets the network adapter
  108. * @param sourceIp the network adapter to be set
  109. */
  110. public void setSourceIp(InetAddress sourceIp) {
  111. this.sourceIp = sourceIp;
  112. }
  113. /**
  114. * returns the udp port
  115. * @return returns the udp port
  116. */
  117. public int getUdpPort() {
  118. return udpPort;
  119. }
  120. /**
  121. * sets the udp port
  122. * @param udpPort the udp port to be set
  123. */
  124. public void setUdpPort(int udpPort) {
  125. this.udpPort = udpPort;
  126. }
  127. /**
  128. * returns the packet length
  129. * @return returns the packet length
  130. */
  131. public int getPacketLength() {
  132. return packetLength;
  133. }
  134. /**
  135. * sets the packet length
  136. * @param packetLength the packet length to be set
  137. */
  138. public void setPacketLength(int packetLength) {
  139. this.packetLength = packetLength;
  140. }
  141. /**
  142. * returns the time to live
  143. * @return returns the time to live
  144. */
  145. public int getTtl() {
  146. return ttl;
  147. }
  148. /**
  149. * sets the time to live
  150. * @param ttl the time to live to be set
  151. */
  152. public void setTtl(int ttl) {
  153. this.ttl = ttl;
  154. }
  155. /**
  156. * returns the desired packet rate
  157. * @return returns the desired packet rate
  158. */
  159. public int getPacketRateDesired() {
  160. return packetRateDesired;
  161. }
  162. /**
  163. * sets the desired packet rate
  164. * @param packetRateDesired the desired packet rate to be set
  165. */
  166. public void setPacketRateDesired(int packetRateDesired) {
  167. this.packetRateDesired = packetRateDesired;
  168. }
  169. /**
  170. * returns the measured packet rate
  171. * @return returns the measured packet rate
  172. */
  173. public int getPacketRateMeasured() {
  174. return packetRateMeasured;
  175. }
  176. /**
  177. * sets the measured packet rate
  178. * @param packetRateMeasured the measured packet rate to be set
  179. */
  180. public void setPacketRateMeasured(int packetRateMeasured) {
  181. this.packetRateMeasured = packetRateMeasured;
  182. }
  183. /**
  184. * returns the typ (L2/L3 Sender/Receiver)
  185. * @return returns the type
  186. */
  187. public Typ getTyp() {
  188. return typ;
  189. }
  190. /**
  191. * sets the type (L2/L3 Receiver/Sender)
  192. * @param typ the type to be set
  193. */
  194. public void setTyp(Typ typ) {
  195. this.typ = typ;
  196. }
  197. /**
  198. * returns the current thread id of the dataset
  199. * @return returns the thread id
  200. */
  201. public int getThreadID() {
  202. return threadID;
  203. }
  204. /**
  205. * sets the current thread id
  206. * @param threadID the current thread id to be set
  207. */
  208. public void setThreadID(int threadID) {
  209. this.threadID = threadID;
  210. }
  211. /**
  212. * returns the host id associated with the dataset
  213. * receiver: the host id of the sender
  214. * sender: its host id
  215. * @return returns the host id
  216. */
  217. public String getHostID() {
  218. return hostID;
  219. }
  220. /**
  221. * sets the host id
  222. * @param hostID the host id to be set
  223. */
  224. public void setHostID(String hostID) {
  225. this.hostID = hostID;
  226. }
  227. /**
  228. * returns whether the dataset is active right now
  229. * @return returns active state
  230. */
  231. public boolean isActive() {
  232. return active;
  233. }
  234. /**
  235. * sets the active state
  236. * @param active the active state
  237. */
  238. public void setActive(boolean active) {
  239. this.active = active;
  240. }
  241. /**
  242. * returns the number of interruptions
  243. * @return returns the number of interruptions
  244. */
  245. public int getNumberOfInterruptions() {
  246. return numberOfInterruptions;
  247. }
  248. /**
  249. * sets the number of interruptions
  250. * @param numberOfInterruptions the number of interruptions to be set
  251. */
  252. public void setNumberOfInterruptions(int numberOfInterruptions) {
  253. this.numberOfInterruptions = numberOfInterruptions;
  254. }
  255. /**
  256. * returns the average interruption time
  257. * @return returns the average interruption time
  258. */
  259. public int getAverageInterruptionTime() {
  260. return averageInterruptionTime;
  261. }
  262. /**
  263. * sets the average interruption time
  264. * @param averageInterruptionTime the average interruption time to be set
  265. */
  266. public void setAverageInterruptionTime(int averageInterruptionTime) {
  267. this.averageInterruptionTime = averageInterruptionTime;
  268. }
  269. /**
  270. * returns the packet loss / sec
  271. * @return returns the packet los / sec
  272. */
  273. public int getPacketLossPerSecond() {
  274. return packetLossPerSecond;
  275. }
  276. /**
  277. * sets the packet loss / sec
  278. * @param packetLossPerSecond the packet loss / sec to be set
  279. */
  280. public void setPacketLossPerSecond(int packetLossPerSecond) {
  281. this.packetLossPerSecond = packetLossPerSecond;
  282. }
  283. /**
  284. * returns the jitter
  285. * @return returns the jitter
  286. */
  287. public int getJitter() {
  288. return jitter;
  289. }
  290. /**
  291. * sets the jitter
  292. * @param jitter the jitter to be set
  293. */
  294. public void setJitter(int jitter) {
  295. this.jitter = jitter;
  296. }
  297. @Override
  298. public String toString() {
  299. return "MulticastData [\n\nactive=" + active
  300. + "\naverageInterruptionTime=" + averageInterruptionTime
  301. + "\ngroupIp=" + groupIp + "\nsourceIp=" + sourceIp + "\nhostID=" + hostID + "\njitter="
  302. + jitter + "\nnumberOfInterruptions=" + numberOfInterruptions
  303. + "\npacketLength=" + packetLength + "\npacketLossPerSecond="
  304. + packetLossPerSecond + "\npacketRateDesired="
  305. + packetRateDesired + "\npacketRateMeasured="
  306. + packetRateMeasured + "\nthreadID="
  307. + threadID + "\nttl=" + ttl + "\ntyp=" + typ + "\nudpPort="
  308. + udpPort
  309. + "\nactive= " + active + "]";
  310. }
  311. /**
  312. * a string representation of this object - console friendly output
  313. * @return console friendly toString of this object
  314. */
  315. public String toStringConsole(){
  316. if (typ == Typ.L3_SENDER)
  317. return groupIp + "\t" + udpPort + "\t" + sourceIp + "\t" + packetRateDesired + "\t"
  318. + threadID + "\t" + ttl + "\t" + packetCount + "\t" + typ + "\t" + getSenderID() + "\t";
  319. return groupIp + "\t" + udpPort + "\t" + sourceIp + "\t" + jitter + "\t"
  320. + numberOfInterruptions + "\t" + packetLossPerSecond + "\t" + packetRateDesired + "\t"
  321. + packetRateMeasured + "\t" + threadID + "\t" + ttl + "\t" + packetCount + "\t" + typ + "\t" + getSenderID() + "\t" + packetSource + "\t" + senders;
  322. }
  323. /**
  324. * return the type, senderID (hostid, random number, threadid) and mc ip address as String
  325. * @return the identify string
  326. */
  327. public String identify(){
  328. if(typ == Typ.L2_SENDER || typ == Typ.L2_RECEIVER)
  329. return this.typ + "_" + this.getSenderID() + "_" + getMmrpGroupMacAsString();
  330. else
  331. return this.typ + "_" + this.getSenderID() + "_" + getGroupIp();
  332. }
  333. /**
  334. * returns the average packet rate
  335. * @return the average packet rate
  336. */
  337. public long getPacketRateAvg() {
  338. return packetRateAvg;
  339. }
  340. /**
  341. * sets the average packet rate
  342. * @param packetRateAvg the average packet rate to be set
  343. */
  344. public void setPacketRateAvg(long packetRateAvg) {
  345. this.packetRateAvg = packetRateAvg;
  346. }
  347. /**
  348. * returns the average packet loss per sec
  349. * @return the average packet loss per sec
  350. */
  351. public long getPacketLossPerSecondAvg() {
  352. return packetLossPerSecondAvg;
  353. }
  354. /**
  355. * sets the average packet loss per sec
  356. * @param packetLossPerSecondAvg the average packet loss per sec to be set
  357. */
  358. public void setPacketLossPerSecondAvg(long packetLossPerSecondAvg) {
  359. this.packetLossPerSecondAvg = packetLossPerSecondAvg;
  360. }
  361. /**
  362. * returns the average jitter
  363. * @return the average jitter
  364. */
  365. public long getJitterAvg() {
  366. return jitterAvg;
  367. }
  368. /**
  369. * sets the average jitter
  370. * @param jitterAvg the average jitter to be set
  371. */
  372. public void setJitterAvg(long jitterAvg) {
  373. this.jitterAvg = jitterAvg;
  374. }
  375. /**
  376. * returns the current traffic
  377. * @return the current traffic
  378. */
  379. public int getTraffic() {
  380. return traffic;
  381. }
  382. /**
  383. * sets the current traffic
  384. * @param traffic the current traffic to be set
  385. */
  386. public void setTraffic(int traffic) {
  387. this.traffic = traffic;
  388. }
  389. /**
  390. * returns the average traffic
  391. * @return the average traffic
  392. */
  393. public long getTrafficAvg() {
  394. return trafficAvg;
  395. }
  396. /**
  397. * sets the average traffic
  398. * @param trafficAvg the average traffic to be set
  399. */
  400. public void setTrafficAvg(long trafficAvg) {
  401. this.trafficAvg = trafficAvg;
  402. }
  403. /**
  404. * returns the packet count
  405. * @return the packet count
  406. */
  407. public long getPacketCount() {
  408. return packetCount;
  409. }
  410. /**
  411. * sets the packet count
  412. * @param packetCount the packet count to be set
  413. */
  414. public void setPacketCount(long packetCount) {
  415. this.packetCount = packetCount;
  416. }
  417. /**
  418. * Returns a unique SenderID consisting of the hostID, the threadID and a random number to differentiate between multiple instances
  419. * @return a unique sender id
  420. */
  421. public String getSenderID(){
  422. return hostID + threadID + ((randomID != null) ? "-" + randomID : "");
  423. }
  424. /**
  425. * returns the packet source
  426. * @return the packet source
  427. */
  428. public Source getPacketSource() {
  429. return packetSource;
  430. }
  431. /**
  432. * sets the packet source
  433. * @param packetSource the packet source to be set
  434. */
  435. public void setPacketSource(Source packetSource) {
  436. this.packetSource = packetSource;
  437. }
  438. /**
  439. * returns the sender state
  440. * @return the sender state
  441. */
  442. public senderState getSenders() {
  443. return senders;
  444. }
  445. /**
  446. * sets the sender state
  447. * @param senders the sender state to be set
  448. */
  449. public void setSenders(senderState senders) {
  450. this.senders = senders;
  451. }
  452. /**
  453. * sets the maximum interruption time that is allowed
  454. * @param maxInterruptionTime the maximum interruption time
  455. */
  456. public void setMaxInterruptionTime(int maxInterruptionTime) {
  457. this.maxInterruptionTime = maxInterruptionTime;
  458. }
  459. /**
  460. * returns the maximum interruption time
  461. * @return the maximum interruption time
  462. */
  463. public int getMaxInterruptionTime() {
  464. return maxInterruptionTime;
  465. }
  466. /**
  467. * adds lost packets to the lost packets counter
  468. * @param lost the number of packets added to the lost packets counter
  469. */
  470. public void addLostPackets(int lost){
  471. this.packetLostCount += lost;
  472. }
  473. /**
  474. * returns the lost packets
  475. * @return the lost packets
  476. */
  477. public int getLostPackets() {
  478. return packetLostCount;
  479. }
  480. /**
  481. * returns the received packets counter
  482. * @return the received packets
  483. */
  484. public int getReceivedPackets() {
  485. return (int)packetCount;
  486. }
  487. /**
  488. * returns the MMRP multicast MAC address
  489. * @return returns the MAC address as byte array
  490. */
  491. public byte[] getMmrpGroupMac() {
  492. return mmrpGroupMac;
  493. }
  494. /**
  495. * returns the MMRP multicast MAC address
  496. * @return returns the MAC address as String
  497. */
  498. public String getMmrpGroupMacAsString(){
  499. String s = "";
  500. if(mmrpGroupMac != null)
  501. for(int i = 0; i < mmrpGroupMac.length; i++){
  502. String tmp = Integer.toHexString(mmrpGroupMac[i]);
  503. //Falls negativer Wert wird fffffXX zurueckgegeben deswegen nur XX nehmen
  504. if(tmp.length() > 2)
  505. tmp = tmp.substring(tmp.length()-2,tmp.length());
  506. if(tmp.length() == 1)
  507. tmp = "0"+tmp;
  508. s+=tmp;
  509. if(i != (mmrpGroupMac.length-1))
  510. s+= ":";
  511. }
  512. return s;
  513. }
  514. /**
  515. * returns a MMRP MAC address as byte array
  516. * @param s String reprensentation of the MAC address
  517. * @return byte array MAC address
  518. * @throws Exception if string is not a MAC address
  519. */
  520. public byte[] getMMRPFromString(String s) throws Exception {
  521. byte[] b = new byte[6];
  522. String substring;
  523. HexBinaryAdapter h = new HexBinaryAdapter();
  524. for(int begin = 0, end, counter = 0; counter < 6 ; counter++ ){
  525. end = s.indexOf(":", begin);
  526. if(end != -1)
  527. substring = (String)s.subSequence(begin, end);
  528. else
  529. substring = (String)s.subSequence(begin, s.length());
  530. //Shouldn't happen ;)
  531. if(substring.length() == 1)
  532. substring = "0" + substring;
  533. b[counter] = h.unmarshal(substring)[0];
  534. begin = end+1;
  535. }
  536. return b;
  537. }
  538. /**
  539. * returns a Layer2 network adapter as string
  540. * @return string representation of a Layer2 network adapter
  541. */
  542. public String getMmrpSourceMacAsString(){
  543. String s = "";
  544. for(int i = 0; i < mmrpSourceMac.length; i++){
  545. String tmp = Integer.toHexString((int)mmrpSourceMac[i]);
  546. //Falls negativer Wert wird fffffXX zurueckgegeben deswegen nur XX nehmen
  547. if(tmp.length() > 2)
  548. tmp = tmp.substring(tmp.length()-2,tmp.length());
  549. if(tmp.length() == 1)
  550. tmp = "0"+tmp;
  551. s+=tmp;
  552. if(i != (mmrpSourceMac.length-1))
  553. s+= ":";
  554. }
  555. return s;
  556. }
  557. /**
  558. * sets the MMRP MAC address
  559. * @param mmrpGroupMac a MMRP MAC address as byte array
  560. */
  561. public void setMmrpGroupMac(byte[] mmrpGroupMac) {
  562. this.mmrpGroupMac = mmrpGroupMac;
  563. }
  564. /**
  565. * returns Layer2 network adapter
  566. * @return Layer2 network adapter
  567. */
  568. public byte[] getMmrpSourceMac() {
  569. return mmrpSourceMac;
  570. }
  571. /**
  572. * sets the Layer2 network adapter
  573. * @param mmrpSourceMac layer2 network adapter as byte array
  574. */
  575. public void setMmrpSourceMac(byte[] mmrpSourceMac) {
  576. this.mmrpSourceMac = mmrpSourceMac;
  577. }
  578. /**
  579. * sets the random id. Used to differentiate between multiple instances
  580. * @param randomID random id as String
  581. */
  582. public void setRandomID(String randomID) {
  583. this.randomID = randomID;
  584. }
  585. /**
  586. * returns the random id assingt to the dataset
  587. * @return the random id as String
  588. */
  589. public String getRandomID() {
  590. return randomID;
  591. }
  592. }