View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      /* (non-Javadoc)
36       * @see org.jdiagnose.remote.RemoteResultSender#send(org.jdiagnose.RemoteResult)
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  }