Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

fix_session_manager.c 3.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  1. /* Copyright (c) 2012, Chris Winter <wintercni@gmail.com>
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. Neither the name of the copyright holder nor the
  13. * names of contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <pthread.h>
  28. #include <stdio.h>
  29. #include <libcore/map.h>
  30. #include <libcore/string.h>
  31. #include "fix_session.h"
  32. #include "fix_session_manager.h"
  33. #include "fix_parser.h"
  34. #define DEBUG 0
  35. #define DBG(...) \
  36. do { if(DEBUG) fprintf(stderr, __VA_ARGS__); } while(0)
  37. /* Private scope */
  38. static Map *sessions = NULL;
  39. static int is_initialized = 0;
  40. static pthread_mutex_t mgr_mutex = PTHREAD_MUTEX_INITIALIZER;
  41. void fix_session_manager_init(void)
  42. {
  43. printf("FIX Session Manager init\n");
  44. pthread_mutex_lock(&mgr_mutex);
  45. if(!is_initialized) {
  46. sessions = map_create((CompareFn)string_compare);
  47. is_initialized = 1;
  48. }
  49. pthread_mutex_unlock(&mgr_mutex);
  50. }
  51. void fix_session_manager_destroy(void)
  52. {
  53. printf("FIX Session Manager destroy\n");
  54. pthread_mutex_lock(&mgr_mutex);
  55. if(is_initialized) {
  56. is_initialized = 0;
  57. map_free_all(sessions, (FreeFn)fix_session_free);
  58. }
  59. pthread_mutex_unlock(&mgr_mutex);
  60. }
  61. int fix_session_manager_lookup_session(String *fix_msg, FixSession **session)
  62. {
  63. String *senderCompId;
  64. MapIterator *it;
  65. int ret;
  66. DBG("Session manager lookup session\n");
  67. *session = NULL;
  68. ret = 0;
  69. pthread_mutex_lock(&mgr_mutex);
  70. if(is_initialized && fix_parse_is_msg_valid(fix_msg)) {
  71. senderCompId = fix_parse_SenderCompId(fix_msg);
  72. DBG("Looking up session for: '%s'\n", string_get_chars(senderCompId));
  73. if((NULL == senderCompId) || string_is_empty(senderCompId)) {
  74. ret = -1;
  75. } else {
  76. it = map_find(sessions, senderCompId);
  77. if(NULL != it) {
  78. DBG("Found existing session object\n");
  79. *session = (FixSession *)map_get_value(it);
  80. } else {
  81. DBG("Creating new session object\n");
  82. *session = fix_session_create(senderCompId,
  83. fix_parse_MsgSeqNum(fix_msg));
  84. if(NULL == *session) {
  85. ret = -1;
  86. } else {
  87. /* Store new session object in the table */
  88. map_insert(sessions, fix_session_get_SenderCompId(*session),
  89. *session);
  90. }
  91. }
  92. }
  93. } else {
  94. ret = -1;
  95. }
  96. pthread_mutex_unlock(&mgr_mutex);
  97. return ret;
  98. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...