1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /* lifecycle_plugin.c: an example plugin to demonstrate the lifecycle hooks.
25  *                    of response body content
26  */
27 
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <inttypes.h>
31 #include <ts/ts.h>
32 
33 #define PLUGIN_NAME "lifecycle"
34 
35 int
CallbackHandler(TSCont this,TSEvent id,void * data)36 CallbackHandler(TSCont this, TSEvent id, void *data)
37 {
38   (void)this; // make compiler shut up about unused variable.
39   switch (id) {
40   case TS_EVENT_LIFECYCLE_PORTS_INITIALIZED:
41     TSDebug(PLUGIN_NAME, "Proxy ports initialized");
42     break;
43   case TS_EVENT_LIFECYCLE_PORTS_READY:
44     TSDebug(PLUGIN_NAME, "Proxy ports active");
45     break;
46   case TS_EVENT_LIFECYCLE_CACHE_READY:
47     TSDebug(PLUGIN_NAME, "Cache ready");
48     break;
49   case TS_EVENT_LIFECYCLE_MSG: {
50     TSPluginMsg *msg = (TSPluginMsg *)data;
51     TSDebug(PLUGIN_NAME, "Message to '%s' - %zu bytes of data", msg->tag, msg->data_size);
52     if (msg->data_size == 0) {
53       TSDebug(PLUGIN_NAME, "Message data is not available");
54     }
55 
56     break;
57   }
58   default:
59     TSDebug(PLUGIN_NAME, "Unexpected event %d", id);
60     break;
61   }
62   return TS_EVENT_NONE;
63 }
64 
65 void
TSPluginInit(int argc,const char * argv[])66 TSPluginInit(int argc, const char *argv[])
67 {
68   TSPluginRegistrationInfo info;
69   TSCont cb;
70 
71   info.plugin_name   = PLUGIN_NAME;
72   info.vendor_name   = "Apache Software Foundation";
73   info.support_email = "dev@trafficserver.apache.org";
74 
75   if (TSPluginRegister(&info) != TS_SUCCESS) {
76     TSError("[%s] Plugin registration failed", PLUGIN_NAME);
77 
78     goto Lerror;
79   }
80 
81   cb = TSContCreate(CallbackHandler, NULL);
82 
83   TSLifecycleHookAdd(TS_LIFECYCLE_PORTS_INITIALIZED_HOOK, cb);
84   TSLifecycleHookAdd(TS_LIFECYCLE_PORTS_READY_HOOK, cb);
85   TSLifecycleHookAdd(TS_LIFECYCLE_CACHE_READY_HOOK, cb);
86   TSLifecycleHookAdd(TS_LIFECYCLE_MSG_HOOK, cb);
87 
88   TSDebug(PLUGIN_NAME, "online");
89 
90   return;
91 
92 Lerror:
93   TSError("[%s] Unable to initialize plugin (disabled)", PLUGIN_NAME);
94 }
95