comparison src/c/urweb.c @ 679:44f23712020d

Chat example working nicely, but without dead channel removal
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Mar 2009 18:26:50 -0400
parents 5ff1ff38e2db
children 6c9b8875f347
comparison
equal deleted inserted replaced
678:5ff1ff38e2db 679:44f23712020d
227 c->data.used.last_contact = time(NULL); 227 c->data.used.last_contact = time(NULL);
228 228
229 if (buf_used(&c->data.used.msgs) > 0) { 229 if (buf_used(&c->data.used.msgs) > 0) {
230 uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1); 230 uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1);
231 uw_really_send(sock, c->data.used.msgs.start, buf_used(&c->data.used.msgs)); 231 uw_really_send(sock, c->data.used.msgs.start, buf_used(&c->data.used.msgs));
232 buf_reset(&c->data.used.msgs);
232 close(sock); 233 close(sock);
233 } 234 }
234 else 235 else
235 c->data.used.sock = sock; 236 c->data.used.sock = sock;
236 237
380 ++ch->data.used.refcount; 381 ++ch->data.used.refcount;
381 pthread_mutex_unlock(&ch->data.used.lock); 382 pthread_mutex_unlock(&ch->data.used.lock);
382 } 383 }
383 384
384 static void uw_subscribe(channel *ch, client *c) { 385 static void uw_subscribe(channel *ch, client *c) {
385 client_list *cs = malloc(sizeof(client_list)); 386 client_list *cs;
386 387
387 pthread_mutex_lock(&ch->data.used.lock); 388 pthread_mutex_lock(&ch->data.used.lock);
388 389
390 for (cs = ch->data.used.clients; cs; cs = cs->next)
391 if (cs->data == c) {
392 pthread_mutex_unlock(&ch->data.used.lock);
393 return;
394 }
395
396 cs = malloc(sizeof(client_list));
389 cs->data = c; 397 cs->data = c;
390 cs->next = ch->data.used.clients; 398 cs->next = ch->data.used.clients;
391 ch->data.used.clients = cs; 399 ch->data.used.clients = cs;
392 400
393 pthread_mutex_unlock(&ch->data.used.lock); 401 pthread_mutex_unlock(&ch->data.used.lock);
836 return ""; 844 return "";
837 else { 845 else {
838 int pass; 846 int pass;
839 client *c = uw_new_client(&pass); 847 client *c = uw_new_client(&pass);
840 848
841 char *r = uw_malloc(ctx, strlen(ctx->script_header) + 65 + 3 * INTS_MAX + buf_used(&ctx->script) 849 char *r = uw_malloc(ctx, strlen(ctx->script_header) + 18 + buf_used(&ctx->script));
842 + strlen(ctx->url_prefix)); 850 sprintf(r, "%s<script>%s</script>",
843 sprintf(r, "%s<script>client_id=%d;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s</script>",
844 ctx->script_header, 851 ctx->script_header,
852 ctx->script.start);
853 return r;
854 }
855 }
856
857 const char *uw_Basis_get_settings(uw_context ctx, uw_Basis_string onload) {
858 if (ctx->script_header[0] == 0)
859 return "";
860 else {
861 int pass;
862 client *c = uw_new_client(&pass);
863
864 char *r = uw_malloc(ctx, 41 + 3 * INTS_MAX + strlen(ctx->url_prefix) + strlen(onload));
865 sprintf(r, " onload='client_id=%d;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s'",
845 (int)c->id, 866 (int)c->id,
846 c->data.used.pass, 867 c->data.used.pass,
847 ctx->url_prefix, 868 ctx->url_prefix,
848 ctx->timeout, 869 ctx->timeout,
849 ctx->script.start); 870 onload);
850 return r; 871 return r;
851 }
852 }
853
854 const char *uw_Basis_get_listener(uw_context ctx, uw_Basis_string onload) {
855 if (ctx->script_header[0] == 0)
856 return "";
857 else if (onload[0] == 0)
858 return " onload='listener()'";
859 else {
860 uw_Basis_string s = uw_malloc(ctx, strlen(onload) + 22);
861
862 sprintf(s, " onload='listener();%s'", onload);
863 return s;
864 } 872 }
865 } 873 }
866 874
867 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) { 875 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
868 char *r, *s2; 876 char *r, *s2;
1106 sprintf(r, "%lld%n", n, &len); 1114 sprintf(r, "%lld%n", n, &len);
1107 ctx->heap.front += len+1; 1115 ctx->heap.front += len+1;
1108 return r; 1116 return r;
1109 } 1117 }
1110 1118
1119 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel n) {
1120 int len;
1121 char *r;
1122
1123 uw_check_heap(ctx, INTS_MAX);
1124 r = ctx->heap.front;
1125 sprintf(r, "%lld%n", (long long)n, &len);
1126 ctx->heap.front += len+1;
1127 return r;
1128 }
1129
1111 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) { 1130 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
1112 int len; 1131 int len;
1113 char *r; 1132 char *r;
1114 1133
1115 uw_check_heap(ctx, FLOATS_MAX); 1134 uw_check_heap(ctx, FLOATS_MAX);
1157 } 1176 }
1158 1177
1159 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) { 1178 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
1160 uw_check(ctx, INTS_MAX); 1179 uw_check(ctx, INTS_MAX);
1161 uw_Basis_urlifyInt_w_unsafe(ctx, n); 1180 uw_Basis_urlifyInt_w_unsafe(ctx, n);
1181
1182 return uw_unit_v;
1183 }
1184
1185 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel n) {
1186 int len;
1187
1188 uw_check(ctx, INTS_MAX);
1189 sprintf(ctx->page.front, "%lld%n", (long long)n, &len);
1190 ctx->page.front += len;
1162 1191
1163 return uw_unit_v; 1192 return uw_unit_v;
1164 } 1193 }
1165 1194
1166 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) { 1195 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
1914 cd = &ctx->deltas[i]; 1943 cd = &ctx->deltas[i];
1915 else { 1944 else {
1916 ++ctx->n_deltas; 1945 ++ctx->n_deltas;
1917 ctx->deltas = realloc(ctx->deltas, sizeof(channel_delta) * ctx->n_deltas); 1946 ctx->deltas = realloc(ctx->deltas, sizeof(channel_delta) * ctx->n_deltas);
1918 cd = &ctx->deltas[ctx->n_deltas-1]; 1947 cd = &ctx->deltas[ctx->n_deltas-1];
1948 cd->n_subscribed = 0;
1949 cd->subscribed = malloc(0);
1950 buf_init(&cd->msgs, 0);
1919 } 1951 }
1920 1952
1921 cd->mode = USED; 1953 cd->mode = USED;
1922 cd->newness = OLD; 1954 cd->newness = OLD;
1923 cd->ch = ch; 1955 cd->ch = ch;
1956 uw_release_channel(ch); 1988 uw_release_channel(ch);
1957 uw_error(ctx, FATAL, "Unknown client ID in subscription request"); 1989 uw_error(ctx, FATAL, "Unknown client ID in subscription request");
1958 } else if (c->data.used.pass != pass) { 1990 } else if (c->data.used.pass != pass) {
1959 uw_release_channel(ch); 1991 uw_release_channel(ch);
1960 uw_release_client(c); 1992 uw_release_client(c);
1961 uw_error(ctx, FATAL, "Wrong client password in subscription request"); 1993 uw_error(ctx, FATAL, "Wrong client password (%d) in subscription request", pass);
1962 } else { 1994 } else {
1963 size_t i; 1995 size_t i;
1964 channel_delta *cd = allocate_delta(ctx, ch); 1996 channel_delta *cd = allocate_delta(ctx, ch);
1965 1997
1966 if (delta_used(cd)) 1998 if (delta_used(cd))