r12696: Reduce the size of include/structs.h
[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
28 static void wreplsrv_out_pull_reschedule(struct wreplsrv_partner *partner, uint32_t interval)
29 {
30         NTSTATUS status;
31
32         partner->pull.next_run = timeval_current_ofs(interval, 0);
33         status = wreplsrv_periodic_schedule(partner->service, interval);
34         if (!NT_STATUS_IS_OK(status)) {
35                 DEBUG(0,("wreplsrv_periodic_schedule() failed\n"));
36         }
37 }
38
39 static void wreplsrv_pull_handler_creq(struct composite_context *creq)
40 {
41         struct wreplsrv_partner *partner = talloc_get_type(creq->async.private_data, struct wreplsrv_partner);
42         struct wreplsrv_pull_cycle_io *old_cycle_io;
43         struct wrepl_table inform_in;
44
45         partner->pull.last_status = wreplsrv_pull_cycle_recv(partner->pull.creq);
46         partner->pull.creq = NULL;
47
48         old_cycle_io = partner->pull.cycle_io;
49         partner->pull.cycle_io = NULL;
50
51         if (NT_STATUS_IS_OK(partner->pull.last_status)) {
52                 partner->pull.error_count = 0;
53                 DEBUG(2,("wreplsrv_pull_cycle(%s): %s\n",
54                          partner->address, nt_errstr(partner->pull.last_status)));
55                 goto done;
56         }
57
58         partner->pull.error_count++;
59
60         if (partner->pull.error_count > 1) {
61                 uint32_t retry_interval;
62
63                 retry_interval = partner->pull.error_count * partner->pull.retry_interval;
64                 retry_interval = MIN(retry_interval, partner->pull.interval);
65
66                 DEBUG(1,("wreplsrv_pull_cycle(%s): %s: error_count: %u: reschedule(%u)\n",
67                          partner->address, nt_errstr(partner->pull.last_status),
68                          partner->pull.error_count, retry_interval));
69
70                 wreplsrv_out_pull_reschedule(partner, retry_interval);
71                 goto done;
72         }
73
74         DEBUG(1,("wreplsrv_pull_cycle(%s): %s: error_count:%u retry\n",
75                  partner->address, nt_errstr(partner->pull.last_status),
76                  partner->pull.error_count));
77         inform_in.partner_count = old_cycle_io->in.num_owners;
78         inform_in.partners      = old_cycle_io->in.owners;
79         wreplsrv_out_partner_pull(partner, &inform_in);
80 done:
81         talloc_free(old_cycle_io);
82 }
83
84 void wreplsrv_out_partner_pull(struct wreplsrv_partner *partner, struct wrepl_table *inform_in)
85 {
86         /* there's already a pull in progress, so we're done */
87         if (partner->pull.creq) return;
88
89         partner->pull.cycle_io = talloc(partner, struct wreplsrv_pull_cycle_io);
90         if (!partner->pull.cycle_io) {
91                 goto nomem;
92         }
93
94         partner->pull.cycle_io->in.partner      = partner;
95         partner->pull.cycle_io->in.wreplconn    = NULL;
96         if (inform_in) {
97                 partner->pull.cycle_io->in.num_owners   = inform_in->partner_count;
98                 partner->pull.cycle_io->in.owners       = inform_in->partners;
99                 talloc_steal(partner->pull.cycle_io, inform_in->partners);
100         } else {
101                 partner->pull.cycle_io->in.num_owners   = 0;
102                 partner->pull.cycle_io->in.owners       = NULL;
103         }
104         partner->pull.creq = wreplsrv_pull_cycle_send(partner->pull.cycle_io, partner->pull.cycle_io);
105         if (!partner->pull.creq) {
106                 DEBUG(1,("wreplsrv_pull_cycle_send(%s) failed\n",
107                          partner->address));
108                 goto nomem;
109         }
110
111         partner->pull.creq->async.fn            = wreplsrv_pull_handler_creq;
112         partner->pull.creq->async.private_data  = partner;
113
114         return;
115 nomem:
116         talloc_free(partner->pull.cycle_io);
117         partner->pull.cycle_io = NULL;
118         DEBUG(0,("wreplsrv_out_partner_pull(%s): no memory? (ignoring)\n",
119                 partner->address));
120         return;
121 }
122
123 NTSTATUS wreplsrv_out_pull_run(struct wreplsrv_service *service)
124 {
125         struct wreplsrv_partner *partner;
126
127         for (partner = service->partners; partner; partner = partner->next) {
128                 /* if it's not a pull partner, go to the next partner */
129                 if (!(partner->type & WINSREPL_PARTNER_PULL)) continue;
130
131                 /* if pulling is disabled for the partner, go to the next one */
132                 if (partner->pull.interval == 0) continue;
133
134                 /* if the next timer isn't reached, go to the next partner */
135                 if (!timeval_expired(&partner->pull.next_run)) continue;
136
137                 wreplsrv_out_pull_reschedule(partner, partner->pull.interval);
138
139                 wreplsrv_out_partner_pull(partner, NULL);
140         }
141
142         return NT_STATUS_OK;
143 }