]> arthur.barton.de Git - netatalk.git/blob - libatalk/rpc/locking.c
Merge master
[netatalk.git] / libatalk / rpc / locking.c
1 /*
2  * Copyright (c) 2011 Frank Lahm
3  * All Rights Reserved.  See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif /* HAVE_CONFIG_H */
9
10 #include <sys/queue.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <pthread.h>
16
17 #include <sys/types.h>
18 #include <sys/ipc.h>
19 #include <sys/shm.h>
20
21 #include "event2/event-config.h"
22
23 #include "event2/event.h"
24 #include "event2/http.h"
25 #include "event2/event_compat.h"
26 #include "event2/http_compat.h"
27 #include "event2/rpc.h"
28 #include "event2/rpc_struct.h"
29
30 #include <atalk/logger.h>
31 #include <atalk/errchk.h>
32 #include <atalk/locking.h>
33 #include <atalk/adouble.h>
34 #include <atalk/bstrlib.h>
35 #include <atalk/bstradd.h>
36 #include <atalk/lockrpc.gen.h>
37
38 EVRPC_HEADER(lock_msg, lock_req, lock_rep)
39 EVRPC_GENERATE(lock_msg, lock_req, lock_rep)
40
41 static struct evrpc_pool *rpc_pool;
42 static struct event_base *ev_base;
43
44 static void ev_log_cb(int severity, const char *msg)
45 {
46     LOG(log_warning, logtype_default, (char *)msg);
47 }
48
49 static void msg_rep_cb(struct evrpc_status *status,
50                        struct lock_req *req,
51                        struct lock_rep *rep,
52                        void *arg)
53 {
54         if (status->error != EVRPC_STATUS_ERR_NONE)
55                 goto done;
56
57 done:
58     event_base_loopexit(ev_base, NULL);
59 }
60
61 static int rpc_dummy(const char *name)
62 {
63     struct lock *lock = NULL;
64         struct lock_req *lock_req = NULL;
65         struct lock_rep *lock_rep = NULL;
66     
67     lock = lock_new();
68     lock_req = lock_req_new();
69     lock_rep = lock_rep_new();
70
71     EVTAG_ASSIGN(lock_req, req_lock, lock);
72     EVTAG_ASSIGN(lock_req, req_filename, name);
73
74     EVRPC_MAKE_REQUEST(lock_msg, rpc_pool, lock_req, lock_rep, msg_rep_cb, NULL);
75
76     event_dispatch();
77 }
78
79 int rpc_lock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
80 {
81     rpc_dummy(cfrombstr(ad->ad_fullpath));
82     return 0;
83 }
84
85 void rpc_unlock(struct adouble *ad, int user)
86 {
87     rpc_dummy(cfrombstr(ad->ad_fullpath));
88 }
89
90 int rpc_tmplock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
91 {
92     rpc_dummy(cfrombstr(ad->ad_fullpath));
93     return 0;
94 }
95
96 /**************************************************************************************
97  * Public functions
98  **************************************************************************************/
99
100 int rpc_init(const char *addr, unsigned short port)
101 {
102     EC_INIT;
103
104     struct evhttp_connection *evcon;
105
106     EC_NULL_LOG(ev_base = event_init());
107     event_set_log_callback(ev_log_cb);
108         EC_NULL_LOG(rpc_pool = evrpc_pool_new(NULL));
109         EC_NULL_LOG(evcon = evhttp_connection_new(addr, port));
110         evrpc_pool_add_connection(rpc_pool, evcon);
111
112 EC_CLEANUP:
113     EC_EXIT;
114 }