1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.jdiagnose.remote.comms;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  import java.net.DatagramPacket;
22  
23  import org.jdiagnose.DiagnosticMessage;
24  import org.jdiagnose.RemoteResult;
25  import org.jdiagnose.ResultInfo;
26  import org.jdiagnose.remote.RemoteResultException;
27  import org.jdiagnose.remote.RemoteResultSender;
28  import org.jdiagnose.runtime.ResultState;
29  
30  /***
31   * @author jmccrindle
32   */
33  public class MulticastSender extends MulticastSupport implements RemoteResultSender {
34  
35      
36  
37  
38      public void send(RemoteResult result) throws RemoteResultException {
39          ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
40          DataOutputStream out = new DataOutputStream(byteOut);
41          try {
42              out.writeUTF(result.getGuid());
43              out.writeUTF(result.getAgent());
44              out.writeUTF(result.getHost());
45              out.writeLong(result.getSequenceNumber());
46              ResultInfo resultInfo = result.getResultInfo();
47              out.writeUTF(resultInfo.getName());
48              out.writeLong(resultInfo.getDuration());
49              out.writeLong(resultInfo.getStartTime());
50              out.writeLong(resultInfo.getFinishTime());
51              out.writeBoolean(resultInfo.getState() == ResultState.SUCCEEDED);
52              out.flush();
53              int size = byteOut.size();
54              if(size > getLength()) {
55                  throw new RemoteResultException("Message too long");
56              }
57              DiagnosticMessage message = resultInfo.getMessage();
58              if(message != null) {
59                  String summary = message.getSummary();
60                  byte[] messageBytes = summary.getBytes("UTF-8");
61                  if(messageBytes.length < getLength() - size - 1) {
62                      out.writeUTF(summary);
63                  } else {
64                      out.writeUTF("");
65                  }
66                  String body = message.getBody();
67                  byte[] bodyBytes = body.getBytes("UTF-8");
68                  size = byteOut.size();
69                  if(bodyBytes.length < getLength() - size - 1) {
70                      out.writeUTF(body);
71                  } else {
72                      out.writeUTF("");
73                  }
74              } else {
75                  out.writeUTF("");
76                  out.writeUTF("");
77              }
78              byte[] bytes = byteOut.toByteArray();
79              DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
80              getSocket().send(packet);
81          } catch (RemoteResultException e) {
82              throw e;
83          } catch (IOException e) {
84              throw new RemoteResultException(e);
85          }
86      }
87  
88  }