Merge some of Mahmut's patches
authorMark Spencer <markster@digium.com>
Thu, 20 Mar 2003 17:21:54 +0000 (17:21 +0000)
committerMark Spencer <markster@digium.com>
Thu, 20 Mar 2003 17:21:54 +0000 (17:21 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@666 65c4cc65-6c06-0410-ace0-fbb531ad65f3

apps/app_queue.c
channels/chan_agent.c
channels/chan_sip.c
manager.c

index f2bd012..7f74820 100755 (executable)
@@ -156,7 +156,8 @@ static int join_queue(char *queuename, struct queue_ent *qe)
                                q->count++;
                                res = 0;
                                manager_event(EVENT_FLAG_CALL, "Join", 
-                                                                        "Queue: %s\r\nPosition: %d\r\n", q->name, qe->pos );
+                                                               "Channel: %s\r\nQueue: %s\r\nPosition: %d\r\n",
+                                                               qe->chan->name, q->name, qe->pos );
 
                        }
                        ast_pthread_mutex_unlock(&q->lock);
@@ -212,7 +213,8 @@ static void leave_queue(struct queue_ent *qe)
        ast_pthread_mutex_lock(&q->lock);
        /* Take us out of the queue */
        manager_event(EVENT_FLAG_CALL, "Leave",
-                                                "Queue: %s\r\n", q->name );
+                                                "Channel: %s\r\nQueue: %s\r\n", 
+                                                qe->chan->name, q->name );
        prev = NULL;
        cur = q->head;
        while(cur) {
index 61f5bb2..0b3d74e 100755 (executable)
@@ -223,21 +223,36 @@ static int agent_call(struct ast_channel *ast, char *dest, int timeout)
        struct agent_pvt *p = ast->pvt->pvt;
        int res = -1;
        ast_pthread_mutex_lock(&p->lock);
+       ast_verbose( VERBOSE_PREFIX_3 "agent_call, call to agent '%s' call on '%s'\n", p->agent, p->chan->name);
+       ast_log( LOG_DEBUG, "Playing beep, lang '%s'\n", p->chan->language);
        res = ast_streamfile(p->chan, "beep", p->chan->language);
-       if (!res)
+       ast_log( LOG_DEBUG, "Played beep, result '%d'\n", res);
+       if (!res) {
                res = ast_waitstream(p->chan, "");
+               ast_log( LOG_DEBUG, "Waited for stream, result '%d'\n", res);
+       }
        if (!res) {
                res = ast_set_read_format(p->chan, ast_best_codec(p->chan->nativeformats));
+               ast_log( LOG_DEBUG, "Set read format, result '%d'\n", res);
                if (res)
                        ast_log(LOG_WARNING, "Unable to set read format to %d\n", ast_best_codec(p->chan->nativeformats));
        }
+       else {
+               // Agent hung-up
+               p->chan = NULL;
+       }
+
        if (!res) {
                ast_set_write_format(p->chan, ast_best_codec(p->chan->nativeformats));
+               ast_log( LOG_DEBUG, "Set write format, result '%d'\n", res);
                if (res)
                        ast_log(LOG_WARNING, "Unable to set write format to %d\n", ast_best_codec(p->chan->nativeformats));
        }
-       /* Call is immediately up */
-       ast_setstate(ast, AST_STATE_UP);
+       if( !res )
+       {
+               /* Call is immediately up */
+               ast_setstate(ast, AST_STATE_UP);
+       }
        CLEANUP(ast,p);
        ast_pthread_mutex_unlock(&p->lock);
        return res;
@@ -250,9 +265,6 @@ static int agent_hangup(struct ast_channel *ast)
        p->owner = NULL;
        ast->pvt->pvt = NULL;
        p->app_sleep_cond = 1;
-       ast_pthread_mutex_unlock(&p->lock);
-       /* Release ownership of the agent to other threads (presumably running the login app). */
-       ast_pthread_mutex_unlock(&p->app_lock);
        if (p->chan) {
                /* If they're dead, go ahead and hang up on the agent now */
                ast_pthread_mutex_lock(&p->chan->lock);
@@ -260,9 +272,20 @@ static int agent_hangup(struct ast_channel *ast)
                        ast_softhangup(p->chan, AST_SOFTHANGUP_EXPLICIT);
                ast_moh_start(p->chan, p->moh);
                ast_pthread_mutex_unlock(&p->chan->lock);
-       } else if (p->dead)
+               ast_pthread_mutex_unlock(&p->lock);
+               /* Release ownership of the agent to other threads (presumably running the login app). */
+               ast_pthread_mutex_unlock(&p->app_lock);
+       } else if (p->dead) {
                /* Go ahead and lose it */
+               ast_pthread_mutex_unlock(&p->lock);
+               /* Release ownership of the agent to other threads (presumably running the login app). */
+               ast_pthread_mutex_unlock(&p->app_lock);
                free(p);
+       } else {
+               ast_pthread_mutex_unlock(&p->lock);
+               /* Release ownership of the agent to other threads (presumably running the login app). */
+               ast_pthread_mutex_unlock(&p->app_lock);
+       }
 
        return 0;
 }
@@ -333,6 +356,15 @@ static struct ast_channel *agent_new(struct agent_pvt *p, int state)
                        ast_pthread_mutex_unlock(&p->lock);     /* For other thread to read the condition. */
                        ast_pthread_mutex_lock(&p->app_lock);
                        ast_pthread_mutex_lock(&p->lock);
+                       if( !p->chan )
+                       {
+                               ast_log(LOG_WARNING, "Agent disconnected while we were connecting the call\n");
+                               p->owner = NULL;
+                               tmp->pvt->pvt = NULL;
+                               p->app_sleep_cond = 1;
+                               ast_channel_free( tmp );
+                               return NULL;
+                       }
                }
                p->owning_app = pthread_self();
                /* After the above step, there should not be any blockers. */
@@ -486,9 +518,6 @@ static int login_exec(struct ast_channel *chan, void *data)
        char *opt_user = NULL;
        char *options = NULL;
        int play_announcement;
-       struct timespec required;
-       struct timespec remaining;
-       int delay;
        
        LOCAL_USER_ADD(u);
 
@@ -577,21 +606,12 @@ static int login_exec(struct ast_channel *chan, void *data)
                                                        ast_pthread_mutex_unlock(&p->lock);
                                                        ast_pthread_mutex_unlock(&agentlock);
                                                        while (res >= 0) {
-                                                               /* If we are not the owner, delay here for a while
-                                                                * so other interested threads can kick in. */
-                                                               delay = 0;
                                                                ast_pthread_mutex_lock(&p->lock);
                                                                if (p->chan != chan)
                                                                        res = -1;
-                                                               if (p->owner) 
-                                                                       delay = 1;
                                                                ast_pthread_mutex_unlock(&p->lock);
-                                                               if (delay) {
-                                                                       sched_yield();
-                                                                       required.tv_sec = 0;
-                                                                       required.tv_nsec = 20 * 1000 * 1000;
-                                                                       nanosleep( &required, &remaining );
-                                                               }
+                                                               /* Yield here so other interested threads can kick in. */
+                                                               sched_yield();
                                                                if (res)
                                                                        break;
 
@@ -603,12 +623,15 @@ static int login_exec(struct ast_channel *chan, void *data)
                                                                res = ast_safe_sleep_conditional( chan, 1000,
                                                                                                                agent_cont_sleep, p );
                                                                pthread_mutex_unlock( &p->app_lock );
+                                                               sched_yield();
                                                        }
+                                                       ast_pthread_mutex_lock(&p->lock);
                                                        if (res && p->owner) 
                                                                ast_log(LOG_WARNING, "Huh?  We broke out when there was still an owner?\n");
                                                        /* Log us off if appropriate */
                                                        if (p->chan == chan)
                                                                p->chan = NULL;
+                                                       ast_pthread_mutex_unlock(&p->lock);
                                                        if (option_verbose > 2)
                                                                ast_verbose(VERBOSE_PREFIX_3 "Agent '%s' logged out\n", p->agent);
                                                        manager_event(EVENT_FLAG_AGENT, "Agentlogoff",
index 824aa7c..d1fa814 100755 (executable)
@@ -77,6 +77,8 @@ static char context[AST_MAX_EXTENSION] = "default";
 
 static char language[MAX_LANGUAGE] = "";
 
+static char callerid[AST_MAX_EXTENSION] = "asterisk";
+
 static int usecnt =0;
 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
@@ -1951,17 +1953,17 @@ static void initreqprep(struct sip_request *req, struct sip_pvt *p, char *cmd, c
        char to[256];
        char tmp[80];
        char cid[256];
-       char *l = "asterisk", *n=NULL;
+       char *l = callerid, *n=NULL;
        if (p->owner && p->owner->callerid) {
                strcpy(cid, p->owner->callerid);
                ast_callerid_parse(cid, &n, &l);
                if (l) 
                        ast_shrink_phone_number(l);
                if (!l || !ast_isphonenumber(l))
-                               l = "asterisk";
+                               l = callerid;
        }
        if (!n)
-               n = l;
+               n = callerid;
        snprintf(from, sizeof(from), "\"%s\" <sip:%s@%s>;tag=%08x", n, l, inet_ntoa(p->ourip), p->tag);
        if (strlen(p->username)) {
                if (ntohs(p->sa.sin_port) != DEFAULT_SIP_PORT) {
@@ -4212,6 +4214,8 @@ static int reload_config(void)
                        }
                } else if (!strcasecmp(v->name, "language")) {
                        strncpy(language, v->value, sizeof(language)-1);
+               } else if (!strcasecmp(v->name, "callerid")) {
+                       strncpy(callerid, v->value, sizeof(callerid)-1);
                } else if (!strcasecmp(v->name, "nat")) {
                        globalnat = ast_true(v->value);
                } else if (!strcasecmp(v->name, "maxexpirey")) {
index be1bb28..1b954c8 100755 (executable)
--- a/manager.c
+++ b/manager.c
 #include <pthread.h>
 #include <string.h>
 #include <sys/time.h>
+#include <sys/types.h>
+#include <netdb.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <arpa/inet.h>
 #include <signal.h>
 #include <errno.h>
@@ -432,6 +435,7 @@ static int process_message(struct mansession *s, struct message *m)
        struct manager_action *tmp = first_action;
 
        strncpy(action, get_header(m, "Action"), sizeof(action));
+       ast_log( LOG_DEBUG, "Manager received command '%s'\n", action );
 
        if (!strlen(action)) {
                send_error(s, "Missing action in request");
@@ -569,10 +573,13 @@ static void *accept_thread(void *ignore)
        struct sockaddr_in sin;
        int sinlen;
        struct mansession *s;
+       struct protoent *p;
+       int arg = 1;
        pthread_attr_t attr;
 
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
        for (;;) {
                sinlen = sizeof(sin);
                as = accept(asock, &sin, &sinlen);
@@ -580,6 +587,12 @@ static void *accept_thread(void *ignore)
                        ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
                        continue;
                }
+               p = getprotobyname("tcp");
+               if( p ) {
+                       if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
+                               ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
+                       }
+               }
                s = malloc(sizeof(struct mansession));
                if (!s) {
                        ast_log(LOG_WARNING, "Failed to allocate management session: %s\n", strerror(errno));