03ec2788749417bacbb45dabbc937fe824bbb55d
[apps/native/gear-racing-car.git] / src / messages / message_factory.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18
19 #include "messages/message_factory.h"
20
21 #include "messages/message_ack.h"
22 #include "messages/message_bye.h"
23 #include "messages/message_command.h"
24 #include "messages/message_connect.h"
25 #include "messages/message_connect_accepted.h"
26 #include "messages/message_connect_refused.h"
27 #include "messages/message_keep_alive.h"
28
29 struct _message_factory {
30         union {
31                 message_ack_t ack;
32                 message_bye_t bye;
33                 message_command_t command;
34                 message_connect_t connect;
35                 message_connect_accepted_t connect_accepted;
36                 message_connect_refused_t connect_refused;
37                 message_keep_alive_t keep_alive;
38         } messages;
39 };
40
41 message_t *message_factory_create_message(message_factory_t *factory, message_type_e type)
42 {
43         switch(type) {
44         case MESSAGE_CONNECT:
45                 message_connect_init(&factory->messages.connect);
46                 return &factory->messages.connect.base;
47         case MESSAGE_CONNECT_ACCEPTED:
48                 message_connect_accepted_init(&factory->messages.connect_accepted);
49                 return &factory->messages.connect_accepted.base;
50         case MESSAGE_CONNECT_REFUSED:
51                 message_connect_refused_init(&factory->messages.connect_refused);
52                 return &factory->messages.connect_refused.base;
53         case MESSAGE_KEEP_ALIVE:
54                 message_keep_alive_init(&factory->messages.keep_alive);
55                 return &factory->messages.keep_alive.base;
56         case MESSAGE_ACK:
57                 message_ack_init(&factory->messages.ack);
58                 return &factory->messages.ack.base;
59         case MESSAGE_COMMAND:
60                 message_command_init(&factory->messages.command);
61                 return &factory->messages.command.base;
62         case MESSAGE_BYE:
63                 message_bye_init(&factory->messages.bye);
64                 return &factory->messages.bye.base;
65         default:
66                 return NULL;
67         }
68 }
69
70 message_factory_t *message_factory_create()
71 {
72         return malloc(sizeof(message_factory_t));
73 }
74
75 void message_factory_destroy(message_factory_t *factory)
76 {
77         free(factory);
78 }