summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/AV/sfp.h
blob: c6b3212cb5fd309038e36278ad4cdedce506f068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    AVStreams.
//
// = FILENAME
//   sfp.h
//
// = AUTHOR
//    Nagarajan Surendran <naga@cs.wustl.edu>
//
// ============================================================================

#if !defined (TAO_AV_SFP_H)
#define TAO_AV_SFP_H

#include "orbsvcs/orbsvcs_export.h"
#include "ace/SOCK_Dgram.h"
#include "orbsvcs/sfpC.h"

#define MAGIC_NUMBER_LEN 5
#define MESSAGE_TYPE_OFFSET 5
#define TAO_WRITEV_MAX 128


#define SFP_MAX_PACKET_SIZE ACE_MAX_DGRAM_SIZE
//#define SFP_MAX_PACKET_SIZE 100


// various message class for SFP.

class TAO_ORBSVCS_Export SFP_Callback
  // =TITLE
  //    Callback interface for SFP.
  //
  // =Description
  //    Application should create a callback object which they
  //    register with the SFP. The SFP implementation notifies the
  //    applicationn of any changes in the stream status like stream
  //    established, stream ended.
{
public:
  virtual int start_failed (void) = 0;
  // This is called for both active and passive start.
  
  virtual int stream_established (void) = 0;
  // This is a callback for both active and passive stream
  // establshment.
  
  virtual int receive_frame (ACE_Message_Block *frame) =0;
  // upcall to the application to receive a frame.

  virtual void end_stream (void) = 0;
  // called when the EndofStream message is received.
};

class TAO_SFP_Fragment_Node
{
public:
  TAO_SFP_Fragment_Node (void)
    :data_ (0)
    {}
  flowProtocol::fragment fragment_info_;
  ACE_Message_Block *data_;
  friend int operator< (const TAO_SFP_Fragment_Node& left,
                        const TAO_SFP_Fragment_Node& right);
};

class TAO_SFP_Fragment_Table_Entry
{
public:
  TAO_SFP_Fragment_Table_Entry (void)
    :last_received_ (0),
     num_fragments_ (0)
    {}
  int last_received_;
  size_t num_fragments_;
  ACE_Ordered_MultiSet<TAO_SFP_Fragment_Node> fragment_set_;
};

typedef ACE_Ordered_MultiSet_Iterator<TAO_SFP_Fragment_Node> FRAGMENT_SET_ITERATOR;

class TAO_ORBSVCS_Export TAO_SFP :public virtual ACE_Event_Handler
  // = TITLE
  //     SFP implementation on UDP.
  //
  // = Description
  //     This implements the methods to send and receive data octet
  //     streams using the Simple Flow Protocol.

{
public:
  // default arguments to pass to use for the ORB
  static const char *TAO_SFP_ORB_ARGUMENTS;

  // SFP magic numbers
  static const char *TAO_SFP_MAGIC_NUMBER;
  static const char *TAO_SFP_FRAGMENT_MAGIC_NUMBER;
  static const char *TAO_SFP_START_MAGIC_NUMBER;
  static const char *TAO_SFP_CREDIT_MAGIC_NUMBER;
  static const char *TAO_SFP_STARTREPLY_MAGIC_NUMBER;

  // SFP version 1.0
  static const unsigned char TAO_SFP_MAJOR_VERSION;
  static const unsigned char TAO_SFP_MINOR_VERSION;

// lengths of various SFP headers
  static const unsigned char TAO_SFP_FRAME_HEADER_LEN;
  enum State
  {
    ACTIVE_START,
    PASSIVE_START,
    TIMEDOUT_T1,
    TIMEDOUT_T2,
    REPLY_RECEIVED,
    START_RECEIVED
  };

  TAO_SFP (CORBA::ORB_ptr orb,
           ACE_Reactor* reactor,
           ACE_Time_Value timeout1,
           ACE_Time_Value timeout2,
           SFP_Callback *callback);
  // constructor.

  virtual int start_stream (const char *receiver_addr);
  // Actively start the stream by trying to connect to the UDP
  // receiver_addr in host:port format.

  virtual int start_stream (const char *local_addr,int credit_);
  // Passive start.

  virtual int send_frame (ACE_Message_Block *frame);
  // sends a single frame over UDP.

  virtual int end_stream (void);
  // terminates the stream.

  virtual int handle_input (ACE_HANDLE fd);
  // Callback when event happens on the dgram socket.

  virtual int handle_timeout (const ACE_Time_Value&, const void*);
  // Used for timeout for the number of tries for starting a stream.

  virtual ACE_HANDLE get_handle (void) const;
private:

  ACE_Message_Block* read_simple_frame (void);
  // receives a single frame from the network.

  int read_frame_header (flowProtocol::frameHeader &frame_header);
  // reads the frame header from the peek buffer in the datagram.

  ACE_Message_Block* read_fragment (void);
  // reads a fragment from the wire.

  void create_local_dgram (void);
  // Create the local dgram endpoint.

  int connect_to_receiver (const char *receiver_addr);
  // Creates a connected dgram with the receiver addr.

  int send_start (void);
  // sends the start message to the receiver.

  int send_startReply (void);
  // sends the StartReply message to the receiver.

  int send_cdr_buffer (TAO_OutputCDR &cdr,
                       ACE_Message_Block *mb);
  // sends the cdr buffer using iovecs.

  int register_dgram_handler (void);
  // registers the dgram socket with the reactor.

  ACE_Message_Block *check_all_fragments (TAO_SFP_Fragment_Table_Entry *fragment_entry);
  // checks if all the fragments for this entry has been received and returns the 
  // head of the chain of message blocks for that frame.

  void dump_buf (char *buf,int n);
  // dumps the buffer to the screen.

  CORBA::ORB_ptr orb_;
  // ORB reference.

  ACE_Reactor* reactor_;
  // Used for registering the dgram handler.

  TAO_OutputCDR output_cdr_;
  // Use the TAO CDR decoder to decode everything

  //  TAO_InputCDR input_cdr_;
  // Use the TAO CDR encoder to encode everything
  
  ACE_SOCK_Dgram dgram_;
  // Connection Oriented Dgram.

  int start_tries_;
  // Number of tries to send a Start message.

  int startReply_tries_;
  // Number of tries to send a StartReply message.

  ACE_Time_Value timeout1_;
  // Timeout used for Start on Sender side and also for Credit on
  // receiver side.
  
  ACE_Time_Value timeout2_;
  // Timeout used for StartReply on the receiver side and also for
  // CREDIT on the sender side.

  State state_;
  // State variable.

  const char *receiver_addr_;
  // The address of the receiver to which we're connected to.
  
  ACE_INET_Addr receiver_inet_addr_;
  // INET addr of the receiver.

  SFP_Callback *callback_;
  // Application Callback Object.
  
  int sequence_num_;
  // sequence number of the packet.

  flowProtocol::frameHeader frame_header_;
  ssize_t frame_header_len_;
  // frame header to be sent with all frames.
  // length of the frame header.

  flowProtocol::fragment fragment_;
  ssize_t fragment_len_;
  // fragment header for each fragment.

  flowProtocol::Start start_;
  ssize_t start_len_;
  // Start message and its length.

  flowProtocol::StartReply start_reply_;
  ssize_t start_reply_len_;
  // StartReply message and its length.
  
  flowProtocol::credit credit_;
  ssize_t credit_len_;
  CORBA::ULong credit_num_;
  // Credit message and its  length.

  char magic_number_[MAGIC_NUMBER_LEN];
  // used for peeking the magic_number.
  const size_t magic_number_len_;
  u_int more_fragments_;
  // boolean flags indicating that there are more fragments.
  ACE_Hash_Map_Manager<CORBA::ULong,TAO_SFP_Fragment_Table_Entry*,ACE_Null_Mutex> fragment_table_;
  // chain of fragments of the current frame.
};

#endif /* !defined (TAO_SFP_H) */