~ubuntu-branches/ubuntu/saucy/ricochet/saucy-proposed

« back to all changes in this revision

Viewing changes to ricochet_0.2.orig/client-link.5c

  • Committer: Package Import Robot
  • Author(s): Keith Packard
  • Date: 2012-06-11 13:37:57 UTC
  • Revision ID: package-import@ubuntu.com-20120611133757-zn0ukd22vz56ymto
Tags: 0.3
* Improve appearance of board
* Fix user list when removing/adding same user

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; version 2 of the License.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
 * General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
15
 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16
 
 */
17
 
 
18
 
autoload RR;
19
 
autoload RR::Lex;
20
 
autoload RR::Send;
21
 
autoload Client;
22
 
autoload Client::Net;
23
 
autoload Mutex;
24
 
autoload List;
25
 
 
26
 
extend namespace Client {
27
 
        public namespace Link {
28
 
 
29
 
                public typedef List::list_t + struct {
30
 
                        string[*]       reply;
31
 
                } message_struct;
32
 
 
33
 
                public typedef *message_struct message_t;
34
 
 
35
 
                public exception link_error(string);
36
 
 
37
 
                typedef struct {
38
 
                        file            f;
39
 
                        string          host;
40
 
                        int             port;
41
 
                        List::list_t    replies;
42
 
                        semaphore       replies_sem;
43
 
                        List::list_t    notices;
44
 
                        semaphore       notices_sem;
45
 
                        bool            closing;
46
 
                        Mutex::mutex    command_lock;
47
 
                        Mutex::mutex    notice_lock;
48
 
                        Mutex::mutex    reply_lock;
49
 
                        thread          reader;
50
 
                } link_struct;
51
 
 
52
 
                public typedef *link_struct link_t;
53
 
 
54
 
                public exception link_closing();
55
 
 
56
 
                void read_replies(link_t l) {
57
 
                        try {
58
 
                                for (;;) {
59
 
                                        message_t m = &(message_struct) { .reply = RR::Lex::recv(l->f) };
60
 
                                        if (dim(m->reply) == 0)
61
 
                                                continue;
62
 
                                        twixt (Mutex::acquire(l->reply_lock); Mutex::release(l->reply_lock)) {
63
 
                                                if (m->reply[0] == "NOTICE") {
64
 
                                                        List::append(m, &l->notices);
65
 
                                                        Semaphore::signal(l->notices_sem);
66
 
                                                } else {
67
 
                                                        List::append(m, &l->replies);
68
 
                                                        Semaphore::signal(l->replies_sem);
69
 
                                                }
70
 
                                        }
71
 
                                }
72
 
                        } catch Thread::signal(int sig) {
73
 
                        } catch File::io_eof(file f) {
74
 
                                l->closing = true;
75
 
                        } catch File::io_error(string reason, File::error_type error, file f) {
76
 
                                l->closing = true;
77
 
                        }
78
 
                        while (Semaphore::count(l->notices_sem) < 0)
79
 
                                Semaphore::signal(l->notices_sem);
80
 
                        while (Semaphore::count(l->replies_sem) < 0)
81
 
                                Semaphore::signal(l->replies_sem);
82
 
                }
83
 
 
84
 
                public message_t command(link_t l, string format, poly args...) {
85
 
                        message_t reply;
86
 
                        twixt (Mutex::acquire(l->command_lock); Mutex::release(l->command_lock)) {
87
 
                                try {
88
 
                                        RR::Send::send(l->f, format, args...);
89
 
                                        File::flush(l->f);
90
 
                                } catch File::io_error(string reason, File::error_type error, file f) {
91
 
                                        raise link_error(sprintf("I/O error on link: %s", reason));
92
 
                                }
93
 
                                Semaphore::wait(l->replies_sem);
94
 
                                twixt(Mutex::acquire(l->reply_lock); Mutex::release(l->reply_lock)) {
95
 
                                        if (l->closing)
96
 
                                                raise link_closing();
97
 
                                        reply = List::first(&l->replies);
98
 
                                        List::remove(reply);
99
 
                                }
100
 
                        }
101
 
                        return reply;
102
 
                }
103
 
 
104
 
                public message_t notice(link_t l) {
105
 
                        message_t notice;
106
 
                        twixt (Mutex::acquire(l->notice_lock); Mutex::release(l->notice_lock)) {
107
 
                                Semaphore::wait(l->notices_sem);
108
 
                                twixt(Mutex::acquire(l->reply_lock); Mutex::release(l->reply_lock)) {
109
 
                                        if (l->closing)
110
 
                                                raise link_closing();
111
 
                                        notice = List::first(&l->notices);
112
 
                                        List::remove(notice);
113
 
                                }
114
 
                        }
115
 
                        return notice;
116
 
                }
117
 
 
118
 
                public link_t new (string host, int port) {
119
 
                        link_t  l = &(link_struct) {
120
 
                                .host = host,
121
 
                                .port = port,
122
 
                                .closing = false,
123
 
                                .command_lock = Mutex::new(),
124
 
                                .notice_lock = Mutex::new(),
125
 
                                .reply_lock = Mutex::new(),
126
 
                                .replies_sem = Semaphore::new(),
127
 
                                .notices_sem = Semaphore::new(),
128
 
                        };
129
 
 
130
 
                        List::init(&l->replies);
131
 
                        List::init(&l->notices);
132
 
 
133
 
                        l->f = Net::connect (l->host, l->port);
134
 
                        l->reader = fork read_replies(l);
135
 
                        return l;
136
 
                }
137
 
 
138
 
                public void close(link_t l) {
139
 
                        l->closing = true;
140
 
                        Thread::send_signal(l->reader, 1);
141
 
                        Thread::join(l->reader);
142
 
                        File::close(l->f);
143
 
                }
144
 
        }
145
 
}