├── .gitignore ├── LICENSE ├── README.md ├── build.xml ├── config └── test.json ├── nbproject ├── build-impl.xml ├── genfiles.properties ├── project.properties └── project.xml ├── src └── com │ └── jilk │ └── ros │ ├── Example.java │ ├── MessageHandler.java │ ├── ROSClient.java │ ├── Service.java │ ├── Topic.java │ ├── message │ ├── Clock.java │ ├── Duration.java │ ├── DurationPrimitive.java │ ├── Empty.java │ ├── Header.java │ ├── Log.java │ ├── Message.java │ ├── MessageException.java │ ├── MessageType.java │ ├── Time.java │ └── TimePrimitive.java │ ├── rosapi │ └── message │ │ ├── Empty.java │ │ ├── GetTime.java │ │ ├── MessageDetails.java │ │ ├── Nodes.java │ │ ├── Service.java │ │ ├── Services.java │ │ ├── Topic.java │ │ ├── Topics.java │ │ ├── Type.java │ │ └── TypeDef.java │ └── rosbridge │ ├── FullMessageHandler.java │ ├── ROSBridgeClient.java │ ├── implementation │ ├── Base64.java │ ├── JSON.java │ ├── ROSBridgeWebSocketClient.java │ └── Registry.java │ ├── indication │ ├── AsArray.java │ ├── Base64Encoded.java │ ├── Indicate.java │ ├── Indicated.java │ ├── Indication.java │ └── Indicator.java │ └── operation │ ├── Advertise.java │ ├── Authenticate.java │ ├── CallService.java │ ├── Fragment.java │ ├── Operation.java │ ├── PNG.java │ ├── Publish.java │ ├── ServiceResponse.java │ ├── SetStatusLevel.java │ ├── Status.java │ ├── Subscribe.java │ ├── Unadvertise.java │ ├── Unsubscribe.java │ └── Wrapper.java └── test └── com └── jilk └── ros └── rosbridge └── implementation ├── JSONTest.java └── TestMessage.java /.gitignore: -------------------------------------------------------------------------------- 1 | /dist/ 2 | /build/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ROSBridgeClient 2 | =============== 3 | 4 | A Java client for ROSBridge 5 | 6 | This project is functional, and has been tested in use in several contexts. However, it is undocumented and has no unit or other test code, so it is likely not to be robust. 7 | 8 | Look at com/jilk/ros/Example.java to see how to use it. You'll need to be familiar with ROS. 9 | -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Builds, tests, and runs the project ROSBridgeClient. 12 | 13 | 74 | 75 | -------------------------------------------------------------------------------- /config/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "filetype": "ros", 3 | "version": 1, 4 | "uri": "ws://162.243.238.80:9090", 5 | "subscribe": [ 6 | ["/clock", "Clock"], 7 | ["/othertopic", "OtherTopic"] 8 | ], 9 | "advertise": [ 10 | ["/publishtopic", "msgs/PublishMsg"] 11 | ], 12 | } 13 | -------------------------------------------------------------------------------- /nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=fdb476b6 2 | build.xml.script.CRC32=e4a47e63 3 | build.xml.stylesheet.CRC32=28e38971@1.56.1.46 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=fdb476b6 7 | nbproject/build-impl.xml.script.CRC32=ebd346eb 8 | nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 9 | -------------------------------------------------------------------------------- /nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processor.options= 4 | annotation.processing.processors.list= 5 | annotation.processing.run.all.processors=true 6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 7 | build.classes.dir=${build.dir}/classes 8 | build.classes.excludes=**/*.java,**/*.form 9 | # This directory is removed when the project is cleaned: 10 | build.dir=build 11 | build.generated.dir=${build.dir}/generated 12 | build.generated.sources.dir=${build.dir}/generated-sources 13 | # Only compile against the classpath explicitly listed here: 14 | build.sysclasspath=ignore 15 | build.test.classes.dir=${build.dir}/test/classes 16 | build.test.results.dir=${build.dir}/test/results 17 | # Uncomment to specify the preferred debugger connection transport: 18 | #debug.transport=dt_socket 19 | debug.classpath=\ 20 | ${run.classpath} 21 | debug.test.classpath=\ 22 | ${run.test.classpath} 23 | # This directory is removed when the project is cleaned: 24 | dist.dir=dist 25 | dist.jar=${dist.dir}/ROSBridgeClient.jar 26 | dist.javadoc.dir=${dist.dir}/javadoc 27 | excludes= 28 | file.reference.java_websocket.jar=jar\\java_websocket.jar 29 | file.reference.json-simple-1.1.1.jar=jar\\json-simple-1.1.1.jar 30 | includes=** 31 | jar.compress=false 32 | javac.classpath=\ 33 | ${file.reference.java_websocket.jar}:\ 34 | ${file.reference.json-simple-1.1.1.jar} 35 | # Space-separated list of extra javac options 36 | javac.compilerargs= 37 | javac.deprecation=false 38 | javac.processorpath=\ 39 | ${javac.classpath} 40 | javac.source=1.7 41 | javac.target=1.7 42 | javac.test.classpath=\ 43 | ${javac.classpath}:\ 44 | ${build.classes.dir}:\ 45 | ${libs.junit_4.classpath} 46 | javac.test.processorpath=\ 47 | ${javac.test.classpath} 48 | javadoc.additionalparam= 49 | javadoc.author=false 50 | javadoc.encoding=${source.encoding} 51 | javadoc.noindex=false 52 | javadoc.nonavbar=false 53 | javadoc.notree=false 54 | javadoc.private=false 55 | javadoc.splitindex=true 56 | javadoc.use=true 57 | javadoc.version=false 58 | javadoc.windowtitle= 59 | meta.inf.dir=${src.dir}/META-INF 60 | mkdist.disabled=true 61 | platform.active=default_platform 62 | run.classpath=\ 63 | ${javac.classpath}:\ 64 | ${build.classes.dir} 65 | # Space-separated list of JVM arguments used when running the project. 66 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 67 | # To set system properties for unit tests define test-sys-prop.name=value: 68 | run.jvmargs= 69 | run.test.classpath=\ 70 | ${javac.test.classpath}:\ 71 | ${build.test.classes.dir} 72 | source.encoding=UTF-8 73 | src.dir=src 74 | test.src.dir=test 75 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | ROSBridgeClient 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/com/jilk/ros/Example.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros; 21 | 22 | import com.jilk.ros.message.Clock; 23 | import com.jilk.ros.rosapi.message.Empty; 24 | import com.jilk.ros.rosapi.message.Topics; 25 | import com.jilk.ros.rosapi.message.Topic; 26 | import com.jilk.ros.rosapi.message.Type; 27 | import com.jilk.ros.rosapi.message.MessageDetails; 28 | import com.jilk.ros.rosapi.message.TypeDef; 29 | import com.jilk.ros.rosapi.message.GetTime; 30 | import com.jilk.ros.rosbridge.ROSBridgeClient; 31 | import com.jilk.ros.message.Log; 32 | 33 | public class Example { 34 | 35 | public Example() {} 36 | 37 | public static void main(String[] args) { 38 | ROSBridgeClient client = new ROSBridgeClient("ws://162.243.238.80:9090"); 39 | client.connect(); 40 | //testTopic(client); 41 | try { 42 | testService(client); 43 | } 44 | catch (RuntimeException ex) { 45 | ex.printStackTrace(); 46 | } 47 | finally { 48 | client.disconnect(); 49 | } 50 | } 51 | 52 | public static void testService(ROSBridgeClient client) { 53 | try { 54 | Service timeService = 55 | new Service("/rosapi/get_time", Empty.class, GetTime.class, client); 56 | timeService.verify(); 57 | //System.out.println("Time (secs): " + timeService.callBlocking(new Empty()).time.sec); 58 | 59 | Service serviceTypeService = 60 | new Service("/rosapi/service_type", 61 | com.jilk.ros.rosapi.message.Service.class, Type.class, client); 62 | serviceTypeService.verify(); 63 | String type = serviceTypeService.callBlocking(new com.jilk.ros.rosapi.message.Service("/rosapi/service_response_details")).type; 64 | 65 | Service serviceDetails = 66 | new Service("/rosapi/service_response_details", 67 | Type.class, MessageDetails.class, client); 68 | serviceDetails.verify(); 69 | //serviceDetails.callBlocking(new Type(type)).print(); 70 | 71 | com.jilk.ros.Topic logTopic = 72 | new com.jilk.ros.Topic("/rosout", Log.class, client); 73 | logTopic.verify(); 74 | 75 | /* 76 | System.out.println("Nodes"); 77 | for (String s : client.getNodes()) 78 | System.out.println(" " + s); 79 | System.out.println("Topics"); 80 | for (String s : client.getTopics()) { 81 | System.out.println(s + ":"); 82 | client.getTopicMessageDetails(s).print(); 83 | } 84 | System.out.println("Services"); 85 | for (String s : client.getServices()) { 86 | System.out.println(s + ":"); 87 | client.getServiceRequestDetails(s).print(); 88 | System.out.println("-----------------"); 89 | client.getServiceResponseDetails(s).print(); 90 | } 91 | */ 92 | } 93 | catch (InterruptedException ex) { 94 | System.out.println("Process was interrupted."); 95 | } 96 | /* 97 | Service topicService = 98 | new Service("/rosapi/topics", Empty.class, Topics.class, client); 99 | Service typeService = 100 | new Service("/rosapi/topic_type", Topic.class, Type.class, client); 101 | Service messageService = 102 | new Service("/rosapi/message_details", Type.class, MessageDetails.class, client); 103 | try { 104 | Topics topics = topicService.callBlocking(new Empty()); 105 | for (String topicString : topics.topics) { 106 | Topic topic = new Topic(); 107 | topic.topic = topicString; 108 | Type type = typeService.callBlocking(topic); 109 | MessageDetails details = messageService.callBlocking(type); 110 | System.out.println("Topic: " + topic.topic + " Type: " + type.type); 111 | details.print(); 112 | System.out.println(); 113 | } 114 | Type type = new Type(); 115 | type.type = "time"; 116 | System.out.print("Single type check on \'time\': "); 117 | messageService.callBlocking(type).print(); 118 | } 119 | catch (InterruptedException ex) { 120 | System.out.println("testService: process was interrupted."); 121 | } 122 | */ 123 | } 124 | 125 | public static void testTopic(ROSBridgeClient client) { 126 | com.jilk.ros.Topic clockTopic = new com.jilk.ros.Topic("/clock", Clock.class, client); 127 | clockTopic.subscribe(); 128 | try {Thread.sleep(20000);} catch(InterruptedException ex) {} 129 | Clock cl = null; 130 | try { 131 | cl = clockTopic.take(); // just gets one 132 | } 133 | catch (InterruptedException ex) {} 134 | cl.print(); 135 | cl.clock.nsecs++; 136 | clockTopic.unsubscribe(); 137 | clockTopic.advertise(); 138 | clockTopic.publish(cl); 139 | clockTopic.unadvertise(); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/com/jilk/ros/MessageHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros; 21 | 22 | import com.jilk.ros.message.Message; 23 | 24 | public interface MessageHandler { 25 | public void onMessage(T message); 26 | } 27 | -------------------------------------------------------------------------------- /src/com/jilk/ros/ROSClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.rosapi.message.TypeDef; 24 | import com.jilk.ros.rosbridge.operation.Operation; 25 | import com.jilk.ros.rosbridge.ROSBridgeClient; 26 | import com.jilk.ros.rosbridge.FullMessageHandler; 27 | 28 | public abstract class ROSClient { 29 | 30 | public ROSClient() {} 31 | 32 | public static ROSClient create(String uriString) { 33 | // if we ever implement other ROSClient types, we'll key off the URI protocol (e.g., ws://) 34 | // we'd also have to abstract out Topic and Service since they depend on the ROSBridge operations 35 | return new ROSBridgeClient(uriString); 36 | } 37 | 38 | public abstract boolean connect(); 39 | public abstract boolean connect(ROSClient.ConnectionStatusListener listener); 40 | public abstract void disconnect(); 41 | public abstract void send(Operation operation); 42 | public abstract void register(Class c, 43 | String s, 44 | Class m, 45 | FullMessageHandler h); 46 | public abstract void unregister(Class c, String s); 47 | public abstract void setDebug(boolean debug); 48 | public abstract String[] getNodes() throws InterruptedException; 49 | public abstract String[] getTopics() throws InterruptedException; 50 | public abstract String[] getServices() throws InterruptedException; 51 | public abstract TypeDef getTopicMessageDetails(String topic) throws InterruptedException; 52 | public abstract TypeDef getServiceRequestDetails(String service) throws InterruptedException; 53 | public abstract TypeDef getServiceResponseDetails(String service) throws InterruptedException; 54 | public abstract TypeDef getTypeDetails(String type) throws InterruptedException; 55 | public abstract void typeMatch(TypeDef t, Class c) throws InterruptedException; 56 | public abstract Object getUnderlyingClient(); // for debugging 57 | 58 | public interface ConnectionStatusListener { 59 | public void onConnect(); 60 | public void onDisconnect(boolean normal, String reason, int code); 61 | public void onError(Exception ex); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/com/jilk/ros/Service.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.rosbridge.operation.CallService; 24 | import com.jilk.ros.rosbridge.operation.ServiceResponse; 25 | import com.jilk.ros.rosbridge.FullMessageHandler; 26 | 27 | import java.util.Map; 28 | import java.util.HashMap; 29 | import java.util.concurrent.CountDownLatch; 30 | 31 | public class Service implements FullMessageHandler { 32 | private String service; 33 | private Class responseType; 34 | private Class callType; 35 | private ROSClient client; 36 | private Map calls; 37 | 38 | public Service(String service, Class callType, 39 | Class responseType, ROSClient client) { 40 | this.service = service; 41 | this.client = client; 42 | this.responseType = responseType; 43 | this.callType = callType; 44 | calls = new HashMap(); 45 | } 46 | 47 | // A result can only be returned once; it is cleared from the hash before 48 | // being sent/returned. This is to ensure that results do not accumulate 49 | // indefinitely. If callers need to keep these around they can set up their 50 | // own hash. 51 | 52 | @Override 53 | public void onMessage(String id, Message response) { 54 | //System.out.print("Service.onMessage: "); 55 | //response.print(); 56 | CallRecord call = calls.get(id); 57 | if (call.handler != null) { 58 | calls.remove(id); 59 | call.handler.onMessage((ResponseType) response); 60 | } 61 | else { 62 | call.result = (ResponseType) response; 63 | call.latch.countDown(); 64 | } 65 | } 66 | 67 | public String call(CallType args) { 68 | return callImpl(args, null); 69 | } 70 | 71 | public void callWithHandler(CallType args, MessageHandler responseHandler) { 72 | callImpl(args, responseHandler); 73 | } 74 | 75 | public ResponseType callBlocking(CallType args) throws InterruptedException { 76 | return take(call(args)); 77 | } 78 | 79 | private String callImpl(CallType args, MessageHandler responseHandler) { 80 | client.register(ServiceResponse.class, service, responseType, this); // do this once on creation? 81 | CallService messageCallService = new CallService(service, args); 82 | String id = messageCallService.id; 83 | CallRecord callRecord = new CallRecord(responseHandler); 84 | calls.put(id, callRecord); 85 | client.send(messageCallService); 86 | return id; 87 | } 88 | 89 | public ResponseType poll(String id) { 90 | CallRecord call = calls.get(id); 91 | if (call.result != null) 92 | calls.remove(id); 93 | return call.result; 94 | } 95 | 96 | public ResponseType take(String id) throws InterruptedException { 97 | CallRecord call = calls.get(id); 98 | call.latch.await(); 99 | calls.remove(id); 100 | return call.result; 101 | } 102 | 103 | public void verify() throws InterruptedException { 104 | 105 | boolean hasService = false; 106 | for (String s : client.getServices()) { 107 | if (s.equals(service)) { 108 | hasService = true; 109 | break; 110 | } 111 | } 112 | if (!hasService) 113 | throw new RuntimeException("Service \'" + service + "\' not available."); 114 | 115 | client.typeMatch(client.getServiceRequestDetails(service), callType); 116 | client.typeMatch(client.getServiceResponseDetails(service), responseType); 117 | } 118 | 119 | private class CallRecord { 120 | public ResponseType result; 121 | public CountDownLatch latch; 122 | public MessageHandler handler; 123 | 124 | public CallRecord(MessageHandler handler) { 125 | this.result = null; 126 | this.latch = new CountDownLatch(1); 127 | this.handler = handler; 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/com/jilk/ros/Topic.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros; 21 | 22 | import java.util.concurrent.LinkedBlockingQueue; 23 | 24 | import com.jilk.ros.message.Message; 25 | import com.jilk.ros.rosbridge.operation.*; 26 | import com.jilk.ros.rosbridge.FullMessageHandler; 27 | 28 | 29 | public class Topic extends LinkedBlockingQueue implements FullMessageHandler { 30 | protected String topic; 31 | private Class type; 32 | private String messageType; 33 | private ROSClient client; 34 | private Thread handlerThread; 35 | 36 | public Topic(String topic, Class type, ROSClient client) { 37 | this.topic = topic; 38 | this.client = client; 39 | this.type = type; 40 | messageType = Message.getMessageType(type); 41 | handlerThread = null; 42 | } 43 | 44 | @Override 45 | public void onMessage(String id, Message message) { 46 | add((T) message); 47 | } 48 | 49 | 50 | // warning: there is a delay between the completion of this method and 51 | // the completion of the subscription; it takes longer than 52 | // publishing multiple other messages, for example. 53 | public void subscribe(MessageHandler handler) { 54 | startRunner(handler); 55 | subscribe(); 56 | } 57 | 58 | public void subscribe() { 59 | client.register(Publish.class, topic, type, this); 60 | send(new Subscribe(topic, messageType)); 61 | } 62 | 63 | public void unsubscribe() { 64 | // need to handle race conditions in incoming message handler 65 | // so that once unsubscribe has happened the handler gets no more 66 | // messages 67 | send(new Unsubscribe(topic)); 68 | client.unregister(Publish.class, topic); 69 | stopRunner(); 70 | } 71 | 72 | private void startRunner(MessageHandler handler) { 73 | stopRunner(); 74 | handlerThread = new Thread(new MessageRunner(handler)); 75 | handlerThread.setName("Message handler for " + topic); 76 | handlerThread.start(); 77 | } 78 | 79 | private void stopRunner() { 80 | if (handlerThread != null) { 81 | handlerThread.interrupt(); 82 | clear(); 83 | handlerThread = null; 84 | } 85 | } 86 | 87 | 88 | public void advertise() { 89 | send(new Advertise(topic, messageType)); 90 | } 91 | 92 | public void publish(T message) { 93 | send(new Publish(topic, message)); 94 | } 95 | 96 | public void unadvertise() { 97 | send(new Unadvertise(topic)); 98 | } 99 | 100 | private void send(Operation operation) { 101 | client.send(operation); 102 | } 103 | 104 | public void verify() throws InterruptedException { 105 | 106 | boolean hasTopic = false; 107 | for (String s : client.getTopics()) { 108 | if (s.equals(topic)) { 109 | hasTopic = true; 110 | break; 111 | } 112 | } 113 | if (!hasTopic) 114 | throw new RuntimeException("Topic \'" + topic + "\' not available."); 115 | 116 | client.typeMatch(client.getTopicMessageDetails(topic), type); 117 | } 118 | 119 | private class MessageRunner implements Runnable { 120 | private MessageHandler handler; 121 | 122 | public MessageRunner(MessageHandler handler) { 123 | this.handler = handler; 124 | } 125 | 126 | @Override 127 | public void run() { 128 | while (!Thread.interrupted()) { 129 | try { 130 | handler.onMessage(take()); 131 | } 132 | catch (InterruptedException ex) { 133 | break; 134 | } 135 | } 136 | } 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/Clock.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "rosgraph_msgs/Clock") 23 | public class Clock extends Message { 24 | public TimePrimitive clock; 25 | } 26 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/Duration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "std_msgs/Duration") 23 | public class Duration extends DurationPrimitive { 24 | } 25 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/DurationPrimitive.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "duration") 23 | public class DurationPrimitive extends TimePrimitive { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/Empty.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "std_srvs/Empty") 23 | public class Empty extends Message { 24 | } 25 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/Header.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "std_msgs/Header") 23 | public class Header extends Message { 24 | public long seq; 25 | public TimePrimitive stamp; 26 | public String frame_id; 27 | } 28 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/Log.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "rosgraph_msgs/Log") 23 | public class Log extends Message { 24 | public Header header; 25 | public byte level; 26 | public String name; 27 | public String msg; 28 | public String file; 29 | public String function; 30 | public long line; 31 | public String[] topics; 32 | } 33 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/Message.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | import java.lang.reflect.Array; 23 | import java.lang.reflect.Field; 24 | import java.util.Map; 25 | 26 | 27 | @MessageType(string = "message") 28 | public abstract class Message { 29 | 30 | // Some requirements about message types: 31 | // - It must have a MessageType declaration to be recognized on inbound messages 32 | // - Every field must be explicitly designated as public 33 | // - Every field that is not a primitive or near-primitive must be another Message class 34 | // - If there is a non-empty constructor, you must also have an empty constructor 35 | // - If it is set up as an inner class, it needs an explicit nullary constructor 36 | // (note: I have seen an inner class otherwise fail, I have not tested it with the explicit constructor) 37 | 38 | public static void register(Class c, Map messageClasses) { 39 | try { 40 | typecheck(c); 41 | 42 | // Must register the class and not have duplicate 43 | // This is not recursive because only the top level message class 44 | // needs to be determined from the string - others are top-down. 45 | String messageString = getMessageType(c); 46 | Class existingClass = messageClasses.get(messageString); 47 | if (existingClass != null && !existingClass.equals(c)) 48 | throw new MessageException("Message String \'" + messageString + 49 | "\' is assigned to two different classes (" + 50 | c.getName() + " and " + existingClass.getName() + ")"); 51 | messageClasses.put(messageString, c); 52 | } 53 | catch (MessageException ex) { 54 | // should be changed to be a hooked method to give library user control 55 | System.out.println(ex.getMessage()); 56 | } 57 | } 58 | 59 | public static String getMessageType(Class c) { 60 | return ((MessageType) c.getAnnotation(MessageType.class)).string(); 61 | } 62 | 63 | // this has never been used or tested but kind of belongs here 64 | // commented out because it uses ReflectiveOperationException which is not available on Android 65 | /* 66 | public static Message newInstance(String className) { 67 | try { 68 | Class messageClass = Class.forName(className); 69 | if (Message.class.isAssignableFrom(messageClass)) 70 | return (Message) messageClass.newInstance(); 71 | else throw new ClassCastException(); 72 | } 73 | catch (ReflectiveOperationException ex) { 74 | throw new RuntimeException("Unable to create message of class \'" + className + "\'.", ex); 75 | } 76 | } 77 | */ 78 | 79 | // Could probably do more checking here, but not sure what right now 80 | private static void typecheck(Class c) throws MessageException { 81 | 82 | // Must inherit from Message 83 | if (!Message.class.isAssignableFrom(c)) 84 | throw new MessageException("Class \'" + c.getName() + 85 | "\' does not extend Message"); 86 | 87 | // Must have the MessageType annotation 88 | if (getMessageType(c) == null) 89 | throw new MessageException("Class \'" + c.getName() + 90 | "\' is missing the MessageType annotation"); 91 | 92 | // All fields must also be valid Message classes 93 | // Note that this also serves to force-load all the message classes 94 | // so that they get registered 95 | for (Field f : c.getFields()) { 96 | Class fc = f.getType(); 97 | if (fc.isArray()) { 98 | Class ac = fc.getComponentType(); 99 | if (!isPrimitive(ac)) 100 | typecheck(ac); 101 | } 102 | else if (!isPrimitive(fc)) 103 | typecheck(fc); 104 | } 105 | } 106 | 107 | public void print() { 108 | printMessage(this, ""); 109 | } 110 | 111 | private static void printMessage(Object o, String indent) { 112 | for (Field f : o.getClass().getFields()) { 113 | Class c = f.getType(); 114 | Object fieldObject = getFieldObject(f, o); 115 | if (fieldObject != null) { 116 | if (isPrimitive(c)) 117 | System.out.println(indent + f.getName() + ": " + fieldObject); 118 | else if (c.isArray()) { 119 | System.out.println(indent + f.getName() + ": ["); 120 | printArray(fieldObject, indent + " "); 121 | System.out.println(indent + "]"); 122 | } 123 | else { 124 | System.out.println(indent + f.getName() + ":"); 125 | printMessage(fieldObject, indent + " "); 126 | } 127 | } 128 | } 129 | } 130 | 131 | private static void printArray(Object array, String indent) { 132 | Class arrayClass = array.getClass().getComponentType(); 133 | for (int i = 0; i < Array.getLength(array); i++) { 134 | Object elementObject = Array.get(array, i); 135 | if (elementObject != null) { 136 | if (isPrimitive(arrayClass)) 137 | System.out.println(indent + i + ": " + elementObject); 138 | else if (arrayClass.isArray()) { // this is not actually allowed in ROS 139 | System.out.println(indent + i + ": ["); 140 | printArray(elementObject, indent + " "); 141 | System.out.println(indent + "]"); 142 | } 143 | else { 144 | System.out.println(indent + i + ":"); 145 | printMessage(elementObject, indent + " "); 146 | } 147 | } 148 | } 149 | // remember to print array indices 150 | } 151 | 152 | public static boolean isPrimitive(Class c) { 153 | return (c.isPrimitive() || 154 | c.equals(String.class) || 155 | Number.class.isAssignableFrom(c) || 156 | c.equals(Boolean.class)); 157 | } 158 | 159 | 160 | // Copied from com.jilk.ros.rosbridge.JSON 161 | private static Object getFieldObject(Field f, Object o) { 162 | Object fo = null; 163 | try { 164 | fo = f.get(o); 165 | } 166 | catch (IllegalAccessException ex) { 167 | ex.printStackTrace(); 168 | } 169 | return fo; 170 | } 171 | 172 | public void copyFrom(Message source) { 173 | try { 174 | if (source.getClass() != getClass()) 175 | throw new RuntimeException("Attempt to copy non-matching classes"); 176 | for (Field f : getClass().getFields()) { 177 | Class fc = f.getType(); 178 | if (fc.isArray()) 179 | throw new RuntimeException("copyFrom - array types not implemented"); 180 | else if (!isPrimitive(fc)) 181 | ((Message) f.get(this)).copyFrom((Message) f.get(source)); 182 | else { 183 | Object value = f.get(source); 184 | f.set(this, value); 185 | } 186 | } 187 | } 188 | catch (IllegalAccessException ex) { 189 | throw new RuntimeException ("copyFrom error", ex); 190 | } 191 | catch (ClassCastException ex) { 192 | throw new RuntimeException ("copyFrom error", ex); 193 | } 194 | // ReflectiveOperationException is not available on Android (Java 1.6) 195 | /* 196 | catch (ReflectiveOperationException ex) { 197 | throw new RuntimeException ("copyFrom error", ex); 198 | } 199 | */ 200 | } 201 | 202 | } 203 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/MessageException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | public class MessageException extends Exception { 23 | 24 | public MessageException(String message) { 25 | super(message); 26 | } 27 | 28 | public MessageException(String message, Throwable cause) { 29 | super(message, cause); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/MessageType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | import java.lang.annotation.*; 23 | 24 | @Retention(RetentionPolicy.RUNTIME) 25 | @Target(ElementType.TYPE) 26 | public @interface MessageType { 27 | String string() default ""; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/Time.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "std_msgs/Time") 23 | public class Time extends TimePrimitive { 24 | } 25 | -------------------------------------------------------------------------------- /src/com/jilk/ros/message/TimePrimitive.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.message; 21 | 22 | @MessageType(string = "time") 23 | public class TimePrimitive extends Message { 24 | public int secs; // when requesting this format from ROSbridge, it uses 'sec' (no 's') 25 | public int nsecs; // when requesting this format from ROSbridge, it uses 'nsec' 26 | } 27 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/Empty.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "std_msgs/Empty") 26 | public class Empty extends Message { 27 | } 28 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/GetTime.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | import com.jilk.ros.message.TimePrimitive; 25 | 26 | 27 | @MessageType(string = "rosapi/GetTimeResponse") 28 | public class GetTime extends Message { 29 | public TimePrimitive time; 30 | } 31 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/MessageDetails.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/MessageDetails") 26 | public class MessageDetails extends Message { 27 | public TypeDef[] typedefs; 28 | } 29 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/Nodes.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/Nodes") 26 | public class Nodes extends Message { 27 | public String[] nodes; 28 | } 29 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/Service.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/Service") 26 | public class Service extends Message { 27 | public String service; 28 | 29 | public Service() {} 30 | 31 | public Service(String service) { 32 | this.service = service; 33 | } 34 | } -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/Services.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/Services") 26 | public class Services extends Message { 27 | public String[] services; 28 | } 29 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/Topic.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/Topic") 26 | public class Topic extends Message { 27 | public String topic; 28 | 29 | public Topic() {} 30 | 31 | public Topic(String topic) { 32 | this.topic = topic; 33 | } 34 | } -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/Topics.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/Topics") 26 | public class Topics extends Message { 27 | public String[] topics; 28 | } 29 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/Type.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/Type") 26 | public class Type extends Message { 27 | public String type; 28 | 29 | public Type() {} 30 | 31 | public Type(String type) { 32 | this.type = type; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosapi/message/TypeDef.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosapi.message; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "rosapi/TypeDef") 26 | public class TypeDef extends Message { 27 | public String type; 28 | public String[] fieldnames; 29 | public String[] fieldtypes; 30 | public int[] fieldarraylen; 31 | public String[] examples; 32 | 33 | public static boolean match(String type, Class c) { 34 | boolean result = false; 35 | if ( 36 | (type.equals("bool") && ((boolean.class.equals(c)) || (Boolean.class.equals(c)))) || 37 | (type.equals("int8") && ((byte.class.equals(c)) || (Byte.class.equals(c)))) || 38 | (type.equals("byte") && ((byte.class.equals(c)) || (Byte.class.equals(c)))) || // deprecated 39 | (type.equals("uint8") && ((short.class.equals(c)) || (Short.class.equals(c)))) || 40 | (type.equals("char") && ((short.class.equals(c)) || (Short.class.equals(c)))) || // deprecated 41 | (type.equals("int16") && ((short.class.equals(c)) || (Short.class.equals(c)))) || 42 | (type.equals("uint16") && ((int.class.equals(c)) || (Integer.class.equals(c)))) || 43 | (type.equals("int32") && ((int.class.equals(c)) || (Integer.class.equals(c)))) || 44 | (type.equals("uint32") && ((long.class.equals(c)) || (Long.class.equals(c)))) || 45 | (type.equals("int64") && ((long.class.equals(c)) || (Long.class.equals(c)))) || 46 | (type.equals("float32") && ((float.class.equals(c)) || (Float.class.equals(c)))) || 47 | (type.equals("float64") && ((double.class.equals(c)) || (Double.class.equals(c)))) || 48 | (type.equals("uint64") && (java.math.BigInteger.class.equals(c))) || 49 | (type.equals("string") && (String.class.equals(c))) 50 | ) 51 | result = true; 52 | 53 | return result; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/FullMessageHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge; 21 | 22 | import com.jilk.ros.message.Message; 23 | 24 | public interface FullMessageHandler { 25 | public void onMessage(String id, T message); 26 | } 27 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/ROSBridgeClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge; 21 | 22 | import com.jilk.ros.Service; 23 | import com.jilk.ros.message.Message; 24 | import com.jilk.ros.ROSClient; 25 | import com.jilk.ros.rosapi.message.*; 26 | import com.jilk.ros.rosbridge.implementation.*; 27 | import com.jilk.ros.rosbridge.operation.*; 28 | import java.lang.reflect.Field; 29 | import com.jilk.ros.message.MessageType; 30 | 31 | public class ROSBridgeClient extends ROSClient { 32 | String uriString; 33 | ROSBridgeWebSocketClient client; 34 | 35 | public ROSBridgeClient(String uriString) { 36 | this.uriString = uriString; 37 | } 38 | 39 | @Override 40 | public boolean connect() { 41 | return connect(null); 42 | } 43 | 44 | @Override 45 | public boolean connect(ROSClient.ConnectionStatusListener listener) { 46 | boolean result = false; 47 | client = ROSBridgeWebSocketClient.create(uriString); 48 | if (client != null) { 49 | client.setListener(listener); 50 | try { 51 | result = client.connectBlocking(); 52 | } 53 | catch (InterruptedException ex) {} 54 | } 55 | return result; 56 | } 57 | 58 | @Override 59 | public void disconnect() { 60 | try { 61 | client.closeBlocking(); 62 | } 63 | catch (InterruptedException ex) {} 64 | } 65 | 66 | @Override 67 | public void send(Operation operation) { 68 | client.send(operation); 69 | } 70 | 71 | @Override 72 | public void register(Class c, 73 | String s, 74 | Class m, 75 | FullMessageHandler h) { 76 | client.register(c, s, m, h); 77 | } 78 | 79 | @Override 80 | public void unregister(Class c, String s) { 81 | client.unregister(c, s); 82 | } 83 | 84 | @Override 85 | public void setDebug(boolean debug) { 86 | client.setDebug(debug); 87 | } 88 | 89 | @Override 90 | public String[] getNodes() throws InterruptedException { 91 | Service nodeService = 92 | new Service("/rosapi/nodes", Empty.class, Nodes.class, this); 93 | return nodeService.callBlocking(new Empty()).nodes; 94 | } 95 | 96 | @Override 97 | public String[] getTopics() throws InterruptedException { 98 | Service topicsService = 99 | new Service("/rosapi/topics", Empty.class, Topics.class, this); 100 | return topicsService.callBlocking(new Empty()).topics; 101 | } 102 | 103 | @Override 104 | public String[] getServices() throws InterruptedException { 105 | Service servicesService = 106 | new Service("/rosapi/services", Empty.class, Services.class, this); 107 | return servicesService.callBlocking(new Empty()).services; 108 | } 109 | 110 | @Override 111 | public TypeDef getTopicMessageDetails(String topic) throws InterruptedException { 112 | return getTypeDetails(getTopicType(topic)); 113 | } 114 | 115 | @Override 116 | public TypeDef getServiceRequestDetails(String service) throws InterruptedException { 117 | return getTypeDetails(getServiceType(service), "Request", "/rosapi/service_request_details"); 118 | } 119 | 120 | @Override 121 | public TypeDef getServiceResponseDetails(String service) throws InterruptedException { 122 | return getTypeDetails(getServiceType(service), "Response", "/rosapi/service_response_details"); 123 | } 124 | 125 | @Override 126 | public TypeDef getTypeDetails(String type) throws InterruptedException { 127 | return getTypeDetails(type, "", "/rosapi/message_details"); 128 | } 129 | 130 | private TypeDef getTypeDetails(String type, String suffix, String serviceName) throws InterruptedException { 131 | Service messageDetailsService = 132 | new Service(serviceName, 133 | Type.class, MessageDetails.class, this); 134 | return findType(type + suffix, messageDetailsService.callBlocking(new Type(type)).typedefs); 135 | } 136 | 137 | private String getTopicType(String topic) throws InterruptedException { 138 | Service topicTypeService = 139 | new Service("/rosapi/topic_type", 140 | Topic.class, Type.class, this); 141 | return topicTypeService.callBlocking(new Topic(topic)).type; 142 | } 143 | 144 | private String getServiceType(String service) throws InterruptedException { 145 | Service serviceTypeService = 146 | new Service("/rosapi/service_type", 147 | com.jilk.ros.rosapi.message.Service.class, Type.class, this); 148 | return serviceTypeService.callBlocking(new com.jilk.ros.rosapi.message.Service(service)).type; 149 | } 150 | 151 | private TypeDef findType(String type, TypeDef[] types) { 152 | TypeDef result = null; 153 | for (TypeDef t : types) { 154 | if (t.type.equals(type)) { 155 | result = t; 156 | break; 157 | } 158 | } 159 | //System.out.println("ROSBridgeClient.findType: "); 160 | //result.print(); 161 | return result; 162 | } 163 | 164 | @Override 165 | public void typeMatch(TypeDef t, Class c) throws InterruptedException { 166 | if (c == null) 167 | throw new RuntimeException("No registered message type found for: " + t.type); 168 | Field[] fields = c.getFields(); 169 | for (int i = 0; i < t.fieldnames.length; i++) { 170 | 171 | // Field names 172 | String classFieldName = fields[i].getName(); 173 | String typeFieldName = t.fieldnames[i]; 174 | if (!classFieldName.equals(typeFieldName)) 175 | typeMatchError(t, c, "field name", typeFieldName, classFieldName); 176 | 177 | // Array type of field 178 | boolean typeIsArray = (t.fieldarraylen[i] >= 0); 179 | boolean fieldIsArray = fields[i].getType().isArray(); 180 | if (typeIsArray != fieldIsArray) 181 | typeMatchError(t, c, "array mismatch", typeFieldName, classFieldName); 182 | 183 | // Get base type of field 184 | Class fieldClass = fields[i].getType(); 185 | if (fieldIsArray) 186 | fieldClass = fields[i].getType().getComponentType(); 187 | String type = t.fieldtypes[i]; 188 | 189 | // Field type for primitives 190 | if (Message.isPrimitive(fieldClass)) { 191 | if (!TypeDef.match(type, fieldClass)) 192 | typeMatchError(t, c, "type mismatch", type, fieldClass.getName()); 193 | } 194 | 195 | // Field type for non-primitive classes, and recurse 196 | else { 197 | if (!Message.class.isAssignableFrom(fieldClass)) 198 | throw new RuntimeException("Member " + classFieldName + 199 | " of class " + fieldClass.getName() + " does not extend Message."); 200 | String fieldClassString = ((MessageType) fieldClass.getAnnotation(MessageType.class)).string(); 201 | if (!type.equals(fieldClassString)) 202 | typeMatchError(t, c, "message type mismatch", type, fieldClassString); 203 | typeMatch(getTypeDetails(type), fieldClass); 204 | } 205 | } 206 | } 207 | 208 | private void typeMatchError(TypeDef t, Class c, 209 | String error, String tString, String cString) { 210 | throw new RuntimeException("Type match error between " + 211 | t.type + " and " + c.getName() + ": " + 212 | error + ": \'" + tString + "\' does not match \'" + cString + "\'."); 213 | } 214 | 215 | @Override 216 | public Object getUnderlyingClient() { 217 | return client; 218 | } 219 | 220 | } 221 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/implementation/Base64.java: -------------------------------------------------------------------------------- 1 | package com.jilk.ros.rosbridge.implementation; 2 | 3 | import java.util.Arrays; 4 | 5 | /** A very fast and memory efficient class to encode and decode to and from BASE64 in full accordance 6 | * with RFC 2045.

7 | * On Windows XP sp1 with 1.4.2_04 and later ;), this encoder and decoder is about 10 times faster 8 | * on small arrays (10 - 1000 bytes) and 2-3 times as fast on larger arrays (10000 - 1000000 bytes) 9 | * compared to sun.misc.Encoder()/Decoder().

10 | * 11 | * On byte arrays the encoder is about 20% faster than Jakarta Commons Base64 Codec for encode and 12 | * about 50% faster for decoding large arrays. This implementation is about twice as fast on very small 13 | * arrays (< 30 bytes). If source/destination is a String this 14 | * version is about three times as fast due to the fact that the Commons Codec result has to be recoded 15 | * to a String from byte[], which is very expensive.

16 | * 17 | * This encode/decode algorithm doesn't create any temporary arrays as many other codecs do, it only 18 | * allocates the resulting array. This produces less garbage and it is possible to handle arrays twice 19 | * as large as algorithms that create a temporary array. (E.g. Jakarta Commons Codec). It is unknown 20 | * whether Sun's sun.misc.Encoder()/Decoder() produce temporary arrays but since performance 21 | * is quite low it probably does.

22 | * 23 | * The encoder produces the same output as the Sun one except that the Sun's encoder appends 24 | * a trailing line separator if the last character isn't a pad. Unclear why but it only adds to the 25 | * length and is probably a side effect. Both are in conformance with RFC 2045 though.
26 | * Commons codec seem to always att a trailing line separator.

27 | * 28 | * Note! 29 | * The encode/decode method pairs (types) come in three versions with the exact same algorithm and 30 | * thus a lot of code redundancy. This is to not create any temporary arrays for transcoding to/from different 31 | * format types. The methods not used can simply be commented out.

32 | * 33 | * There is also a "fast" version of all decode methods that works the same way as the normal ones, but 34 | * har a few demands on the decoded input. Normally though, these fast verions should be used if the source if 35 | * the input is known and it hasn't bee tampered with.

36 | * 37 | * If you find the code useful or you find a bug, please send me a note at base64 @ miginfocom . com. 38 | * 39 | * Licence (BSD): 40 | * ============== 41 | * 42 | * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (base64 @ miginfocom . com) 43 | * All rights reserved. 44 | * 45 | * Redistribution and use in source and binary forms, with or without modification, 46 | * are permitted provided that the following conditions are met: 47 | * Redistributions of source code must retain the above copyright notice, this list 48 | * of conditions and the following disclaimer. 49 | * Redistributions in binary form must reproduce the above copyright notice, this 50 | * list of conditions and the following disclaimer in the documentation and/or other 51 | * materials provided with the distribution. 52 | * Neither the name of the MiG InfoCom AB nor the names of its contributors may be 53 | * used to endorse or promote products derived from this software without specific 54 | * prior written permission. 55 | * 56 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 57 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 58 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 59 | * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 60 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 61 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 62 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 63 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 64 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 65 | * OF SUCH DAMAGE. 66 | * 67 | * @version 2.2 68 | * @author Mikael Grev 69 | * Date: 2004-aug-02 70 | * Time: 11:31:11 71 | */ 72 | 73 | public class Base64 74 | { 75 | private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); 76 | private static final int[] IA = new int[256]; 77 | static { 78 | Arrays.fill(IA, -1); 79 | for (int i = 0, iS = CA.length; i < iS; i++) 80 | IA[CA[i]] = i; 81 | IA['='] = 0; 82 | } 83 | 84 | // **************************************************************************************** 85 | // * char[] version 86 | // **************************************************************************************** 87 | 88 | /** Encodes a raw byte array into a BASE64 char[] representation i accordance with RFC 2045. 89 | * @param sArr The bytes to convert. If null or length 0 an empty array will be returned. 90 | * @param lineSep Optional "\r\n" after 76 characters, unless end of file.
91 | * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a 92 | * little faster. 93 | * @return A BASE64 encoded array. Never null. 94 | */ 95 | public final static char[] encodeToChar(byte[] sArr, boolean lineSep) 96 | { 97 | // Check special case 98 | int sLen = sArr != null ? sArr.length : 0; 99 | if (sLen == 0) 100 | return new char[0]; 101 | 102 | int eLen = (sLen / 3) * 3; // Length of even 24-bits. 103 | int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count 104 | int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array 105 | char[] dArr = new char[dLen]; 106 | 107 | // Encode even 24-bits 108 | for (int s = 0, d = 0, cc = 0; s < eLen;) { 109 | // Copy next three bytes into lower 24 bits of int, paying attension to sign. 110 | int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff); 111 | 112 | // Encode the int into four chars 113 | dArr[d++] = CA[(i >>> 18) & 0x3f]; 114 | dArr[d++] = CA[(i >>> 12) & 0x3f]; 115 | dArr[d++] = CA[(i >>> 6) & 0x3f]; 116 | dArr[d++] = CA[i & 0x3f]; 117 | 118 | // Add optional line separator 119 | if (lineSep && ++cc == 19 && d < dLen - 2) { 120 | dArr[d++] = '\r'; 121 | dArr[d++] = '\n'; 122 | cc = 0; 123 | } 124 | } 125 | 126 | // Pad and encode last bits if source isn't even 24 bits. 127 | int left = sLen - eLen; // 0 - 2. 128 | if (left > 0) { 129 | // Prepare the int 130 | int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0); 131 | 132 | // Set last four chars 133 | dArr[dLen - 4] = CA[i >> 12]; 134 | dArr[dLen - 3] = CA[(i >>> 6) & 0x3f]; 135 | dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '='; 136 | dArr[dLen - 1] = '='; 137 | } 138 | return dArr; 139 | } 140 | 141 | /** Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with 142 | * and without line separators. 143 | * @param sArr The source array. null or length 0 will return an empty array. 144 | * @return The decoded array of bytes. May be of length 0. Will be null if the legal characters 145 | * (including '=') isn't divideable by 4. (I.e. definitely corrupted). 146 | */ 147 | public final static byte[] decode(char[] sArr) 148 | { 149 | // Check special case 150 | int sLen = sArr != null ? sArr.length : 0; 151 | if (sLen == 0) 152 | return new byte[0]; 153 | 154 | // Count illegal characters (including '\r', '\n') to know what size the returned array will be, 155 | // so we don't have to reallocate & copy it later. 156 | int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) 157 | for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. 158 | if (IA[sArr[i]] < 0) 159 | sepCnt++; 160 | 161 | // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. 162 | if ((sLen - sepCnt) % 4 != 0) 163 | return null; 164 | 165 | int pad = 0; 166 | for (int i = sLen; i > 1 && IA[sArr[--i]] <= 0;) 167 | if (sArr[i] == '=') 168 | pad++; 169 | 170 | int len = ((sLen - sepCnt) * 6 >> 3) - pad; 171 | 172 | byte[] dArr = new byte[len]; // Preallocate byte[] of exact length 173 | 174 | for (int s = 0, d = 0; d < len;) { 175 | // Assemble three bytes into an int from four "valid" characters. 176 | int i = 0; 177 | for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. 178 | int c = IA[sArr[s++]]; 179 | if (c >= 0) 180 | i |= c << (18 - j * 6); 181 | else 182 | j--; 183 | } 184 | // Add the bytes 185 | dArr[d++] = (byte) (i >> 16); 186 | if (d < len) { 187 | dArr[d++]= (byte) (i >> 8); 188 | if (d < len) 189 | dArr[d++] = (byte) i; 190 | } 191 | } 192 | return dArr; 193 | } 194 | 195 | /** Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as 196 | * fast as {@link #decode(char[])}. The preconditions are:
197 | * + The array must have a line length of 76 chars OR no line separators at all (one line).
198 | * + Line separator must be "\r\n", as specified in RFC 2045 199 | * + The array must not contain illegal characters within the encoded string
200 | * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.
201 | * @param sArr The source array. Length 0 will return an empty array. null will throw an exception. 202 | * @return The decoded array of bytes. May be of length 0. 203 | */ 204 | public final static byte[] decodeFast(char[] sArr) 205 | { 206 | // Check special case 207 | int sLen = sArr.length; 208 | if (sLen == 0) 209 | return new byte[0]; 210 | 211 | int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. 212 | 213 | // Trim illegal chars from start 214 | while (sIx < eIx && IA[sArr[sIx]] < 0) 215 | sIx++; 216 | 217 | // Trim illegal chars from end 218 | while (eIx > 0 && IA[sArr[eIx]] < 0) 219 | eIx--; 220 | 221 | // get the padding count (=) (0, 1 or 2) 222 | int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end. 223 | int cCnt = eIx - sIx + 1; // Content count including possible separators 224 | int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0; 225 | 226 | int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes 227 | byte[] dArr = new byte[len]; // Preallocate byte[] of exact length 228 | 229 | // Decode all but the last 0 - 2 bytes. 230 | int d = 0; 231 | for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { 232 | // Assemble three bytes into an int from four "valid" characters. 233 | int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]]; 234 | 235 | // Add the bytes 236 | dArr[d++] = (byte) (i >> 16); 237 | dArr[d++] = (byte) (i >> 8); 238 | dArr[d++] = (byte) i; 239 | 240 | // If line separator, jump over it. 241 | if (sepCnt > 0 && ++cc == 19) { 242 | sIx += 2; 243 | cc = 0; 244 | } 245 | } 246 | 247 | if (d < len) { 248 | // Decode last 1-3 bytes (incl '=') into 1-3 bytes 249 | int i = 0; 250 | for (int j = 0; sIx <= eIx - pad; j++) 251 | i |= IA[sArr[sIx++]] << (18 - j * 6); 252 | 253 | for (int r = 16; d < len; r -= 8) 254 | dArr[d++] = (byte) (i >> r); 255 | } 256 | 257 | return dArr; 258 | } 259 | 260 | // **************************************************************************************** 261 | // * byte[] version 262 | // **************************************************************************************** 263 | 264 | /** Encodes a raw byte array into a BASE64 byte[] representation i accordance with RFC 2045. 265 | * @param sArr The bytes to convert. If null or length 0 an empty array will be returned. 266 | * @param lineSep Optional "\r\n" after 76 characters, unless end of file.
267 | * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a 268 | * little faster. 269 | * @return A BASE64 encoded array. Never null. 270 | */ 271 | public final static byte[] encodeToByte(byte[] sArr, boolean lineSep) 272 | { 273 | // Check special case 274 | int sLen = sArr != null ? sArr.length : 0; 275 | if (sLen == 0) 276 | return new byte[0]; 277 | 278 | int eLen = (sLen / 3) * 3; // Length of even 24-bits. 279 | int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count 280 | int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array 281 | byte[] dArr = new byte[dLen]; 282 | 283 | // Encode even 24-bits 284 | for (int s = 0, d = 0, cc = 0; s < eLen;) { 285 | // Copy next three bytes into lower 24 bits of int, paying attension to sign. 286 | int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff); 287 | 288 | // Encode the int into four chars 289 | dArr[d++] = (byte) CA[(i >>> 18) & 0x3f]; 290 | dArr[d++] = (byte) CA[(i >>> 12) & 0x3f]; 291 | dArr[d++] = (byte) CA[(i >>> 6) & 0x3f]; 292 | dArr[d++] = (byte) CA[i & 0x3f]; 293 | 294 | // Add optional line separator 295 | if (lineSep && ++cc == 19 && d < dLen - 2) { 296 | dArr[d++] = '\r'; 297 | dArr[d++] = '\n'; 298 | cc = 0; 299 | } 300 | } 301 | 302 | // Pad and encode last bits if source isn't an even 24 bits. 303 | int left = sLen - eLen; // 0 - 2. 304 | if (left > 0) { 305 | // Prepare the int 306 | int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0); 307 | 308 | // Set last four chars 309 | dArr[dLen - 4] = (byte) CA[i >> 12]; 310 | dArr[dLen - 3] = (byte) CA[(i >>> 6) & 0x3f]; 311 | dArr[dLen - 2] = left == 2 ? (byte) CA[i & 0x3f] : (byte) '='; 312 | dArr[dLen - 1] = '='; 313 | } 314 | return dArr; 315 | } 316 | 317 | /** Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with 318 | * and without line separators. 319 | * @param sArr The source array. Length 0 will return an empty array. null will throw an exception. 320 | * @return The decoded array of bytes. May be of length 0. Will be null if the legal characters 321 | * (including '=') isn't divideable by 4. (I.e. definitely corrupted). 322 | */ 323 | public final static byte[] decode(byte[] sArr) 324 | { 325 | // Check special case 326 | int sLen = sArr.length; 327 | 328 | // Count illegal characters (including '\r', '\n') to know what size the returned array will be, 329 | // so we don't have to reallocate & copy it later. 330 | int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) 331 | for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. 332 | if (IA[sArr[i] & 0xff] < 0) 333 | sepCnt++; 334 | 335 | // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. 336 | if ((sLen - sepCnt) % 4 != 0) 337 | return null; 338 | 339 | int pad = 0; 340 | for (int i = sLen; i > 1 && IA[sArr[--i] & 0xff] <= 0;) 341 | if (sArr[i] == '=') 342 | pad++; 343 | 344 | int len = ((sLen - sepCnt) * 6 >> 3) - pad; 345 | 346 | byte[] dArr = new byte[len]; // Preallocate byte[] of exact length 347 | 348 | for (int s = 0, d = 0; d < len;) { 349 | // Assemble three bytes into an int from four "valid" characters. 350 | int i = 0; 351 | for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. 352 | int c = IA[sArr[s++] & 0xff]; 353 | if (c >= 0) 354 | i |= c << (18 - j * 6); 355 | else 356 | j--; 357 | } 358 | 359 | // Add the bytes 360 | dArr[d++] = (byte) (i >> 16); 361 | if (d < len) { 362 | dArr[d++]= (byte) (i >> 8); 363 | if (d < len) 364 | dArr[d++] = (byte) i; 365 | } 366 | } 367 | 368 | return dArr; 369 | } 370 | 371 | 372 | /** Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as 373 | * fast as {@link #decode(byte[])}. The preconditions are:
374 | * + The array must have a line length of 76 chars OR no line separators at all (one line).
375 | * + Line separator must be "\r\n", as specified in RFC 2045 376 | * + The array must not contain illegal characters within the encoded string
377 | * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.
378 | * @param sArr The source array. Length 0 will return an empty array. null will throw an exception. 379 | * @return The decoded array of bytes. May be of length 0. 380 | */ 381 | public final static byte[] decodeFast(byte[] sArr) 382 | { 383 | // Check special case 384 | int sLen = sArr.length; 385 | if (sLen == 0) 386 | return new byte[0]; 387 | 388 | int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. 389 | 390 | // Trim illegal chars from start 391 | while (sIx < eIx && IA[sArr[sIx] & 0xff] < 0) 392 | sIx++; 393 | 394 | // Trim illegal chars from end 395 | while (eIx > 0 && IA[sArr[eIx] & 0xff] < 0) 396 | eIx--; 397 | 398 | // get the padding count (=) (0, 1 or 2) 399 | int pad = sArr[eIx] == '=' ? (sArr[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end. 400 | int cCnt = eIx - sIx + 1; // Content count including possible separators 401 | int sepCnt = sLen > 76 ? (sArr[76] == '\r' ? cCnt / 78 : 0) << 1 : 0; 402 | 403 | int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes 404 | byte[] dArr = new byte[len]; // Preallocate byte[] of exact length 405 | 406 | // Decode all but the last 0 - 2 bytes. 407 | int d = 0; 408 | for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { 409 | // Assemble three bytes into an int from four "valid" characters. 410 | int i = IA[sArr[sIx++]] << 18 | IA[sArr[sIx++]] << 12 | IA[sArr[sIx++]] << 6 | IA[sArr[sIx++]]; 411 | 412 | // Add the bytes 413 | dArr[d++] = (byte) (i >> 16); 414 | dArr[d++] = (byte) (i >> 8); 415 | dArr[d++] = (byte) i; 416 | 417 | // If line separator, jump over it. 418 | if (sepCnt > 0 && ++cc == 19) { 419 | sIx += 2; 420 | cc = 0; 421 | } 422 | } 423 | 424 | if (d < len) { 425 | // Decode last 1-3 bytes (incl '=') into 1-3 bytes 426 | int i = 0; 427 | for (int j = 0; sIx <= eIx - pad; j++) 428 | i |= IA[sArr[sIx++]] << (18 - j * 6); 429 | 430 | for (int r = 16; d < len; r -= 8) 431 | dArr[d++] = (byte) (i >> r); 432 | } 433 | 434 | return dArr; 435 | } 436 | 437 | // **************************************************************************************** 438 | // * String version 439 | // **************************************************************************************** 440 | 441 | /** Encodes a raw byte array into a BASE64 String representation i accordance with RFC 2045. 442 | * @param sArr The bytes to convert. If null or length 0 an empty array will be returned. 443 | * @param lineSep Optional "\r\n" after 76 characters, unless end of file.
444 | * No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a 445 | * little faster. 446 | * @return A BASE64 encoded array. Never null. 447 | */ 448 | public final static String encodeToString(byte[] sArr, boolean lineSep) 449 | { 450 | // Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower. 451 | return new String(encodeToChar(sArr, lineSep)); 452 | } 453 | 454 | /** Decodes a BASE64 encoded String. All illegal characters will be ignored and can handle both strings with 455 | * and without line separators.
456 | * Note! It can be up to about 2x the speed to call decode(str.toCharArray()) instead. That 457 | * will create a temporary array though. This version will use str.charAt(i) to iterate the string. 458 | * @param str The source string. null or length 0 will return an empty array. 459 | * @return The decoded array of bytes. May be of length 0. Will be null if the legal characters 460 | * (including '=') isn't divideable by 4. (I.e. definitely corrupted). 461 | */ 462 | public final static byte[] decode(String str) 463 | { 464 | // Check special case 465 | int sLen = str != null ? str.length() : 0; 466 | if (sLen == 0) 467 | return new byte[0]; 468 | 469 | // Count illegal characters (including '\r', '\n') to know what size the returned array will be, 470 | // so we don't have to reallocate & copy it later. 471 | int sepCnt = 0; // Number of separator characters. (Actually illegal characters, but that's a bonus...) 472 | for (int i = 0; i < sLen; i++) // If input is "pure" (I.e. no line separators or illegal chars) base64 this loop can be commented out. 473 | if (IA[str.charAt(i)] < 0) 474 | sepCnt++; 475 | 476 | // Check so that legal chars (including '=') are evenly divideable by 4 as specified in RFC 2045. 477 | if ((sLen - sepCnt) % 4 != 0) 478 | return null; 479 | 480 | // Count '=' at end 481 | int pad = 0; 482 | for (int i = sLen; i > 1 && IA[str.charAt(--i)] <= 0;) 483 | if (str.charAt(i) == '=') 484 | pad++; 485 | 486 | int len = ((sLen - sepCnt) * 6 >> 3) - pad; 487 | 488 | byte[] dArr = new byte[len]; // Preallocate byte[] of exact length 489 | 490 | for (int s = 0, d = 0; d < len;) { 491 | // Assemble three bytes into an int from four "valid" characters. 492 | int i = 0; 493 | for (int j = 0; j < 4; j++) { // j only increased if a valid char was found. 494 | int c = IA[str.charAt(s++)]; 495 | if (c >= 0) 496 | i |= c << (18 - j * 6); 497 | else 498 | j--; 499 | } 500 | // Add the bytes 501 | dArr[d++] = (byte) (i >> 16); 502 | if (d < len) { 503 | dArr[d++]= (byte) (i >> 8); 504 | if (d < len) 505 | dArr[d++] = (byte) i; 506 | } 507 | } 508 | return dArr; 509 | } 510 | 511 | /** Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as 512 | * fast as {@link #decode(String)}. The preconditions are:
513 | * + The array must have a line length of 76 chars OR no line separators at all (one line).
514 | * + Line separator must be "\r\n", as specified in RFC 2045 515 | * + The array must not contain illegal characters within the encoded string
516 | * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.
517 | * @param s The source string. Length 0 will return an empty array. null will throw an exception. 518 | * @return The decoded array of bytes. May be of length 0. 519 | */ 520 | public final static byte[] decodeFast(String s) 521 | { 522 | // Check special case 523 | int sLen = s.length(); 524 | if (sLen == 0) 525 | return new byte[0]; 526 | 527 | int sIx = 0, eIx = sLen - 1; // Start and end index after trimming. 528 | 529 | // Trim illegal chars from start 530 | while (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0) 531 | sIx++; 532 | 533 | // Trim illegal chars from end 534 | while (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0) 535 | eIx--; 536 | 537 | // get the padding count (=) (0, 1 or 2) 538 | int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end. 539 | int cCnt = eIx - sIx + 1; // Content count including possible separators 540 | int sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0; 541 | 542 | int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytes 543 | byte[] dArr = new byte[len]; // Preallocate byte[] of exact length 544 | 545 | // Decode all but the last 0 - 2 bytes. 546 | int d = 0; 547 | for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { 548 | // Assemble three bytes into an int from four "valid" characters. 549 | int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)]; 550 | 551 | // Add the bytes 552 | dArr[d++] = (byte) (i >> 16); 553 | dArr[d++] = (byte) (i >> 8); 554 | dArr[d++] = (byte) i; 555 | 556 | // If line separator, jump over it. 557 | if (sepCnt > 0 && ++cc == 19) { 558 | sIx += 2; 559 | cc = 0; 560 | } 561 | } 562 | 563 | if (d < len) { 564 | // Decode last 1-3 bytes (incl '=') into 1-3 bytes 565 | int i = 0; 566 | for (int j = 0; sIx <= eIx - pad; j++) 567 | i |= IA[s.charAt(sIx++)] << (18 - j * 6); 568 | 569 | for (int r = 16; d < len; r -= 8) 570 | dArr[d++] = (byte) (i >> r); 571 | } 572 | 573 | return dArr; 574 | } 575 | } -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/implementation/JSON.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.implementation; 21 | 22 | import org.json.simple.*; 23 | import org.json.simple.parser.*; 24 | import java.io.StringReader; 25 | import java.lang.reflect.Array; 26 | import java.lang.reflect.Field; 27 | 28 | import com.jilk.ros.message.Message; 29 | import com.jilk.ros.rosbridge.indication.Indication; 30 | import com.jilk.ros.rosbridge.operation.Wrapper; 31 | 32 | 33 | // The slightly crazy abstractions here are designed to isolate knowledge of 34 | // the JSON library and data types from the Operation details of rosbridge. 35 | // Why is this important? A few reasons I can see. First, we might want 36 | // to change JSON libraries and this encapsulates all use of JSON-simple. 37 | // Second, as much as possible I would like the semantics of the rosbridge 38 | // protocol to be encapsulated in the Operation and its subclasses rather 39 | // than in a module that is essentially about serialization. 40 | // 41 | // Unfortunately the hierarchical Message abstraction is a bit broken 42 | // at the top level. Beginning at the actual operation (e.g., Publish), the 43 | // types of the fields are determined either by the fields themselves or by 44 | // an indicator. However, the type of the operation itself is not determined 45 | // this way, because the indicator is in the object itself, which means it 46 | // would have to be created before its type is known. Rather than build in 47 | // exceptions, I elected to create a "Wrapper" operation type that simply 48 | // wraps the concrete operation and copies its "op" field. 49 | // 50 | 51 | public class JSON { 52 | 53 | /** 54 | * Translates a Message recursively into JSON. Normally the Message is also an 55 | * Operation, but it does not have to be. The caller constructs a complete 56 | * message using @Operation and @Message types. This includes situations 57 | * where one or more fields are marked to be turned into arrays, using @AsArray. 58 | * @param m the @Message object to be recursively translated. 59 | * @return the complete JSON string. 60 | */ 61 | public static String toJSON(Message m) { 62 | JSONObject jo = convertObjectToJSONObject(m); // Object to JSON-Simple 63 | return jo.toJSONString(); // JSON-Simple to string 64 | } 65 | 66 | /** 67 | * Translates JSON into a hierarchical Operation/Message structure. 68 | * This includes handling fields that are @Indicated and @AsArray. If the 69 | * @Class parameter is a @Wrapper, this is a special case whereby the 70 | * object is wrapped to create a consistent hierarchy. 71 | * @param json the source JSON string 72 | * @param c the top level class of the JSON. Normally @Wrapper 73 | * @param r the @Registry containing topic registrations 74 | * @return the fully instantiated message hierarchy represented 75 | * by the JSON string. 76 | */ 77 | public static Message toMessage(String json, Class c, Registry r) { 78 | JSONObject joUnwrapped = convertStringToJSONObject(json); // String to JSON-Simple 79 | JSONObject jo = joUnwrapped; 80 | if (Wrapper.class.isAssignableFrom(c)) 81 | jo = wrap(joUnwrapped, c); // wrap: a hack to make the hierarchy homogeneous 82 | return convertJSONObjectToMessage(jo, c, r); // JSON-Simple to Message 83 | } 84 | 85 | // *** Create JSON from Messages *** // 86 | 87 | // Translate the object into a JSON-Simple object, field-by-field, 88 | // recursively via convertElementToJSON. 89 | // except for the case where AsArray is indicated 90 | private static JSONObject convertObjectToJSONObject(Object o) { 91 | JSONObject result = new JSONObject(); 92 | for (Field f : o.getClass().getFields()) { 93 | Object fieldObject = getFieldObject(f, o); 94 | if (fieldObject != null) { 95 | Object resultObject; 96 | if (Indication.isBase64Encoded(f)) 97 | resultObject = convertByteArrayToBase64JSONString(fieldObject); 98 | else if (Indication.asArray(f)) 99 | resultObject = convertObjectToJSONArray(fieldObject); 100 | else resultObject = convertElementToJSON(fieldObject); 101 | result.put(f.getName(), resultObject); 102 | } 103 | } 104 | return result; 105 | } 106 | 107 | // Convert an array type to a JSON-Simple array, element-by-element, 108 | // recursively via convertElementToJSON. 109 | private static JSONArray convertArrayToJSONArray(Object array) { 110 | JSONArray result = new JSONArray(); 111 | for (int i = 0; i < Array.getLength(array); i++) { 112 | Object elementObject = Array.get(array, i); 113 | if (elementObject != null) { 114 | Object resultObject = convertElementToJSON(elementObject); 115 | result.add(resultObject); 116 | } 117 | } 118 | return result; 119 | } 120 | 121 | // For AsArray objects, convert the object to a JSON-Simple array 122 | // NOTE: This relies on later versions of the JDK providing 123 | // the fields in order. 124 | private static JSONArray convertObjectToJSONArray(Object o) { 125 | JSONArray result = new JSONArray(); 126 | for (Field f : o.getClass().getFields()) { 127 | Object fieldObject = getFieldObject(f, o); 128 | if (fieldObject != null) { 129 | Object resultObject = convertElementToJSON(fieldObject); 130 | result.add(resultObject); 131 | } 132 | } 133 | return result; 134 | } 135 | 136 | // Convert the individual field or array element items recursively 137 | private static Object convertElementToJSON(Object elementObject) { 138 | Class elementClass = elementObject.getClass(); 139 | Object resultObject; 140 | if (Message.isPrimitive(elementClass)) 141 | resultObject = elementObject; 142 | else if (elementClass.isArray()) 143 | resultObject = convertArrayToJSONArray(elementObject); 144 | else 145 | resultObject = convertObjectToJSONObject(elementObject); 146 | return resultObject; 147 | } 148 | 149 | // Special case for Base 64-encoded fields 150 | private static Object convertByteArrayToBase64JSONString(Object fieldObject) { 151 | return Base64.encodeToString((byte[]) fieldObject, false); 152 | } 153 | 154 | // This is just to buffer the code from the exception. Better error 155 | // handling needed here. 156 | private static Object getFieldObject(Field f, Object o) { 157 | Object fo = null; 158 | try { 159 | fo = f.get(o); 160 | } 161 | catch (IllegalAccessException ex) { 162 | ex.printStackTrace(); 163 | } 164 | return fo; 165 | } 166 | 167 | // *** Create Messages from JSON *** // 168 | 169 | // Use the JSON-simple parser to create the JSON-Simple object 170 | private static JSONObject convertStringToJSONObject(String json) { 171 | JSONObject result = null; 172 | StringReader r = new StringReader(json); 173 | JSONParser jp = new JSONParser(); 174 | try { 175 | result = (JSONObject) jp.parse(r); 176 | } 177 | catch (Throwable t) { 178 | System.out.println(t.getMessage()); 179 | } 180 | r.close(); 181 | return result; 182 | } 183 | 184 | // A bit of a hack to create a consistent hierarchy with jsonbridge operations 185 | // At least it does not depend on any specific field names, it just copies the 186 | // Indicator and Indicated fields. 187 | private static JSONObject wrap(JSONObject jo, Class c) { 188 | JSONObject result = new JSONObject(); 189 | String indicatorName = Indication.getIndicatorName(c); 190 | String indicatedName = Indication.getIndicatedName(c); 191 | result.put(indicatorName, jo.get(indicatorName)); 192 | result.put(indicatedName, jo); 193 | return result; 194 | } 195 | 196 | // Convert the JSON-Simple object to the indicated message, field-by-field 197 | // recursively via convertElementToField. 198 | private static Message convertJSONObjectToMessage(JSONObject jo, Class c, Registry r) { 199 | //System.out.println("JSON.convertJSONObjectToMessage: " + jo.toJSONString()); 200 | try { 201 | Message result = (Message) c.newInstance(); 202 | for (Field f : c.getFields()) { 203 | Class fc = getFieldClass(result, jo, f, r); 204 | Object lookup = jo.get(f.getName()); 205 | if (lookup != null) { 206 | Object value = convertElementToField(lookup, fc, f, r); 207 | f.set(result, value); 208 | } 209 | } 210 | return result; 211 | } 212 | catch (Exception ex) { 213 | ex.printStackTrace(); 214 | return null; 215 | } 216 | } 217 | 218 | // Convert the JSON-Simple array to the indicated message, element-by-element 219 | // recursively via convertElementToField. 220 | private static Object convertJSONArrayToArray(JSONArray ja, Class c, Registry r) { 221 | Object result = Array.newInstance(c, ja.size()); 222 | for (int i = 0; i < ja.size(); i++) { 223 | Object lookup = ja.get(i); 224 | Object value = null; 225 | if (lookup != null) { 226 | if (lookup.getClass().equals(JSONObject.class)) 227 | value = convertJSONObjectToMessage((JSONObject) lookup, c, r); 228 | else if (lookup.getClass().equals(JSONArray.class)) // this is not actually allowed in ROS 229 | value = convertJSONArrayToArray((JSONArray) lookup, c.getComponentType(), r); 230 | else 231 | value = convertJSONPrimitiveToPrimitive(lookup, c); 232 | Array.set(result, i, value); 233 | } 234 | } 235 | 236 | return result; 237 | } 238 | 239 | // Convert a JSON-Simple array to a Message, field-by-field of the Message, 240 | // element-by-element of the array, recursively via convertElementToField. 241 | // NOTE: This relies on later versions of the JDK providing 242 | // the fields in order. 243 | private static Message convertJSONArrayToMessage(JSONArray ja, Class c, Registry r) { 244 | try { 245 | Message result = (Message) c.newInstance(); 246 | int arrayIndex = 0; 247 | for (Field f : c.getFields()) { 248 | Class fc = getFieldClass(result, null, f, r); 249 | Object lookup = ja.get(arrayIndex++); // yes we are assuming that the fields are delivered in order 250 | if (lookup != null) { 251 | Object value = convertElementToField(lookup, fc, f, r); 252 | f.set(result, value); 253 | } 254 | } 255 | 256 | return result; 257 | } 258 | catch (Exception ex) { 259 | ex.printStackTrace(); 260 | return null; 261 | } 262 | } 263 | 264 | // Convert an individual array or object element to a field in the Message, 265 | // recursively, and applying AsArray if needed. 266 | private static Object convertElementToField(Object element, Class fc, Field f, Registry r) { 267 | //System.out.println("JSON.convertElementToField: " + f.getName() + " " + fc.getName()); 268 | Object value; 269 | if (element.getClass().equals(JSONObject.class)) { 270 | //System.out.println("JSON.convertElementToField: JSON Object " + ((JSONObject) element).toJSONString()); 271 | value = convertJSONObjectToMessage((JSONObject) element, fc, r); 272 | } 273 | else if (element.getClass().equals(JSONArray.class)) { 274 | //System.out.println("JSON.convertElementToField: JSON Array " + ((JSONArray) element).toJSONString()); 275 | if (Indication.asArray(f)) 276 | value = convertJSONArrayToMessage((JSONArray) element, fc, r); 277 | else value = convertJSONArrayToArray((JSONArray) element, fc, r); 278 | } 279 | else { 280 | //System.out.println("JSON.convertElementToField: Primitive " + element); 281 | if (Indication.isBase64Encoded(f)) 282 | value = convertBase64JSONStringToByteArray(element); 283 | else value = convertJSONPrimitiveToPrimitive(element, fc); 284 | } 285 | 286 | return value; 287 | } 288 | 289 | // Note that this is not checking ranges 290 | public static Object convertJSONPrimitiveToPrimitive(Object o, Class c) { 291 | Object result = o; 292 | if (c.isPrimitive() || Number.class.isAssignableFrom(c)) { 293 | if (c.equals(double.class) || c.equals(Double.class)) 294 | result = new Double(((Number) o).doubleValue()); 295 | else if (c.equals(float.class) || c.equals(Float.class)) 296 | result = new Float(((Number) o).floatValue()); 297 | else if (c.equals(long.class) || c.equals(Long.class)) 298 | result = new Long(((Number) o).longValue()); 299 | else if (int.class.equals(c) || c.equals(Integer.class)) 300 | result = new Integer(((Number) o).intValue()); 301 | else if (c.equals(short.class) || c.equals(Short.class)) 302 | result = new Short(((Number) o).shortValue()); 303 | else if (c.equals(byte.class) || c.equals(Byte.class)) 304 | result = new Byte(((Number) o).byteValue()); 305 | } 306 | return result; 307 | } 308 | 309 | public static byte[] convertBase64JSONStringToByteArray(Object element) { 310 | return Base64.decode((String) element); 311 | } 312 | 313 | // Determine the target class of a field in the object or array, based 314 | // directly on the field's type, or using the Indicator if applicable, 315 | // The Indicator field only provides the topic/service, so we have to look 316 | // up the Class in the registry. 317 | public static Class getFieldClass(Message parent, JSONObject jo, Field f, Registry r) { 318 | Class fc; 319 | fc = f.getType(); 320 | if (fc.isArray()) 321 | fc = f.getType().getComponentType(); 322 | if (Indication.isIndicated(f) && (jo != null)) { 323 | //fc = Indication.getIndication(parent, 324 | // (String) jo.get(Indication.getIndicatorName(parent.getClass()))); 325 | fc = r.lookup(parent.getClass(), 326 | (String) jo.get(Indication.getIndicatorName(parent.getClass()))); 327 | //System.out.println("JSON.getFieldClass: parent class " + parent.getClass().getName() + 328 | // " Indicator: " + Indication.getIndicatorName(parent.getClass()) + 329 | // " result: " + fc.getName()); 330 | } 331 | return fc; 332 | } 333 | } 334 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/implementation/ROSBridgeWebSocketClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.implementation; 21 | 22 | import java.net.URI; 23 | import java.net.URISyntaxException; 24 | 25 | import org.java_websocket.client.WebSocketClient; 26 | import org.java_websocket.handshake.ServerHandshake; 27 | 28 | import com.jilk.ros.rosbridge.FullMessageHandler; 29 | import com.jilk.ros.rosbridge.operation.Operation; 30 | import com.jilk.ros.message.Message; 31 | import com.jilk.ros.rosbridge.operation.Publish; 32 | import com.jilk.ros.rosbridge.operation.ServiceResponse; 33 | import com.jilk.ros.ROSClient; 34 | import org.java_websocket.framing.CloseFrame; 35 | 36 | import java.lang.reflect.Field; 37 | import java.nio.channels.SocketChannel; 38 | import java.net.Socket; 39 | 40 | 41 | public class ROSBridgeWebSocketClient extends WebSocketClient { 42 | private Registry classes; 43 | private Registry handlers; 44 | private boolean debug; 45 | private ROSClient.ConnectionStatusListener listener; 46 | 47 | ROSBridgeWebSocketClient(URI serverURI) { 48 | super(serverURI); 49 | classes = new Registry(); 50 | handlers = new Registry(); 51 | Operation.initialize(classes); // note, this ensures that the Message Map is initialized too 52 | listener = null; 53 | } 54 | 55 | public static ROSBridgeWebSocketClient create(String URIString) { 56 | ROSBridgeWebSocketClient client = null; 57 | try { 58 | URI uri = new URI(URIString); 59 | client = new ROSBridgeWebSocketClient(uri); 60 | } 61 | catch (URISyntaxException ex) { 62 | ex.printStackTrace(); 63 | } 64 | return client; 65 | } 66 | 67 | public void setListener(ROSClient.ConnectionStatusListener listener) { 68 | this.listener = listener; 69 | } 70 | 71 | @Override 72 | public void onOpen(ServerHandshake handshakedata) { 73 | if (listener != null) 74 | listener.onConnect(); 75 | } 76 | 77 | @Override 78 | public void onMessage(String message) { 79 | if (debug) System.out.println(" " + json); 158 | send(json); 159 | } 160 | 161 | public void register(Class c, 162 | String s, 163 | Class m, 164 | FullMessageHandler h) { 165 | Message.register(m, classes.get(Message.class)); 166 | classes.register(c, s, m); 167 | if (h != null) 168 | handlers.register(c, s, h); 169 | } 170 | 171 | public void unregister(Class c, String s) { 172 | handlers.unregister(c, s); 173 | // Note that there is no concept of unregistering a class - it can get replaced is all 174 | } 175 | 176 | public Class getRegisteredMessage(String messageString) { 177 | return classes.lookup(Message.class, messageString); 178 | } 179 | 180 | public void setDebug(boolean debug) { 181 | this.debug = debug; 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/implementation/Registry.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.implementation; 21 | 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | 25 | public class Registry extends HashMap> { 26 | 27 | public void register(Class c, String s, T t) { 28 | Map table = get(c); 29 | if (table == null) { 30 | table = new HashMap(); 31 | put(c, table); 32 | } 33 | table.put(s, t); 34 | } 35 | 36 | public void unregister(Class c, String s) { 37 | Map table = get(c); 38 | if (table != null) 39 | table.remove(s); 40 | } 41 | 42 | public T lookup(Class c, String s) { 43 | T result = null; 44 | Map table = get(c); 45 | if (table != null) 46 | result = table.get(s); 47 | return result; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/indication/AsArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.indication; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.FIELD) 29 | public @interface AsArray { 30 | } 31 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/indication/Base64Encoded.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.indication; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.FIELD) 29 | public @interface Base64Encoded { 30 | } 31 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/indication/Indicate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.indication; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.METHOD) 29 | public @interface Indicate { 30 | // if later we want multiple indicated fields, use an int here 31 | } 32 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/indication/Indicated.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.indication; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.FIELD) 29 | public @interface Indicated { 30 | // if later we want multiple indicated fields, use an int here 31 | } 32 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/indication/Indication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.indication; 21 | 22 | import java.lang.reflect.Field; 23 | import java.lang.reflect.Method; 24 | 25 | public class Indication { 26 | public static boolean isIndicated(Field f) { 27 | return (f.getAnnotation(Indicated.class) != null); 28 | } 29 | 30 | public static boolean asArray(Field f) { 31 | return (f.getAnnotation(AsArray.class) != null); 32 | } 33 | 34 | public static boolean isBase64Encoded(Field f) { 35 | return ((f.getAnnotation(Base64Encoded.class) != null) && 36 | f.getType().isArray() && 37 | f.getType().getComponentType().equals(byte.class)); 38 | } 39 | 40 | public static String getIndicatorName(Class c) { 41 | return getName(c, Indicator.class); 42 | } 43 | 44 | public static String getIndicatedName(Class c) { 45 | return getName(c, Indicated.class); 46 | } 47 | 48 | private static String getName(Class c, Class annotation) { 49 | String result = null; 50 | for (Field f : c.getFields()) { 51 | if (f.getAnnotation(annotation) != null) { 52 | result = f.getName(); 53 | break; 54 | } 55 | } 56 | return result; 57 | } 58 | 59 | /* 60 | public static Class getIndication(Object o, String s) { 61 | Class c = o.getClass(); 62 | Class result = null; 63 | try { 64 | Method m = getIndicateMethod(c); 65 | result = (Class) (m.invoke(o, s)); 66 | } 67 | catch (ReflectiveOperationException ex) { 68 | ex.printStackTrace(); 69 | } 70 | return result; 71 | } 72 | 73 | private static Method getIndicateMethod(Class c) { 74 | Method result = null; 75 | for (Method m : c.getMethods()) { 76 | if (m.getAnnotation(Indicate.class) != null) { 77 | result = m; 78 | break; 79 | } 80 | } 81 | return result; 82 | } 83 | */ 84 | } 85 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/indication/Indicator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.indication; 21 | 22 | import java.lang.annotation.ElementType; 23 | import java.lang.annotation.Retention; 24 | import java.lang.annotation.RetentionPolicy; 25 | import java.lang.annotation.Target; 26 | 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.FIELD) 29 | public @interface Indicator { 30 | // if later we want multiple indicated fields, use an int here 31 | } 32 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Advertise.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | 24 | @MessageType(string = "advertise") 25 | public class Advertise extends Operation { 26 | public String topic; 27 | public String type; 28 | 29 | public Advertise() {} 30 | 31 | public Advertise(String topic, String type) { 32 | this.topic = topic; 33 | this.type = type; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Authenticate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | 24 | @MessageType(string = "auth") 25 | public class Authenticate extends Operation { 26 | public String mac; 27 | public String client; 28 | public String dest; 29 | public String rand; 30 | public int t; 31 | public String level; 32 | public int end; 33 | 34 | public Authenticate() {} 35 | 36 | public Authenticate( 37 | String mac, 38 | String client, 39 | String dest, 40 | String rand, 41 | int t, 42 | String level, 43 | int end) 44 | { 45 | this.mac = mac; 46 | this.client = client; 47 | this.dest = dest; 48 | this.rand = rand; 49 | this.t = t; 50 | this.level = level; 51 | this.end = end; 52 | 53 | this.id = null; // even though id is on EVERY OTHER operation type 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/CallService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | import com.jilk.ros.message.Message; 24 | import com.jilk.ros.rosbridge.indication.*; 25 | 26 | @MessageType(string = "call_service") 27 | public class CallService extends Operation { 28 | @Indicator public String service; 29 | @Indicated @AsArray public Message args; 30 | public Integer fragment_size; // use Integer for optional items 31 | public String compression; 32 | 33 | public CallService() {} 34 | 35 | public CallService(String service, Message args) { 36 | this.service = service; 37 | this.args = args; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Fragment.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | 24 | @MessageType(string = "fragment") 25 | public class Fragment extends Operation { 26 | public String data; 27 | public int num; 28 | public int total; 29 | 30 | public Fragment() {} 31 | 32 | public Fragment(String data, int num, int total) { 33 | this.data = data; 34 | this.num = num; 35 | this.total = total; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Operation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | import com.jilk.ros.rosbridge.implementation.JSON; 25 | import com.jilk.ros.rosbridge.implementation.Registry; 26 | 27 | @MessageType(string = "operation") 28 | public class Operation extends Message { 29 | private static Long uid = 0L; 30 | 31 | public String op; 32 | public String id; 33 | 34 | public Operation() { 35 | this.op = getMessageType(getClass()); 36 | this.id = nextId(); 37 | } 38 | 39 | private static synchronized String nextId() { 40 | String result = uid.toString(); 41 | uid++; 42 | return result; 43 | } 44 | 45 | public String toJSON() { 46 | return JSON.toJSON(this); 47 | } 48 | 49 | public static Operation toOperation(String json, Registry registry) { 50 | return ((Wrapper) JSON.toMessage(json, Wrapper.class, registry)).msg; 51 | } 52 | 53 | public static void initialize(Registry registry) { 54 | initClass(registry, Advertise.class); 55 | initClass(registry, Authenticate.class); 56 | initClass(registry, CallService.class); 57 | initClass(registry, Fragment.class); 58 | initClass(registry, Operation.class); 59 | initClass(registry, PNG.class); 60 | initClass(registry, Publish.class); 61 | initClass(registry, ServiceResponse.class); 62 | initClass(registry, SetStatusLevel.class); 63 | initClass(registry, Status.class); 64 | initClass(registry, Subscribe.class); 65 | initClass(registry, Unadvertise.class); 66 | initClass(registry, Unsubscribe.class); 67 | initClass(registry, Wrapper.class); 68 | 69 | registry.register(Wrapper.class, Message.getMessageType(Publish.class), Publish.class); 70 | registry.register(Wrapper.class, Message.getMessageType(CallService.class), CallService.class); 71 | registry.register(Wrapper.class, Message.getMessageType(ServiceResponse.class), ServiceResponse.class); 72 | } 73 | 74 | private static void initClass(Registry registry, Class c) { 75 | registry.register(Message.class, Message.getMessageType(c), c); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/PNG.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "png") 26 | public class PNG extends Operation { 27 | public String data; 28 | public Integer num; // use Integer for optional items 29 | public Integer total; // use Integer for optional items 30 | 31 | public PNG() {} 32 | 33 | public PNG(String data) { 34 | this.data = data; 35 | } 36 | 37 | public PNG(String data, int num, int total) { 38 | this.data = data; 39 | this.num = num; 40 | this.total = total; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Publish.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.rosbridge.indication.Indicator; 23 | import com.jilk.ros.rosbridge.indication.Indicated; 24 | import com.jilk.ros.rosbridge.indication.Indicate; 25 | import com.jilk.ros.message.MessageType; 26 | import com.jilk.ros.message.Message; 27 | import com.jilk.ros.rosbridge.implementation.Registry; 28 | 29 | @MessageType(string = "publish") 30 | public class Publish extends Operation { 31 | 32 | @Indicator public String topic; 33 | @Indicated public Message msg; 34 | 35 | public Publish() {} 36 | 37 | public Publish(String topic, Message msg) { 38 | this.topic = topic; 39 | this.msg = msg; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/ServiceResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | import com.jilk.ros.message.Message; 24 | import com.jilk.ros.rosbridge.indication.*; 25 | 26 | @MessageType(string = "service_response") 27 | public class ServiceResponse extends Operation { 28 | @Indicator public String service; 29 | public boolean result; 30 | @Indicated public Message values; 31 | 32 | public ServiceResponse() {} 33 | 34 | public ServiceResponse(String service) { 35 | this.service = service; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/SetStatusLevel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "set_level") 26 | public class SetStatusLevel extends Operation { 27 | public String level; 28 | 29 | public SetStatusLevel() {} 30 | 31 | public SetStatusLevel(String level) { 32 | this.level = null; 33 | if ("none".equals(level) || 34 | "warning".equals(level) || 35 | "error".equals(level) || 36 | "info".equals(level)) 37 | this.level = level; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Status.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "status") 26 | public class Status extends Operation { 27 | String level; 28 | String msg; 29 | 30 | public Status() {} 31 | 32 | public Status(String level, String msg) { 33 | this.level = level; 34 | this.msg = msg; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Subscribe.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | 24 | @MessageType(string = "subscribe") 25 | public class Subscribe extends Operation { 26 | public String topic; 27 | public String type; 28 | public Integer throttle_rate; // use Integer for optional items 29 | public Integer queue_length; // use Integer for optional items 30 | public Integer fragment_size; // use Integer for optional items 31 | public String compression; 32 | 33 | public Subscribe() {} 34 | 35 | public Subscribe(String topic, String type) { 36 | this.topic = topic; 37 | this.type = type; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Unadvertise.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | 24 | @MessageType(string = "unadvertise") 25 | public class Unadvertise extends Operation { 26 | public String topic; 27 | 28 | public Unadvertise() {} 29 | 30 | public Unadvertise(String topic) { 31 | this.topic = topic; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Unsubscribe.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.message.MessageType; 23 | 24 | @MessageType(string = "unsubscribe") 25 | public class Unsubscribe extends Operation { 26 | public String topic; 27 | 28 | public Unsubscribe() {} 29 | 30 | public Unsubscribe(String topic) { 31 | this.topic = topic; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/com/jilk/ros/rosbridge/operation/Wrapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.operation; 21 | 22 | import com.jilk.ros.rosbridge.indication.Indicator; 23 | import com.jilk.ros.rosbridge.indication.Indicated; 24 | import com.jilk.ros.rosbridge.indication.Indicate; 25 | import com.jilk.ros.message.MessageType; 26 | import com.jilk.ros.message.Message; 27 | 28 | @MessageType(string = "wrapper") 29 | public class Wrapper extends Operation { 30 | @Indicator public String op; 31 | @Indicated public Operation msg; 32 | 33 | public Wrapper() {} 34 | } 35 | -------------------------------------------------------------------------------- /test/com/jilk/ros/rosbridge/implementation/JSONTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.implementation; 21 | 22 | import org.junit.After; 23 | import org.junit.AfterClass; 24 | import org.junit.Before; 25 | import org.junit.BeforeClass; 26 | import org.junit.Test; 27 | import static org.junit.Assert.*; 28 | 29 | // temporary - for tests 30 | import com.jilk.ros.rosbridge.operation.*; 31 | import com.jilk.ros.message.*; 32 | import static com.jilk.ros.rosbridge.implementation.JSON.toJSON; 33 | 34 | 35 | public class JSONTest { 36 | 37 | public JSONTest() { 38 | } 39 | 40 | @BeforeClass 41 | public static void setUpClass() { 42 | } 43 | 44 | @AfterClass 45 | public static void tearDownClass() { 46 | } 47 | 48 | @Before 49 | public void setUp() { 50 | } 51 | 52 | @After 53 | public void tearDown() { 54 | } 55 | 56 | @Test 57 | public void mainTest() { 58 | 59 | // This just runs the test, it doesn't have any assertions! 60 | 61 | Registry registry = new Registry(); 62 | 63 | TestClass t = new TestClass(); 64 | t.myInt = 99; 65 | t.myLong = 100000000; 66 | t.myDouble = 3.14159; 67 | t.myString = "Test String"; 68 | t.myTS = new TestSubClass(); 69 | t.myTS.mySubString = "Test Sub String"; 70 | t.myTS.myBooleanArray = new boolean[] {false, true}; 71 | t.myTS.myBoolean2DArray = new boolean[][] {new boolean[] {true, true}, new boolean[] {false, false}}; 72 | System.out.println("Test Class"); 73 | t.print(); 74 | System.out.println("Test Class as JSON"); 75 | String json = toJSON(t); 76 | System.out.println(json); 77 | 78 | Operation.initialize(registry); 79 | Message.register(Clock.class, registry.get(Message.class)); 80 | registry.register(Publish.class, "/clock", Clock.class); 81 | Publish p = new Publish(); 82 | p.topic = "/clock"; 83 | Clock c = new Clock(); 84 | c.clock = new TimePrimitive(); 85 | c.clock.secs = 1000; 86 | c.clock.nsecs = 999999999; 87 | p.msg = c; 88 | 89 | System.out.println("Publish Operation"); 90 | p.print(); 91 | json = toJSON(p); 92 | System.out.println("Publish Operation as JSON"); 93 | System.out.println(json); 94 | 95 | System.out.println("Publish Operation converted back from JSON"); 96 | Message p2 = Operation.toOperation(json, registry); 97 | p2.print(); 98 | 99 | Message.register(TestMessage.class, registry.get(Message.class)); 100 | registry.register(CallService.class, "/srv", TestMessage.class); 101 | TestMessage t1 = new TestMessage(); 102 | t1.testName = "Test Name"; 103 | t1.testInt = 77; 104 | t1.testDouble = 2.71828; 105 | t1.testShortArray = new short[] {1, 2, 3, 4}; 106 | CallService cs = new CallService("/srv", t1); 107 | 108 | System.out.println("CallService Operation"); 109 | cs.print(); 110 | json = toJSON(cs); 111 | 112 | System.out.println("CallService Operation as JSON"); 113 | System.out.println(json); 114 | 115 | System.out.println("CallService Operation converted back from JSON"); 116 | Message cs2 = Operation.toOperation(json, registry); 117 | cs2.print(); 118 | } 119 | 120 | public static class TestClass extends Message { 121 | public long myInt; 122 | public long myLong; 123 | public double myDouble; 124 | public String myString; 125 | public TestSubClass myTS; 126 | } 127 | 128 | public static class TestSubClass { 129 | public String mySubString; 130 | public boolean[] myBooleanArray; 131 | public boolean[][] myBoolean2DArray; 132 | } 133 | 134 | } -------------------------------------------------------------------------------- /test/com/jilk/ros/rosbridge/implementation/TestMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 Jilk Systems, Inc. 3 | * 4 | * This file is part of the Java ROSBridge Client. 5 | * 6 | * The Java ROSBridge Client is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The Java ROSBridge Client is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with the Java ROSBridge Client. If not, see http://www.gnu.org/licenses/. 18 | * 19 | */ 20 | package com.jilk.ros.rosbridge.implementation; 21 | 22 | import com.jilk.ros.message.Message; 23 | import com.jilk.ros.message.MessageType; 24 | 25 | @MessageType(string = "std_msgs/Test") 26 | public class TestMessage extends Message { 27 | public String testName; 28 | public int testInt; 29 | public double testDouble; 30 | public short[] testShortArray; 31 | } 32 | --------------------------------------------------------------------------------