Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 1 | from __future__ import print_function |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import grpc |
| 6 | import flatbuffers |
| 7 | |
| 8 | from concurrent import futures |
| 9 | |
| 10 | sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'tests')) |
| 11 | import MyGame.Example.Monster as Monster |
| 12 | import MyGame.Example.Stat as Stat |
| 13 | import MyGame.Example.Vec3 as Vec3 |
| 14 | import MyGame.Example.Test as Test |
| 15 | import MyGame.Example.monster_test_grpc_fb as monster_grpc_fb |
| 16 | |
| 17 | |
| 18 | test_stat_id = "test_stat_id" |
| 19 | test_stat_val = 8 |
| 20 | test_stat_count = 1 |
| 21 | |
| 22 | test_monster_name1 = "test_monster_name1" |
| 23 | test_monster_name2 = "test_monster_name2" |
| 24 | test_string = "test_string" |
| 25 | test_color = 2 |
| 26 | test_X = 3.0 |
| 27 | test_Y = 2.0 |
| 28 | test_Z = 6.0 |
| 29 | test_test1 = 4.0 |
| 30 | test_a = 8 |
| 31 | test_b = 5 |
| 32 | test_hp = 67 |
| 33 | test_inventory = [1, 1, 2, 3, 5, 8] |
| 34 | test_testtype = 4 |
| 35 | |
| 36 | test_monsters_name_retrieve = ["big_monster", "small_monster"] |
| 37 | test_no_of_monsters = 2 |
| 38 | |
| 39 | |
| 40 | class MonsterStorage(monster_grpc_fb.MonsterStorageServicer): |
| 41 | |
| 42 | def Store(self, request, context): |
| 43 | |
| 44 | m = Monster.Monster().GetRootAsMonster(request, 0) |
| 45 | |
| 46 | assert m.Name().decode("utf-8") == test_monster_name1 |
| 47 | |
| 48 | assert m.Pos().X() == test_X |
| 49 | assert m.Pos().Y() == test_Y |
| 50 | assert m.Pos().Z() == test_Z |
| 51 | assert m.Pos().Test1() == test_test1 |
| 52 | assert m.Pos().Test2() == test_color |
| 53 | test3 = Test.Test() |
| 54 | assert m.Pos().Test3(test3).A() == test_a |
| 55 | assert m.Pos().Test3(test3).B() == test_b |
| 56 | |
| 57 | assert m.Hp() == test_hp |
| 58 | |
| 59 | assert m.Color() == test_color |
| 60 | |
| 61 | assert m.InventoryLength() == len(test_inventory) |
| 62 | for i in range(0, len(test_inventory)): |
| 63 | assert m.Inventory(i) == test_inventory[len(test_inventory)-i -1] |
| 64 | |
| 65 | assert m.TestType() == test_testtype |
| 66 | |
| 67 | assert m.Test() is not None |
| 68 | table = m.Test() |
| 69 | |
| 70 | m2 = Monster.Monster() |
| 71 | m2.Init(table.Bytes, table.Pos) |
| 72 | assert m2.Name().decode("utf-8") == test_monster_name2 |
| 73 | |
| 74 | m3 = m.Enemy() |
| 75 | assert m3.Name().decode("utf-8") == test_monster_name2 |
| 76 | |
| 77 | assert m.Testarrayofstring(0).decode("utf-8") == test_string |
| 78 | |
| 79 | b = flatbuffers.Builder(0) |
| 80 | i = b.CreateString(test_stat_id) |
| 81 | Stat.StatStart(b) |
| 82 | Stat.StatAddId(b, i) |
| 83 | Stat.StatAddVal(b, test_stat_val) |
| 84 | Stat.StatAddCount(b, test_stat_count) |
| 85 | b.Finish(Stat.StatEnd(b)) |
| 86 | return bytes(b.Output()) |
| 87 | |
| 88 | def Retrieve(self, request, context): |
| 89 | |
| 90 | s = Stat.Stat().GetRootAsStat(request, 0) |
| 91 | |
| 92 | no_of_monsters = test_no_of_monsters |
| 93 | for i in range(0, no_of_monsters): |
| 94 | b = flatbuffers.Builder(0) |
| 95 | i = b.CreateString(test_monsters_name_retrieve[i]) |
| 96 | Monster.MonsterStart(b) |
| 97 | Monster.MonsterAddName(b, i) |
| 98 | b.Finish(Monster.MonsterEnd(b)) |
| 99 | yield bytes(b.Output()) |
| 100 | |
| 101 | |
| 102 | def serve(): |
| 103 | |
| 104 | server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) |
| 105 | monster_grpc_fb.add_MonsterStorageServicer_to_server(MonsterStorage(), server) |
| 106 | server.add_insecure_port('[::]:50051') |
| 107 | |
| 108 | server.start() |
| 109 | |
| 110 | run() |
| 111 | |
| 112 | |
| 113 | def run(): |
| 114 | |
| 115 | channel = grpc.insecure_channel('127.0.0.1:50051') |
| 116 | stub = monster_grpc_fb.MonsterStorageStub(channel) |
| 117 | |
| 118 | b = flatbuffers.Builder(0) |
| 119 | name2 = b.CreateString(test_monster_name2) |
| 120 | name1 = b.CreateString(test_monster_name1) |
| 121 | Monster.MonsterStart(b) |
| 122 | Monster.MonsterAddName(b, name2) |
| 123 | monster2 = Monster.MonsterEnd(b) |
| 124 | test1 = b.CreateString(test_string) |
| 125 | |
| 126 | Monster.MonsterStartInventoryVector(b, len(test_inventory)) |
| 127 | for i in range(0, len(test_inventory)): |
| 128 | b.PrependByte(test_inventory[i]) |
| 129 | inv = b.EndVector(len(test_inventory)) |
| 130 | |
| 131 | Monster.MonsterStartTest4Vector(b, 2) |
| 132 | Test.CreateTest(b, 10, 20) |
| 133 | Test.CreateTest(b, 30, 40) |
| 134 | test4 = b.EndVector(2) |
| 135 | |
| 136 | Monster.MonsterStartTestarrayofstringVector(b, 1) |
| 137 | b.PrependUOffsetTRelative(test1) |
| 138 | test_array_of_string = b.EndVector(1) |
| 139 | |
| 140 | Monster.MonsterStart(b) |
| 141 | |
| 142 | Monster.MonsterAddHp(b, test_hp) |
| 143 | Monster.MonsterAddName(b, name1) |
| 144 | Monster.MonsterAddColor(b, test_color) |
| 145 | pos = Vec3.CreateVec3(b, test_X, test_Y, test_Z, test_test1, test_color, test_a, test_b) |
| 146 | Monster.MonsterAddPos(b, pos) |
| 147 | Monster.MonsterAddInventory(b, inv) |
| 148 | Monster.MonsterAddTestType(b, test_testtype) |
| 149 | Monster.MonsterAddTest(b, monster2) |
| 150 | Monster.MonsterAddTest4(b, test4) |
| 151 | Monster.MonsterAddEnemy(b, monster2) |
| 152 | Monster.MonsterAddTestarrayofstring(b, test_array_of_string) |
| 153 | monster = Monster.MonsterEnd(b) |
| 154 | |
| 155 | b.Finish(monster) |
| 156 | |
| 157 | stat_response = stub.Store(bytes(b.Output())) |
| 158 | |
| 159 | s = Stat.Stat().GetRootAsStat(stat_response, 0) |
| 160 | |
| 161 | assert s.Id().decode("utf-8") == test_stat_id |
| 162 | assert s.Val() == test_stat_val |
| 163 | assert s.Count() == test_stat_count |
| 164 | |
| 165 | monster_reponses = stub.Retrieve(stat_response) |
| 166 | count = 0 |
| 167 | for monster_reponse in monster_reponses: |
| 168 | m = Monster.Monster().GetRootAsMonster(monster_reponse, 0) |
| 169 | assert m.Name().decode("utf-8") == test_monsters_name_retrieve[count] |
| 170 | count = count + 1 |
| 171 | |
| 172 | |
| 173 | if __name__ == '__main__': |
| 174 | serve() |