summaryrefslogtreecommitdiff
path: root/cpp/examples/failover/direct_producer.cpp
blob: 9a510b6fb340933141faeaaa4dd465e45861061e (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
#include <qpid/client/FailoverConnection.h>
#include <qpid/client/Session.h>
#include <qpid/client/AsyncSession.h>
#include <qpid/client/Message.h>


#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <fstream>

#include <sstream>

using namespace qpid::client;
using namespace qpid::framing;

using namespace std;




int 
main ( int argc, char ** argv) 
{
  struct timeval broker_killed_time     = {0,0},
                 failover_complete_time = {0,0},
                 duration               = {0,0};


  if ( argc < 3 )
  {
    std::cerr << "Usage: ./direct_producer host cluster_port_file_name\n";
    std::cerr << "i.e. for host: 127.0.0.1\n";
    exit(1);
  }

  char const * host = argv[1];
  int  port = atoi(argv[2]);
  char const * broker_to_kill = 0;

  if ( argc > 3 )
  {
    broker_to_kill = argv[3];
    std::cerr << "main:  Broker marked for death is process ID " 
              << broker_to_kill
              << endl;
  }
  else
  {
    std::cerr << "PRODUCER main: there is no broker to kill.\n";
  }

  FailoverConnection connection;
  FailoverSession    * session;
  Message message;

  string program_name = "PRODUCER";


  connection.failoverCompleteTime = & failover_complete_time;
  connection.name = program_name;
  connection.open ( host, port );

  session = connection.newSession();
  session->name    = program_name;

  int send_this_many = 30,
      messages_sent  = 0;

  while ( messages_sent < send_this_many )
  {
    if ( (messages_sent == 13) && broker_to_kill )
    {
      char command[1000];
      std::cerr << program_name << " killing broker " << broker_to_kill << ".\n";
      sprintf(command, "kill -9 %s", broker_to_kill);
      system ( command );
      gettimeofday ( & broker_killed_time, 0 );
    }

    message.getDeliveryProperties().setRoutingKey("routing_key"); 

    std::cerr << "sending message " 
              << messages_sent
              << " of " 
              << send_this_many 
              << ".\n";

    stringstream message_data;
    message_data << messages_sent;
    message.setData(message_data.str());

    try
    {
      /* MICK FIXME
      session.messageTransfer ( arg::content=message,  
                                arg::destination="amq.direct"
                              ); */
      session->messageTransfer ( "amq.direct",
                                1,
                                0,
                                message
                              );
    }
    catch ( const std::exception& error) 
    {
      cerr << program_name << " exception: " << error.what() << endl;
    }

    sleep ( 1 );
    ++ messages_sent;
  }

  message.setData ( "That's all, folks!" );

  /* MICK FIXME
  session.messageTransfer ( arg::content=message,  
                            arg::destination="amq.direct"
                          ); 
   */
  session->messageTransfer ( "amq.direct",
                            1,
                            0,
                            message
                          ); 

  session->sync();
  connection.close();

  // This will be incorrect if you killed more than one...
  if ( broker_to_kill )
  {
    timersub ( & failover_complete_time, 
               & broker_killed_time, 
               & duration 
             );
    fprintf ( stderr, 
              "Failover time: %ld.%.6ld\n",
              duration.tv_sec,
              duration.tv_usec
            );
  }

  return 0;  
}