001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt;
009    
010    /**
011     * keep code values in sync with
012     * 
013     * @see <a href="http://udt.sourceforge.net/udt4/doc/ecode.htm">UDT Error Codes
014     *      List</a>
015     */
016    public enum ErrorUDT {
017    
018            SUCCESS(0, "success operation"), //
019    
020            ECONNSETUP(1000, "connection setup failure"), //
021    
022            NOSERVER(1001, "server does not exist"), //
023    
024            ECONNREJ(1002, "connection request was rejected by server"), //
025    
026            ESOCKFAIL(1003, "could not create/configure UDP socket"), //
027    
028            ESECFAIL(1004, "connection request was aborted due to security reasons"), //
029    
030            ECONNFAIL(2000, "connection failure"), //
031    
032            ECONNLOST(2001, "connection was broken"), //
033    
034            ENOCONN(2002, "connection does not exist"), //
035    
036            ERESOURCE(3000, "system resource failure"), //
037    
038            ETHREAD(3001, "could not create new thread"), //
039    
040            ENOBUF(3002, "no memory space"), //
041    
042            EFILE(4000, "file access error"), //
043    
044            EINVRDOFF(4001, "invalid read offset"), //
045    
046            ERDPERM(4002, "no read permission"), //
047    
048            EINVWROFF(4003, "invalid write offset"), //
049    
050            EWRPERM(4004, "no write permission"), //
051    
052            EINVOP(5000, "operation not supported"), //
053    
054            EBOUNDSOCK(5001, "cannot execute the operation on a bound socket"), //
055    
056            ECONNSOCK(5002, "cannot execute the operation on a connected socket"), //
057    
058            EINVPARAM(5003, "bad parameters"), //
059    
060            EINVSOCK(5004, "invalid UDT socket"), //
061    
062            EUNBOUNDSOCK(5005, "cannot listen on unbound socket"), //
063    
064            ENOLISTEN(5006, "(accept) socket is not in listening state"), //
065    
066            ERDVNOSERV(5007,
067                            "rendezvous connection process does not allow listen and accept call"), //
068    
069            ERDVUNBOUND(
070                            5008,
071                            "rendezvous connection setup is enabled but bind has not been called before connect"), //
072    
073            ESTREAMILL(5009, "operation not supported in SOCK_STREAM mode"), //
074    
075            EDGRAMILL(5010, "operation not supported in SOCK_DGRAM mode"), //
076    
077            EDUPLISTEN(5011, "another socket is already listening on the same UDP port"), //
078    
079            ELARGEMSG(5012, "message is too large to be hold in the sending buffer"), //
080    
081            EINVPOLLID(5013, "epoll ID is invalid"), //
082    
083            EASYNCFAIL(6000, "non-blocking call failure"), //
084    
085            EASYNCSND(6001, "no buffer available for sending"), //
086    
087            EASYNCRCV(6002, "no data available for read"), //
088    
089            ETIMEOUT(6003, "timeout before operation completes"), //
090    
091            EPEERERR(7000, "error has happened at the peer side"), //
092    
093            // non UDT values:
094    
095            WRAPPER_UNKNOWN(-1, "unknown error code"), //
096            WRAPPER_UNIMPLEMENTED(-2, "this feature is not yet implemented"), //
097            WRAPPER_MESSAGE(-3, "wrapper generated error"), //
098            USER_DEFINED_MESSAGE(-4, "user defined message"), //
099    
100            ;
101    
102            private final int code;
103    
104            public int getCode() {
105                    return code;
106            }
107    
108            private final String description;
109    
110            public String getDescription() {
111                    return description;
112            }
113    
114            private ErrorUDT(final int code, final String description) {
115                    this.code = code;
116                    this.description = description;
117            }
118    
119            static final ErrorUDT[] ENUM_VALS = values();
120    
121            public static ErrorUDT errorFrom(final int code) {
122                    for (final ErrorUDT known : ENUM_VALS) {
123                            if (known.code == code) {
124                                    return known;
125                            }
126                    }
127                    return WRAPPER_UNKNOWN;
128            }
129    
130            //
131    
132            public static String descriptionFrom(final int socketID,
133                            final int errorCode, final String errorComment) {
134                    final ErrorUDT error = ErrorUDT.errorFrom(errorCode);
135                    return String.format("UDT Error : %d : %s : %s [id: 0x%08x]", //
136                                    errorCode, error.description, errorComment, socketID);
137            }
138    
139    }