r12608: Remove some unused #include lines.
[kai/samba.git] / source4 / wrepl_server / wrepl_out_pull.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/ndr_winsrepl.h"
25 #include "wrepl_server/wrepl_server.h"
26 #include "libcli/composite/composite.h"
27 #include "wrepl_server/wrepl_out_helpers.h"
28
29 static void wreplsrv_out_pull_reschedule(struct wreplsrv_partner *partner, uint32_t interval)
30 {
31         NTSTATUS status;
32
33         partner->pull.next_run = timeval_current_ofs(interval, 0);
34         status = wreplsrv_periodic_schedule(partner->service, interval);
35         if (!NT_STATUS_IS_OK(status)) {
36                 DEBUG(0,("wreplsrv_periodic_schedule() failed\n"));
37         }
38 }
39
40 static void wreplsrv_pull_handler_creq(struct composite_context *creq)
41 {
42         struct wreplsrv_partner *partner = talloc_get_type(creq->async.private_data, struct wreplsrv_partner);
43         struct wreplsrv_pull_cycle_io *old_cycle_io;
44         struct wrepl_table inform_in;
45
46         partner->pull.last_status = wreplsrv_pull_cycle_recv(partner->pull.creq);
47         partner->pull.creq = NULL;
48
49         old_cycle_io = partner->pull.cycle_io;
50         partner->pull.cycle_io = NULL;
51
52         if (NT_STATUS_IS_OK(partner->pull.last_status)) {
53                 partner->pull.error_count = 0;
54                 DEBUG(2,("wreplsrv_pull_cycle(%s): %s\n",
55                          partner->address, nt_errstr(partner->pull.last_status)));
56                 goto done;
57         }
58
59         partner->pull.error_count++;
60
61         if (partner->pull.error_count > 1) {
62                 uint32_t retry_interval;
63
64                 retry_interval = partner->pull.error_count * partner->pull.retry_interval;
65                 retry_interval = MIN(retry_interval, partner->pull.interval);
66
67                 DEBUG(1,("wreplsrv_pull_cycle(%s): %s: error_count: %u: reschedule(%u)\n",
68                          partner->address, nt_errstr(partner->pull.last_status),
69                          partner->pull.error_count, retry_interval));
70
71                 wreplsrv_out_pull_reschedule(partner, retry_interval);
72                 goto done;
73         }
74
75         DEBUG(1,("wreplsrv_pull_cycle(%s): %s: error_count:%u retry\n",
76                  partner->address, nt_errstr(partner->pull.last_status),
77                  partner->pull.error_count));
78         inform_in.partner_count = old_cycle_io->in.num_owners;
79         inform_in.partners      = old_cycle_io->in.owners;
80         wreplsrv_out_partner_pull(partner, &inform_in);
81 done:
82         talloc_free(old_cycle_io);
83 }
84
85 void wreplsrv_out_partner_pull(struct wreplsrv_partner *partner, struct wrepl_table *inform_in)
86 {
87         /* there's already a pull in progress, so we're done */
88         if (partner->pull.creq) return;
89
90         partner->pull.cycle_io = talloc(partner, struct wreplsrv_pull_cycle_io);
91         if (!partner->pull.cycle_io) {
92                 goto nomem;
93         }
94
95         partner->pull.cycle_io->in.partner      = partner;
96         partner->pull.cycle_io->in.wreplconn    = NULL;
97         if (inform_in) {
98                 partner->pull.cycle_io->in.num_owners   = inform_in->partner_count;
99                 partner->pull.cycle_io->in.owners       = inform_in->partners;
100                 talloc_steal(partner->pull.cycle_io, inform_in->partners);
101         } else {
102                 partner->pull.cycle_io->in.num_owners   = 0;
103                 partner->pull.cycle_io->in.owners       = NULL;
104         }
105         partner->pull.creq = wreplsrv_pull_cycle_send(partner->pull.cycle_io, partner->pull.cycle_io);
106         if (!partner->pull.creq) {
107                 DEBUG(1,("wreplsrv_pull_cycle_send(%s) failed\n",
108                          partner->address));
109                 goto nomem;
110         }
111
112         partner->pull.creq->async.fn            = wreplsrv_pull_handler_creq;
113         partner->pull.creq->async.private_data  = partner;
114
115         return;
116 nomem:
117         talloc_free(partner->pull.cycle_io);
118         partner->pull.cycle_io = NULL;
119         DEBUG(0,("wreplsrv_out_partner_pull(%s): no memory? (ignoring)\n",
120                 partner->address));
121         return;
122 }
123
124 NTSTATUS wreplsrv_out_pull_run(struct wreplsrv_service *service)
125 {
126         struct wreplsrv_partner *partner;
127
128         for (partner = service->partners; partner; partner = partner->next) {
129                 /* if it's not a pull partner, go to the next partner */
130                 if (!(partner->type & WINSREPL_PARTNER_PULL)) continue;
131
132                 /* if pulling is disabled for the partner, go to the next one */
133                 if (partner->pull.interval == 0) continue;
134
135                 /* if the next timer isn't reached, go to the next partner */
136                 if (!timeval_expired(&partner->pull.next_run)) continue;
137
138                 wreplsrv_out_pull_reschedule(partner, partner->pull.interval);
139
140                 wreplsrv_out_partner_pull(partner, NULL);
141         }
142
143         return NT_STATUS_OK;
144 }